fix(browserContext): make a copy of passed options object (#370)

This commit is contained in:
Dmitry Gozman 2020-01-03 14:42:18 -08:00 committed by GitHub
parent 62888d8543
commit cf6f04893c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 8 deletions

6
index.d.ts vendored
View File

@ -2,6 +2,6 @@
// Licensed under the MIT license.
export * from './lib/api';
export function playwright(browser: 'chromium'): import('./lib/api').Chromium;
export function playwright(browser: 'firefox'): import('./lib/api').Firefox;
export function playwright(browser: 'webkit'): import('./lib/api').WebKit;
export function playwright(browser: 'chromium'): import('./lib/api').ChromiumPlaywright;
export function playwright(browser: 'firefox'): import('./lib/api').FirefoxPlaywright;
export function playwright(browser: 'webkit'): import('./lib/api').WebKitPlaywright;

View File

@ -53,9 +53,13 @@ export class BrowserContext {
constructor(delegate: BrowserContextDelegate, options: BrowserContextOptions) {
this._delegate = delegate;
this._options = options;
if (!options.viewport && options.viewport !== null)
options.viewport = { width: 800, height: 600 };
this._options = { ...options };
if (!this._options.viewport && this._options.viewport !== null)
this._options.viewport = { width: 800, height: 600 };
if (this._options.viewport)
this._options.viewport = { ...this._options.viewport };
if (this._options.geolocation)
this._options.geolocation = { ...this._options.geolocation };
}
async pages(): Promise<Page[]> {

View File

@ -193,7 +193,7 @@ module.exports.describe = function({testRunner, expect, FFOX, CHROME, WEBKIT}) {
expect(await page.accessibility.snapshot()).toEqual(golden);
});
// WebKit rich text accessibility is iffy
!WEBKIT && fit('rich text editable fields should have children', async function({page}) {
!WEBKIT && it('rich text editable fields should have children', async function({page}) {
await page.setContent(`
<div contenteditable="true">
Edit this image: <img src="fakeimage.png" alt="my fake image">

View File

@ -131,6 +131,16 @@ module.exports.describe = function({testRunner, expect, playwright, CHROME, WEBK
expect(await page.evaluate('window.innerWidth')).toBe(456);
expect(await page.evaluate('window.innerHeight')).toBe(789);
});
it('should make a copy of default viewport', async({ newContext }) => {
const viewport = { width: 456, height: 789 };
const context = await newContext({ viewport });
viewport.width = 567;
const page = await context.newPage();
expect(page.viewport().width).toBe(456);
expect(page.viewport().height).toBe(789);
expect(await page.evaluate('window.innerWidth')).toBe(456);
expect(await page.evaluate('window.innerHeight')).toBe(789);
});
});
describe('BrowserContext({setUserAgent})', function() {
@ -174,6 +184,17 @@ module.exports.describe = function({testRunner, expect, playwright, CHROME, WEBK
expect(await page.evaluate(() => navigator.userAgent)).toContain('iPhone');
}
});
it('should make a copy of default options', async({newContext, server}) => {
const options = { userAgent: 'foobar' };
const context = await newContext(options);
options.userAgent = 'wrong';
const page = await context.newPage();
const [request] = await Promise.all([
server.waitForRequest('/empty.html'),
page.goto(server.EMPTY_PAGE),
]);
expect(request.headers['user-agent']).toBe('foobar');
});
});
describe('BrowserContext({bypassCSP})', function() {

View File

@ -44,6 +44,12 @@ module.exports.describe = function ({ testRunner, expect, FFOX }) {
}
expect(error.message).toContain('Invalid longitude "200"');
});
it('should not modify passed default options object', async({newContext}) => {
const geolocation = { longitude: 10, latitude: 10 };
const options = { geolocation };
const context = await newContext(options);
await context.setGeolocation({ longitude: 20, latitude: 20 });
expect(options.geolocation).toBe(geolocation);
});
});
};