mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
feat(webkit): introduce BrowserContext({language}) (#972)
This commit is contained in:
parent
8006b2c01f
commit
8ed88c910a
@ -44,6 +44,7 @@ export type BrowserContextOptions = {
|
|||||||
javaScriptEnabled?: boolean,
|
javaScriptEnabled?: boolean,
|
||||||
bypassCSP?: boolean,
|
bypassCSP?: boolean,
|
||||||
userAgent?: string,
|
userAgent?: string,
|
||||||
|
language?: string,
|
||||||
timezoneId?: string,
|
timezoneId?: string,
|
||||||
geolocation?: types.Geolocation,
|
geolocation?: types.Geolocation,
|
||||||
permissions?: { [key: string]: string[] };
|
permissions?: { [key: string]: string[] };
|
||||||
|
@ -79,10 +79,6 @@ export class CRNetworkManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async setUserAgent(userAgent: string) {
|
|
||||||
await this._client.send('Network.setUserAgentOverride', { userAgent });
|
|
||||||
}
|
|
||||||
|
|
||||||
async setCacheEnabled(enabled: boolean) {
|
async setCacheEnabled(enabled: boolean) {
|
||||||
this._userCacheDisabled = !enabled;
|
this._userCacheDisabled = !enabled;
|
||||||
await this._updateProtocolCacheDisabled();
|
await this._updateProtocolCacheDisabled();
|
||||||
|
@ -110,8 +110,8 @@ export class CRPage implements PageDelegate {
|
|||||||
promises.push(this._updateViewport(true /* updateTouch */));
|
promises.push(this._updateViewport(true /* updateTouch */));
|
||||||
if (options.javaScriptEnabled === false)
|
if (options.javaScriptEnabled === false)
|
||||||
promises.push(this._client.send('Emulation.setScriptExecutionDisabled', { value: true }));
|
promises.push(this._client.send('Emulation.setScriptExecutionDisabled', { value: true }));
|
||||||
if (options.userAgent)
|
if (options.userAgent || options.language)
|
||||||
this._networkManager.setUserAgent(options.userAgent);
|
promises.push(this._client.send('Emulation.setUserAgentOverride', { userAgent: options.userAgent || '', acceptLanguage: options.language }));
|
||||||
if (options.timezoneId)
|
if (options.timezoneId)
|
||||||
promises.push(emulateTimezone(this._client, options.timezoneId));
|
promises.push(emulateTimezone(this._client, options.timezoneId));
|
||||||
if (options.geolocation)
|
if (options.geolocation)
|
||||||
|
@ -323,7 +323,8 @@ export class Frame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async goto(url: string, options: GotoOptions = {}): Promise<network.Response | null> {
|
async goto(url: string, options: GotoOptions = {}): Promise<network.Response | null> {
|
||||||
let referer = (this._page._state.extraHTTPHeaders || {})['referer'];
|
const headers = (this._page._state.extraHTTPHeaders || {});
|
||||||
|
let referer = headers['referer'] || headers['Referer'];
|
||||||
if (options.referer !== undefined) {
|
if (options.referer !== undefined) {
|
||||||
if (referer !== undefined && referer !== options.referer)
|
if (referer !== undefined && referer !== options.referer)
|
||||||
throw new Error('"referer" is already specified as extra HTTP header');
|
throw new Error('"referer" is already specified as extra HTTP header');
|
||||||
|
@ -272,7 +272,7 @@ export class Page extends platform.EventEmitter {
|
|||||||
for (const key of Object.keys(headers)) {
|
for (const key of Object.keys(headers)) {
|
||||||
const value = headers[key];
|
const value = headers[key];
|
||||||
assert(helper.isString(value), `Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
|
assert(helper.isString(value), `Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
|
||||||
this._state.extraHTTPHeaders[key.toLowerCase()] = value;
|
this._state.extraHTTPHeaders[key] = value;
|
||||||
}
|
}
|
||||||
return this._delegate.setExtraHTTPHeaders(headers);
|
return this._delegate.setExtraHTTPHeaders(headers);
|
||||||
}
|
}
|
||||||
|
@ -78,6 +78,8 @@ export class WKBrowser extends platform.EventEmitter implements Browser {
|
|||||||
const context = this._createBrowserContext(browserContextId, options);
|
const context = this._createBrowserContext(browserContextId, options);
|
||||||
if (options.ignoreHTTPSErrors)
|
if (options.ignoreHTTPSErrors)
|
||||||
await this._browserSession.send('Browser.setIgnoreCertificateErrors', { browserContextId, ignore: true });
|
await this._browserSession.send('Browser.setIgnoreCertificateErrors', { browserContextId, ignore: true });
|
||||||
|
if (options.language)
|
||||||
|
await this._browserSession.send('Browser.setLanguages', { browserContextId, languages: [options.language] });
|
||||||
await context._initialize();
|
await context._initialize();
|
||||||
this._contexts.set(browserContextId, context);
|
this._contexts.set(browserContextId, context);
|
||||||
return context;
|
return context;
|
||||||
|
@ -142,8 +142,12 @@ export class WKPage implements PageDelegate {
|
|||||||
}
|
}
|
||||||
if (contextOptions.bypassCSP)
|
if (contextOptions.bypassCSP)
|
||||||
promises.push(session.send('Page.setBypassCSP', { enabled: true }));
|
promises.push(session.send('Page.setBypassCSP', { enabled: true }));
|
||||||
if (this._page._state.extraHTTPHeaders !== null)
|
if (this._page._state.extraHTTPHeaders || contextOptions.language) {
|
||||||
promises.push(session.send('Network.setExtraHTTPHeaders', { headers: this._page._state.extraHTTPHeaders }));
|
const headers = this._page._state.extraHTTPHeaders || {};
|
||||||
|
if (contextOptions.language)
|
||||||
|
headers['Accept-Language'] = contextOptions.language;
|
||||||
|
promises.push(session.send('Network.setExtraHTTPHeaders', { headers }));
|
||||||
|
}
|
||||||
if (this._page._state.hasTouch)
|
if (this._page._state.hasTouch)
|
||||||
promises.push(session.send('Page.setTouchEmulationEnabled', { enabled: true }));
|
promises.push(session.send('Page.setTouchEmulationEnabled', { enabled: true }));
|
||||||
if (contextOptions.timezoneId) {
|
if (contextOptions.timezoneId) {
|
||||||
@ -375,7 +379,11 @@ export class WKPage implements PageDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
|
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
|
||||||
await this._updateState('Network.setExtraHTTPHeaders', { headers });
|
const copy = { ...headers };
|
||||||
|
const language = this._page.context()._options.language;
|
||||||
|
if (language)
|
||||||
|
copy['Accept-Language'] = language;
|
||||||
|
await this._updateState('Network.setExtraHTTPHeaders', { headers: copy });
|
||||||
}
|
}
|
||||||
|
|
||||||
async setEmulateMedia(mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> {
|
async setEmulateMedia(mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> {
|
||||||
|
@ -107,7 +107,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, WE
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('BrowserContext({setUserAgent})', function() {
|
describe('BrowserContext({userAgent})', function() {
|
||||||
it('should work', async({newPage, server}) => {
|
it('should work', async({newPage, server}) => {
|
||||||
{
|
{
|
||||||
const page = await newPage();
|
const page = await newPage();
|
||||||
|
@ -223,6 +223,44 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe.skip(CHROMIUM || FFOX)('BrowserContext({language})', function() {
|
||||||
|
it('should affect accept-language header', async({newPage, server}) => {
|
||||||
|
const page = await newPage({ language: 'fr-CH' });
|
||||||
|
const [request] = await Promise.all([
|
||||||
|
server.waitForRequest('/empty.html'),
|
||||||
|
page.goto(server.EMPTY_PAGE),
|
||||||
|
]);
|
||||||
|
expect(request.headers['accept-language']).toBe('fr-CH');
|
||||||
|
expect(await page.evaluate(() => navigator.language)).toBe('fr-CH');
|
||||||
|
});
|
||||||
|
it('should format number', async({newPage, server}) => {
|
||||||
|
{
|
||||||
|
const page = await newPage();
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(await page.evaluate(() => (1000000.50).toLocaleString())).toBe('1,000,000.5');
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const page = await newPage({ language: 'fr-CH' });
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(await page.evaluate(() => (1000000.50).toLocaleString())).toBe('1 000 000,5');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
it('should format date', async({newPage, server}) => {
|
||||||
|
{
|
||||||
|
const page = await newPage();
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(await page.evaluate(() => new Date(1479579154987).toString())).toBe(
|
||||||
|
'Sat Nov 19 2016 10:12:34 GMT-0800 (PST)');
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const page = await newPage({ language: 'de-de', timezoneId: 'Europe/Berlin' });
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(await page.evaluate(() => new Date(1479579154987).toString())).toBe(
|
||||||
|
'Sat Nov 19 2016 19:12:34 GMT+0100 (Mitteleuropäische Normalzeit)');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('focus', function() {
|
describe('focus', function() {
|
||||||
it.skip(!headless)('should think that it is focused by default', async({page}) => {
|
it.skip(!headless)('should think that it is focused by default', async({page}) => {
|
||||||
expect(await page.evaluate('document.hasFocus()')).toBe(true);
|
expect(await page.evaluate('document.hasFocus()')).toBe(true);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user