Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ export class WVBrowserContext extends BrowserContext {
await (page.delegate as WVPage).updateExtraHTTPHeaders();
}

async setUserAgent(userAgent: string | undefined): Promise<void> {
this._options.userAgent = userAgent;
for (const page of this.pages())
await (page.delegate as WVPage).updateUserAgent();
}

async doAddInitScript(initScript: InitScript) {
for (const page of this.pages())
await (page.delegate as WVPage)._updateBootstrapScript();
Expand All @@ -360,7 +366,6 @@ export class WVBrowserContext extends BrowserContext {
override async clearCache(): Promise<void> { throw new Error('Method not implemented.'); }
override async doClose(reason: string | undefined): Promise<void | 'close-browser'> { throw new Error('Method not implemented.'); }
override async cancelDownload(uuid: string) { throw new Error('Method not implemented.'); }
override async setUserAgent(userAgent: string | undefined): Promise<void> { throw new Error('Method not implemented.'); }
protected override async doSetHTTPCredentials(httpCredentials?: types.Credentials): Promise<void> { throw new Error('Method not implemented.'); }
protected override async doUpdateOffline(): Promise<void> { throw new Error('Method not implemented.'); }
}
8 changes: 8 additions & 0 deletions packages/playwright-core/src/server/webkit/webview/wvPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ export class WVPage implements PageDelegate {
session.sendMayFail('Page.setBootstrapScript', { source: this._calculateBootstrapScript() }),
session.sendMayFail('Runtime.evaluate', { expression: saveGlobalsSnapshotSource, returnByValue: true } as any),
]);
const contextOptions = this._browserContext._options;
if (contextOptions.userAgent)
await session.sendMayFail('Page.overrideUserAgent', { value: contextOptions.userAgent });
if (this._page.needsRequestInterception()) {
await Promise.all([
session.sendMayFail('Network.setInterceptionEnabled', { enabled: true }),
Expand Down Expand Up @@ -566,6 +569,11 @@ export class WVPage implements PageDelegate {
return headers;
}

async updateUserAgent(): Promise<void> {
const contextOptions = this._browserContext._options;
this._updateState('Page.overrideUserAgent', { value: contextOptions.userAgent });
}

async bringToFront(): Promise<void> {
}

Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/server/webkit/wkPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class WKPage implements PageDelegate {

const contextOptions = this._browserContext._options;
if (contextOptions.userAgent)
promises.push(this.updateUserAgent());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert?

promises.push(session.send('Page.overrideUserAgent', { value: contextOptions.userAgent }));
const emulatedMedia = this._page.emulatedMedia();
if (emulatedMedia.media || emulatedMedia.colorScheme || emulatedMedia.reducedMotion || emulatedMedia.forcedColors || emulatedMedia.contrast)
promises.push(WKPage._setEmulateMedia(session, emulatedMedia.media, emulatedMedia.colorScheme, emulatedMedia.reducedMotion, emulatedMedia.forcedColors, emulatedMedia.contrast));
Expand Down
Loading