Skip to content

Commit 8cbadf1

Browse files
authored
fix(playwright): userDataDir and locale config options show error this.browser.contexts is not a function (#3814)
* fix: #2887 * fix: improve code * fix: error * fix: error with _session * fix: stabilize test * fix: session issue * fix: session error * fix: session issue
1 parent a786da7 commit 8cbadf1

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

docs/helpers/Playwright.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Traces will be saved to `output/trace`
165165
url: "http://localhost",
166166
show: true // headless mode not supported for extensions
167167
chromium: {
168+
// Note: due to this would launch persistent context, so to avoid the error when running tests with run-workers a timestamp would be appended to the defined folder name. For instance: playwright-tmp_1692715649511
168169
userDataDir: '/tmp/playwright-tmp', // necessary to launch the browser in normal mode instead of incognito,
169170
args: [
170171
`--disable-extensions-except=${pathToExtension}`,

lib/helper/Playwright.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ const config = {};
208208
* url: "http://localhost",
209209
* show: true // headless mode not supported for extensions
210210
* chromium: {
211+
* // Note: due to this would launch persistent context, so to avoid the error when running tests with run-workers a timestamp would be appended to the defined folder name. For instance: playwright-tmp_1692715649511
211212
* userDataDir: '/tmp/playwright-tmp', // necessary to launch the browser in normal mode instead of incognito,
212213
* args: [
213214
* `--disable-extensions-except=${pathToExtension}`,
@@ -396,7 +397,7 @@ class Playwright extends Helper {
396397
}
397398
this.isRemoteBrowser = !!this.playwrightOptions.browserWSEndpoint;
398399
this.isElectron = this.options.browser === 'electron';
399-
this.userDataDir = this.playwrightOptions.userDataDir;
400+
this.userDataDir = this.playwrightOptions.userDataDir ? `${this.playwrightOptions.userDataDir}_${Date.now().toString()}` : undefined;
400401
this.isCDPConnection = this.playwrightOptions.cdpConnection;
401402
popupStore.defaultAction = this.options.defaultPopupAction;
402403
}
@@ -469,7 +470,7 @@ class Playwright extends Helper {
469470
this.isAuthenticated = false;
470471
if (this.isElectron) {
471472
this.browserContext = this.browser.context();
472-
} else if (this.userDataDir) {
473+
} else if (this.playwrightOptions.userDataDir) {
473474
this.browserContext = this.browser;
474475
} else {
475476
const contextOptions = {
@@ -495,8 +496,17 @@ class Playwright extends Helper {
495496
if (this.isElectron) {
496497
mainPage = await this.browser.firstWindow();
497498
} else {
498-
const existingPages = await this.browserContext.pages();
499-
mainPage = existingPages[0] || await this.browserContext.newPage();
499+
try {
500+
const existingPages = await this.browserContext.pages();
501+
mainPage = existingPages[0] || await this.browserContext.newPage();
502+
} catch (e) {
503+
if (this.playwrightOptions.userDataDir) {
504+
this.browser = await playwright[this.options.browser].launchPersistentContext(this.userDataDir, this.playwrightOptions);
505+
this.browserContext = this.browser;
506+
const existingPages = await this.browserContext.pages();
507+
mainPage = existingPages[0];
508+
}
509+
}
500510
}
501511
await targetCreatedHandler.call(this, mainPage);
502512

@@ -527,13 +537,15 @@ class Playwright extends Helper {
527537

528538
// close other sessions
529539
try {
530-
const contexts = await this.browser.contexts();
531-
const currentContext = contexts[0];
532-
if (currentContext && (this.options.keepCookies || this.options.keepBrowserState)) {
533-
this.storageState = await currentContext.storageState();
534-
}
540+
if ((await this.browser)._type === 'Browser') {
541+
const contexts = await this.browser.contexts();
542+
const currentContext = contexts[0];
543+
if (currentContext && (this.options.keepCookies || this.options.keepBrowserState)) {
544+
this.storageState = await currentContext.storageState();
545+
}
535546

536-
await Promise.all(contexts.map(c => c.close()));
547+
await Promise.all(contexts.map(c => c.close()));
548+
}
537549
} catch (e) {
538550
console.log(e);
539551
}
@@ -563,8 +575,16 @@ class Playwright extends Helper {
563575
browserContext = browser.context();
564576
page = await browser.firstWindow();
565577
} else {
566-
browserContext = await this.browser.newContext(Object.assign(this.options, config));
567-
page = await browserContext.newPage();
578+
try {
579+
browserContext = await this.browser.newContext(Object.assign(this.options, config));
580+
page = await browserContext.newPage();
581+
} catch (e) {
582+
if (this.playwrightOptions.userDataDir) {
583+
browserContext = await playwright[this.options.browser].launchPersistentContext(`${this.userDataDir}_${this.activeSessionName}`, this.playwrightOptions);
584+
this.browser = browserContext;
585+
page = await browserContext.pages()[0];
586+
}
587+
}
568588
}
569589

570590
if (this.options.trace) await browserContext.tracing.start({ screenshots: true, snapshots: true });
@@ -577,10 +597,12 @@ class Playwright extends Helper {
577597
// is closed by _after
578598
},
579599
loadVars: async (context) => {
580-
this.browserContext = context;
581-
const existingPages = await context.pages();
582-
this.sessionPages[this.activeSessionName] = existingPages[0];
583-
return this._setPage(this.sessionPages[this.activeSessionName]);
600+
if (context) {
601+
this.browserContext = context;
602+
const existingPages = await context.pages();
603+
this.sessionPages[this.activeSessionName] = existingPages[0];
604+
return this._setPage(this.sessionPages[this.activeSessionName]);
605+
}
584606
},
585607
restoreVars: async (session) => {
586608
this.withinLocator = null;
@@ -771,7 +793,7 @@ class Playwright extends Helper {
771793
}
772794
throw err;
773795
}
774-
} else if (this.userDataDir) {
796+
} else if (this.playwrightOptions.userDataDir) {
775797
this.browser = await playwright[this.options.browser].launchPersistentContext(this.userDataDir, this.playwrightOptions);
776798
} else {
777799
this.browser = await playwright[this.options.browser].launch(this.playwrightOptions);

0 commit comments

Comments
 (0)