feat(media): remove media emulation from browser context options (#377)

This commit is contained in:
Pavel Feldman 2020-01-05 14:39:16 -08:00 committed by Yury Semikhatsky
parent 2f3593bd9a
commit 38e79f12ec
8 changed files with 13 additions and 26 deletions

View File

@ -2015,7 +2015,7 @@ Shortcut for [page.mainFrame().dblclick(selector[, options])](#framedblclicksele
#### page.emulateMedia(options)
- `options` <[Object]>
- `type` <"screen"|"print"> Changes the CSS media type of the page. The only allowed values are `'screen'`, `'print'` and `null`. Passing `null` disables CSS media emulation.
- `media` <"screen"|"print"> Changes the CSS media type of the page. The only allowed values are `'screen'`, `'print'` and `null`. Passing `null` disables CSS media emulation.
- `colorScheme` <"dark"|"light"|"no-preference"> Emulates `'prefers-colors-scheme'` media feature, supported values are `'light'`, `'dark'`, `'no-preference'`.
- returns: <[Promise]>
@ -2025,7 +2025,7 @@ await page.evaluate(() => matchMedia('screen').matches));
await page.evaluate(() => matchMedia('print').matches));
// → true
await page.emulateMedia({ type: 'print' });
await page.emulateMedia({ media: 'print' });
await page.evaluate(() => matchMedia('screen').matches));
// → false
await page.evaluate(() => matchMedia('print').matches));

View File

@ -39,8 +39,6 @@ export type BrowserContextOptions = {
ignoreHTTPSErrors?: boolean,
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
mediaType?: types.MediaType,
colorScheme?: types.ColorScheme,
userAgent?: string,
timezoneId?: string,
geolocation?: types.Geolocation

View File

@ -104,10 +104,6 @@ export class CRPage implements PageDelegate {
promises.push(this._client.send('Emulation.setScriptExecutionDisabled', { value: true }));
if (options.userAgent)
this._networkManager.setUserAgent(options.userAgent);
if (options.mediaType || options.colorScheme) {
const features = options.colorScheme ? [{ name: 'prefers-color-scheme', value: options.colorScheme }] : [];
promises.push(this._client.send('Emulation.setEmulatedMedia', { media: options.mediaType || '', features }));
}
if (options.timezoneId)
promises.push(emulateTimezone(this._client, options.timezoneId));
if (options.geolocation)

View File

@ -82,8 +82,6 @@ export class FFPage implements PageDelegate {
promises.push(this._session.send('Page.setJavascriptEnabled', { enabled: false }));
if (options.userAgent)
promises.push(this._session.send('Page.setUserAgent', { userAgent: options.userAgent }));
if (options.mediaType || options.colorScheme)
promises.push(this._session.send('Page.setEmulatedMedia', { type: options.mediaType, colorScheme: options.colorScheme }));
await Promise.all(promises);
}

View File

@ -113,8 +113,8 @@ export class Page extends EventEmitter {
this._browserContext = browserContext;
this._state = {
viewport: browserContext._options.viewport || null,
mediaType: browserContext._options.mediaType || null,
colorScheme: browserContext._options.colorScheme || null,
mediaType: null,
colorScheme: null,
extraHTTPHeaders: null,
cacheEnabled: null,
interceptNetwork: null,
@ -370,11 +370,11 @@ export class Page extends EventEmitter {
return waitPromise;
}
async emulateMedia(options: { type?: types.MediaType, colorScheme?: types.ColorScheme }) {
assert(!options.type || types.mediaTypes.has(options.type), 'Unsupported media type: ' + options.type);
async emulateMedia(options: { media?: types.MediaType, colorScheme?: types.ColorScheme }) {
assert(!options.media || types.mediaTypes.has(options.media), 'Unsupported media: ' + options.media);
assert(!options.colorScheme || types.colorSchemes.has(options.colorScheme), 'Unsupported color scheme: ' + options.colorScheme);
if (options.type !== undefined)
this._state.mediaType = options.type;
if (options.media !== undefined)
this._state.mediaType = options.media;
if (options.colorScheme !== undefined)
this._state.colorScheme = options.colorScheme;
await this._delegate.setEmulateMedia(this._state.mediaType, this._state.colorScheme);

View File

@ -100,11 +100,6 @@ 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.skip(WEBKIT)('should propagate default mediaType and colorScheme to the page', async({ newPage }) => {
const page = await newPage({ mediaType: 'print', colorScheme: 'dark' });
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true);
expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches)).toBe(true);
});
it('should take fullPage screenshots when default viewport is null', async({server, newPage}) => {
const page = await newPage({ viewport: null });
await page.goto(server.PREFIX + '/grid.html');

View File

@ -117,20 +117,20 @@ module.exports.describe = function({testRunner, expect, playwright, FFOX, CHROME
it('should work', async({page, server}) => {
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true);
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false);
await page.emulateMedia({ type: 'print' });
await page.emulateMedia({ media: 'print' });
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false);
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true);
await page.emulateMedia({});
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false);
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true);
await page.emulateMedia({ type: '' });
await page.emulateMedia({ media: '' });
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true);
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false);
});
it('should throw in case of bad type argument', async({page, server}) => {
let error = null;
await page.emulateMedia({ type: 'bad' }).catch(e => error = e);
expect(error.message).toBe('Unsupported media type: bad');
await page.emulateMedia({ media: 'bad' }).catch(e => error = e);
expect(error.message).toBe('Unsupported media: bad');
});
});

View File

@ -172,7 +172,7 @@ module.exports.describe = function({testRunner, expect, headless, playwright, FF
expect(await page.evaluate(() => !!window.opener)).toBe(false);
expect(await popup.evaluate(() => !!window.opener)).toBe(false);
});
it.skip(WEBKIT || FFOX)('should not treat navigations as new popups', async({page, server}) => {
it.skip(FFOX)('should not treat navigations as new popups', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
const [popup] = await Promise.all([