diff --git a/src/node/ChromeLauncher.ts b/src/node/ChromeLauncher.ts index 26b8ef28632ba..2db109a75280e 100644 --- a/src/node/ChromeLauncher.ts +++ b/src/node/ChromeLauncher.ts @@ -92,7 +92,7 @@ export class ChromeLauncher implements ProductLauncher { } } - let isTempUserDataDir = true; + let isTempUserDataDir = false; // Check for the user data dir argument, which will always be set even // with a custom directory specified via the userDataDir option. @@ -100,6 +100,7 @@ export class ChromeLauncher implements ProductLauncher { return arg.startsWith('--user-data-dir'); }); if (userDataDirIndex < 0) { + isTempUserDataDir = true; chromeArguments.push( `--user-data-dir=${await fs.promises.mkdtemp( path.join(tmpdir(), 'puppeteer_dev_chrome_profile-') @@ -111,8 +112,6 @@ export class ChromeLauncher implements ProductLauncher { const userDataDir = chromeArguments[userDataDirIndex]!.split('=', 2)[1]; assert(typeof userDataDir === 'string', '`--user-data-dir` is malformed'); - isTempUserDataDir = false; - let chromeExecutable = executablePath; if (channel) { // executablePath is detected by channel, so it should not be specified by user. diff --git a/test/src/launcher.spec.ts b/test/src/launcher.spec.ts index 4c934063c617d..20e8331bffe39 100644 --- a/test/src/launcher.spec.ts +++ b/test/src/launcher.spec.ts @@ -251,6 +251,35 @@ describe('Launcher specs', function () { // This might throw. See https://github.com/puppeteer/puppeteer/issues/2778 await rmAsync(userDataDir).catch(() => {}); }); + itChromeOnly('tmp profile should be cleaned up', async () => { + const {defaultBrowserOptions, puppeteer} = getTestState(); + + // Set a custom test tmp dir so that we can validate that + // the profile dir is created and then cleaned up. + const testTmpDir = await fs.promises.mkdtemp( + path.join(os.tmpdir(), 'puppeteer_test_chrome_profile-') + ); + process.env['PUPPETEER_TMP_DIR'] = testTmpDir; + + // Path should be empty before starting the browser. + expect(fs.readdirSync(testTmpDir).length).toEqual(0); + const browser = await puppeteer.launch(defaultBrowserOptions); + + // One profile folder should have been created at this moment. + const profiles = fs.readdirSync(testTmpDir); + expect(profiles.length).toEqual(1); + expect(profiles[0]?.startsWith('puppeteer_dev_chrome_profile-')).toBe( + true + ); + + // Open a page to make sure its functional. + await browser.newPage(); + await browser.close(); + // Profile should be deleted after closing the browser + expect(fs.readdirSync(testTmpDir).length).toEqual(0); + // Restore env var + process.env['PUPPETEER_TMP_DIR'] = ''; + }); itFirefoxOnly('userDataDir option restores preferences', async () => { const {defaultBrowserOptions, puppeteer} = getTestState();