From cdc8cb9e6a0e4381ddd9f3e53e2025d88c75db8a Mon Sep 17 00:00:00 2001 From: Junyan Date: Tue, 18 Jul 2023 15:24:19 +0800 Subject: [PATCH] fix: set browser preferences correctly --- src/chrome-launcher.ts | 7 ++++++- test/chrome-launcher-test.ts | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/chrome-launcher.ts b/src/chrome-launcher.ts index 5d53a65f3..7b769b77b 100644 --- a/src/chrome-launcher.ts +++ b/src/chrome-launcher.ts @@ -230,7 +230,12 @@ class Launcher { return; } - const preferenceFile = `${this.userDataDir}/Preferences`; + const profileDir = `${this.userDataDir}/Default`; + if (!this.fs.existsSync(profileDir)) { + this.fs.mkdirSync(profileDir, {recursive: true}); + } + + const preferenceFile = `${profileDir}/Preferences`; try { if (this.fs.existsSync(preferenceFile)) { // overwrite existing file diff --git a/test/chrome-launcher-test.ts b/test/chrome-launcher-test.ts index 44b2b49d9..c343f502f 100644 --- a/test/chrome-launcher-test.ts +++ b/test/chrome-launcher-test.ts @@ -74,7 +74,8 @@ describe('Launcher', () => { const existStub = stub().returns(true) const readFileStub = stub().returns(JSON.stringify({ some: 'prefs' })) const writeFileStub = stub() - const fs = {...fsMock, rmdirSync: spy(), readFileSync: readFileStub, writeFileSync: writeFileStub, existsSync: existStub }; + const mkdirStub = stub() + const fs = {...fsMock, rmdir: spy(), readFileSync: readFileStub, writeFileSync: writeFileStub, existsSync: existStub, mkdirSync: mkdirStub }; const chromeInstance = new Launcher({prefs: {'download.default_directory': '/some/dir'}}, {fs: fs as any}); @@ -89,7 +90,8 @@ describe('Launcher', () => { const existStub = stub().returns(false) const readFileStub = stub().returns(Buffer.from(JSON.stringify({ some: 'prefs' }))) const writeFileStub = stub() - const fs = {...fsMock, rmdirSync: spy(), readFileSync: readFileStub, writeFileSync: writeFileStub, existsSync: existStub }; + const mkdirStub = stub() + const fs = {...fsMock, rmdir: spy(), readFileSync: readFileStub, writeFileSync: writeFileStub, existsSync: existStub, mkdirSync: mkdirStub }; const chromeInstance = new Launcher({prefs: {'download.default_directory': '/some/dir'}}, {fs: fs as any});