Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webkit): introduce BrowserContext({language}) #972

Merged
merged 1 commit into from
Feb 13, 2020
Merged
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
1 change: 1 addition & 0 deletions src/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type BrowserContextOptions = {
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
userAgent?: string,
language?: string,
timezoneId?: string,
geolocation?: types.Geolocation,
permissions?: { [key: string]: string[] };
Expand Down
4 changes: 0 additions & 4 deletions src/chromium/crNetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ export class CRNetworkManager {
});
}

async setUserAgent(userAgent: string) {
await this._client.send('Network.setUserAgentOverride', { userAgent });
}

async setCacheEnabled(enabled: boolean) {
this._userCacheDisabled = !enabled;
await this._updateProtocolCacheDisabled();
Expand Down
4 changes: 2 additions & 2 deletions src/chromium/crPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export class CRPage implements PageDelegate {
promises.push(this._updateViewport(true /* updateTouch */));
if (options.javaScriptEnabled === false)
promises.push(this._client.send('Emulation.setScriptExecutionDisabled', { value: true }));
if (options.userAgent)
this._networkManager.setUserAgent(options.userAgent);
if (options.userAgent || options.language)
promises.push(this._client.send('Emulation.setUserAgentOverride', { userAgent: options.userAgent || '', acceptLanguage: options.language }));
if (options.timezoneId)
promises.push(emulateTimezone(this._client, options.timezoneId));
if (options.geolocation)
Expand Down
3 changes: 2 additions & 1 deletion src/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ export class Frame {
}

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 (referer !== undefined && referer !== options.referer)
throw new Error('"referer" is already specified as extra HTTP header');
Expand Down
2 changes: 1 addition & 1 deletion src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class Page extends platform.EventEmitter {
for (const key of Object.keys(headers)) {
const value = headers[key];
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please update the referer logic.

Copy link
Member Author

Choose a reason for hiding this comment

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

All done

}
return this._delegate.setExtraHTTPHeaders(headers);
}
Expand Down
2 changes: 2 additions & 0 deletions src/webkit/wkBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export class WKBrowser extends platform.EventEmitter implements Browser {
const context = this._createBrowserContext(browserContextId, options);
if (options.ignoreHTTPSErrors)
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();
this._contexts.set(browserContextId, context);
return context;
Expand Down
14 changes: 11 additions & 3 deletions src/webkit/wkPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ export class WKPage implements PageDelegate {
}
if (contextOptions.bypassCSP)
promises.push(session.send('Page.setBypassCSP', { enabled: true }));
if (this._page._state.extraHTTPHeaders !== null)
promises.push(session.send('Network.setExtraHTTPHeaders', { headers: this._page._state.extraHTTPHeaders }));
if (this._page._state.extraHTTPHeaders || contextOptions.language) {
const headers = this._page._state.extraHTTPHeaders || {};
if (contextOptions.language)
headers['Accept-Language'] = contextOptions.language;
promises.push(session.send('Network.setExtraHTTPHeaders', { headers }));
Copy link
Contributor

Choose a reason for hiding this comment

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

Also update wkPage.setExtraHTTPHeaders.

}
if (this._page._state.hasTouch)
promises.push(session.send('Page.setTouchEmulationEnabled', { enabled: true }));
if (contextOptions.timezoneId) {
Expand Down Expand Up @@ -375,7 +379,11 @@ export class WKPage implements PageDelegate {
}

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> {
Expand Down
2 changes: 1 addition & 1 deletion test/browsercontext.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => {
{
const page = await newPage();
Expand Down
38 changes: 38 additions & 0 deletions test/emulation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
it.skip(!headless)('should think that it is focused by default', async({page}) => {
expect(await page.evaluate('document.hasFocus()')).toBe(true);
Expand Down