Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(default context): Apply context workaround only when browser and scenario match (#15904)

Eran Rom68d558f896de
diff --git a/packages/playwright-core/src/server/browserContext.ts b/packages/playwright-core/src/server/browserContext.ts
index 2a88e641bc..0aa9d59916 100644
--- a/packages/playwright-core/src/server/browserContext.ts
+++ b/packages/playwright-core/src/server/browserContext.ts
@@ -295,7 +295,8 @@ export abstract class BrowserContext extends SdkObject {
async _loadDefaultContext(progress: Progress) {
const pages = await this._loadDefaultContextAsIs(progress);
- if (this._options.isMobile || this._options.locale) {
+ const browserName = this._browser.options.name;
+ if ((this._options.isMobile && browserName === 'chromium') || (this._options.locale && browserName === 'webkit')) {
// Workaround for:
// - chromium fails to change isMobile for existing page;
// - webkit fails to change locale for existing page.
diff --git a/tests/library/har.spec.ts b/tests/library/har.spec.ts
index 21f1990d12..da05fc0d95 100644
--- a/tests/library/har.spec.ts
+++ b/tests/library/har.spec.ts
@@ -79,7 +79,7 @@ it('should have pages', async ({ contextFactory, server }, testInfo) => {
expect(pageEntry.pageTimings.onLoad).toBeGreaterThan(0);
});
-it('should have pages in persistent context', async ({ launchPersistent }, testInfo) => {
+it('should have pages in persistent context', async ({ launchPersistent, browserName }, testInfo) => {
const harPath = testInfo.outputPath('test.har');
const { context, page } = await launchPersistent({ recordHar: { path: harPath } });
await page.goto('data:text/html,<title>Hello</title>');
@@ -87,11 +87,17 @@ it('should have pages in persistent context', async ({ launchPersistent }, testI
await page.waitForLoadState('domcontentloaded');
await context.close();
const log = JSON.parse(fs.readFileSync(harPath).toString())['log'];
+ let pageEntry;
+ if (browserName === 'webkit') {
// Explicit locale emulation forces a new page creation when
// doing a new context.
// See https://github.com/microsoft/playwright/blob/13dd41c2e36a63f35ddef5dc5dec322052d670c6/packages/playwright-core/src/server/browserContext.ts#L232-L242
- expect(log.pages.length).toBe(2);
- const pageEntry = log.pages[1];
+ expect(log.pages.length).toBe(2);
+ pageEntry = log.pages[1];
+ } else {
+ expect(log.pages.length).toBe(1);
+ pageEntry = log.pages[0];
+ }
expect(pageEntry.id).toBeTruthy();
expect(pageEntry.title).toBe('Hello');
});