mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(webdriver): support page.setUserAgent
for WebDriver BiDi (#12330)
This commit is contained in:
parent
88b46ee502
commit
1f99e669a1
@ -76,7 +76,7 @@ export class BidiHTTPRequest extends HTTPRequest {
|
|||||||
|
|
||||||
this.#frame.page().trustedEmitter.emit(PageEvent.Request, this);
|
this.#frame.page().trustedEmitter.emit(PageEvent.Request, this);
|
||||||
|
|
||||||
if (Object.keys(this.#extraHTTPHeaders).length) {
|
if (this.#hasInternalHeaderOverwrite) {
|
||||||
this.interception.handlers.push(async () => {
|
this.interception.handlers.push(async () => {
|
||||||
await this.continue(
|
await this.continue(
|
||||||
{
|
{
|
||||||
@ -112,10 +112,21 @@ export class BidiHTTPRequest extends HTTPRequest {
|
|||||||
throw new UnsupportedOperation();
|
throw new UnsupportedOperation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get #hasInternalHeaderOverwrite(): boolean {
|
||||||
|
return Boolean(
|
||||||
|
Object.keys(this.#extraHTTPHeaders).length ||
|
||||||
|
Object.keys(this.#userAgentHeaders).length
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
get #extraHTTPHeaders(): Record<string, string> {
|
get #extraHTTPHeaders(): Record<string, string> {
|
||||||
return this.#frame?.page()._extraHTTPHeaders ?? {};
|
return this.#frame?.page()._extraHTTPHeaders ?? {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get #userAgentHeaders(): Record<string, string> {
|
||||||
|
return this.#frame?.page()._userAgentHeaders ?? {};
|
||||||
|
}
|
||||||
|
|
||||||
override headers(): Record<string, string> {
|
override headers(): Record<string, string> {
|
||||||
const headers: Record<string, string> = {};
|
const headers: Record<string, string> = {};
|
||||||
for (const header of this.#request.headers) {
|
for (const header of this.#request.headers) {
|
||||||
@ -124,6 +135,7 @@ export class BidiHTTPRequest extends HTTPRequest {
|
|||||||
return {
|
return {
|
||||||
...headers,
|
...headers,
|
||||||
...this.#extraHTTPHeaders,
|
...this.#extraHTTPHeaders,
|
||||||
|
...this.#userAgentHeaders,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,9 +181,7 @@ export class BidiHTTPRequest extends HTTPRequest {
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
return await super.continue(
|
return await super.continue(
|
||||||
{
|
{
|
||||||
headers: Object.keys(this.#extraHTTPHeaders).length
|
headers: this.#hasInternalHeaderOverwrite ? this.headers() : undefined,
|
||||||
? this.headers()
|
|
||||||
: undefined,
|
|
||||||
...overrides,
|
...overrides,
|
||||||
},
|
},
|
||||||
priority
|
priority
|
||||||
|
@ -125,16 +125,71 @@ export class BidiPage extends Page {
|
|||||||
this.#workers.delete(worker as BidiWebWorker);
|
this.#workers.delete(worker as BidiWebWorker);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
_userAgentHeaders: Record<string, string> = {};
|
||||||
|
#userAgentInterception?: string;
|
||||||
|
#userAgentPreloadScript?: string;
|
||||||
override async setUserAgent(
|
override async setUserAgent(
|
||||||
userAgent: string,
|
userAgent: string,
|
||||||
userAgentMetadata?: Protocol.Emulation.UserAgentMetadata | undefined
|
userAgentMetadata?: Protocol.Emulation.UserAgentMetadata
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// TODO: handle CDP-specific cases such as mprach.
|
if (!this.#browserContext.browser().cdpSupported && userAgentMetadata) {
|
||||||
await this._client().send('Network.setUserAgentOverride', {
|
throw new UnsupportedOperation(
|
||||||
userAgent: userAgent,
|
'Current Browser does not support `userAgentMetadata`'
|
||||||
userAgentMetadata: userAgentMetadata,
|
);
|
||||||
});
|
} else if (
|
||||||
|
this.#browserContext.browser().cdpSupported &&
|
||||||
|
userAgentMetadata
|
||||||
|
) {
|
||||||
|
return await this._client().send('Network.setUserAgentOverride', {
|
||||||
|
userAgent: userAgent,
|
||||||
|
userAgentMetadata: userAgentMetadata,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const enable = userAgent !== '';
|
||||||
|
userAgent = userAgent ?? (await this.#browserContext.browser().userAgent());
|
||||||
|
|
||||||
|
this._userAgentHeaders = enable
|
||||||
|
? {
|
||||||
|
'User-Agent': userAgent,
|
||||||
|
}
|
||||||
|
: {};
|
||||||
|
|
||||||
|
this.#userAgentInterception = await this.#toggleInterception(
|
||||||
|
[Bidi.Network.InterceptPhase.BeforeRequestSent],
|
||||||
|
this.#userAgentInterception,
|
||||||
|
enable
|
||||||
|
);
|
||||||
|
|
||||||
|
const changeUserAgent = (userAgent: string) => {
|
||||||
|
Object.defineProperty(navigator, 'userAgent', {
|
||||||
|
value: userAgent,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const frames = [this.#frame];
|
||||||
|
for (const frame of frames) {
|
||||||
|
frames.push(...frame.childFrames());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.#userAgentPreloadScript) {
|
||||||
|
await this.removeScriptToEvaluateOnNewDocument(
|
||||||
|
this.#userAgentPreloadScript
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const [evaluateToken] = await Promise.all([
|
||||||
|
enable
|
||||||
|
? this.evaluateOnNewDocument(changeUserAgent, userAgent)
|
||||||
|
: undefined,
|
||||||
|
// When we disable the UserAgent we want to
|
||||||
|
// evaluate the original value in all Browsing Contexts
|
||||||
|
frames.map(frame => {
|
||||||
|
return frame.evaluate(changeUserAgent, userAgent);
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
this.#userAgentPreloadScript = evaluateToken?.identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
override async setBypassCSP(enabled: boolean): Promise<void> {
|
override async setBypassCSP(enabled: boolean): Promise<void> {
|
||||||
|
@ -676,8 +676,8 @@
|
|||||||
"testIdPattern": "[page.spec] Page Page.setUserAgent *",
|
"testIdPattern": "[page.spec] Page Page.setUserAgent *",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
"parameters": ["firefox", "webDriverBiDi"],
|
"parameters": ["firefox", "webDriverBiDi"],
|
||||||
"expectations": ["FAIL"],
|
"expectations": ["SKIP"],
|
||||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
"comment": "Firefox does not support headers override"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"testIdPattern": "[pdf.spec] Page.pdf *",
|
"testIdPattern": "[pdf.spec] Page.pdf *",
|
||||||
@ -1424,15 +1424,8 @@
|
|||||||
"testIdPattern": "[emulation.spec] Emulation Page.emulate should support clicking",
|
"testIdPattern": "[emulation.spec] Emulation Page.emulate should support clicking",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
"parameters": ["firefox", "webDriverBiDi"],
|
"parameters": ["firefox", "webDriverBiDi"],
|
||||||
"expectations": ["FAIL"],
|
"expectations": ["SKIP"],
|
||||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
"comment": "Firefox does not support headers override"
|
||||||
},
|
|
||||||
{
|
|
||||||
"testIdPattern": "[emulation.spec] Emulation Page.emulate should work",
|
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
|
||||||
"parameters": ["firefox", "webDriverBiDi"],
|
|
||||||
"expectations": ["FAIL"],
|
|
||||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"testIdPattern": "[emulation.spec] Emulation Page.emulateCPUThrottling should change the CPU throttling rate successfully",
|
"testIdPattern": "[emulation.spec] Emulation Page.emulateCPUThrottling should change the CPU throttling rate successfully",
|
||||||
@ -3074,7 +3067,7 @@
|
|||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
"parameters": ["firefox", "webDriverBiDi"],
|
"parameters": ["firefox", "webDriverBiDi"],
|
||||||
"expectations": ["FAIL"],
|
"expectations": ["FAIL"],
|
||||||
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
|
"comment": "https://bugzilla.mozilla.org/show_bug.cgi?id=1866749"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"testIdPattern": "[page.spec] Page Page.Events.error should throw when page crashes",
|
"testIdPattern": "[page.spec] Page Page.Events.error should throw when page crashes",
|
||||||
|
@ -1327,6 +1327,31 @@ describe('Page', function () {
|
|||||||
expect(uaData['platformVersion']).toBe('3.1');
|
expect(uaData['platformVersion']).toBe('3.1');
|
||||||
expect(request.headers['user-agent']).toBe('MockBrowser');
|
expect(request.headers['user-agent']).toBe('MockBrowser');
|
||||||
});
|
});
|
||||||
|
it('should restore original', async () => {
|
||||||
|
const {page, server} = await getTestState();
|
||||||
|
|
||||||
|
const userAgent = await page.evaluate(() => {
|
||||||
|
return navigator.userAgent;
|
||||||
|
});
|
||||||
|
|
||||||
|
await page.setUserAgent('foobar');
|
||||||
|
const [requestWithOverride] = await Promise.all([
|
||||||
|
server.waitForRequest('/empty.html'),
|
||||||
|
page.goto(server.EMPTY_PAGE),
|
||||||
|
]);
|
||||||
|
expect(requestWithOverride.headers['user-agent']).toBe('foobar');
|
||||||
|
|
||||||
|
await page.setUserAgent('');
|
||||||
|
const [request] = await Promise.all([
|
||||||
|
server.waitForRequest('/empty.html'),
|
||||||
|
page.goto(server.EMPTY_PAGE),
|
||||||
|
]);
|
||||||
|
expect(request.headers['user-agent']).toBe(userAgent);
|
||||||
|
const userAgentRestored = await page.evaluate(() => {
|
||||||
|
return navigator.userAgent;
|
||||||
|
});
|
||||||
|
expect(userAgentRestored).toBe(userAgent);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Page.setContent', function () {
|
describe('Page.setContent', function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user