Skip to content

Commit

Permalink
feat(webkit): introduce BrowserContext({language})
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Feb 13, 2020
1 parent 5dbc880 commit 916c7d4
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 10 deletions.
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
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;
}
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
8 changes: 6 additions & 2 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 }));
}
if (this._page._state.hasTouch)
promises.push(session.send('Page.setTouchEmulationEnabled', { enabled: true }));
if (contextOptions.timezoneId) {
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
24 changes: 24 additions & 0 deletions test/emulation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,30 @@ 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 affect number format', 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');
}
});
});

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

0 comments on commit 916c7d4

Please sign in to comment.