feat(viewport): update defaults to 1280x720, fix Firefox (#1038)

This commit is contained in:
Pavel Feldman 2020-02-18 09:16:32 -08:00 committed by GitHub
parent f2b2d72693
commit 1ee657823e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 12 deletions

View file

@ -184,7 +184,7 @@ Indicates that the browser is connected.
- `options` <[Object]> - `options` <[Object]>
- `ignoreHTTPSErrors` <?[boolean]> Whether to ignore HTTPS errors during navigation. Defaults to `false`. - `ignoreHTTPSErrors` <?[boolean]> Whether to ignore HTTPS errors during navigation. Defaults to `false`.
- `bypassCSP` <?[boolean]> Toggles bypassing page's Content-Security-Policy. - `bypassCSP` <?[boolean]> Toggles bypassing page's Content-Security-Policy.
- `viewport` <?[Object]> Sets a consistent viewport for each page. Defaults to an 800x600 viewport. `null` disables the default viewport. - `viewport` <?[Object]> Sets a consistent viewport for each page. Defaults to an 1280x720 viewport. `null` disables the default viewport.
- `width` <[number]> page width in pixels. - `width` <[number]> page width in pixels.
- `height` <[number]> page height in pixels. - `height` <[number]> page height in pixels.
- `deviceScaleFactor` <[number]> Specify device scale factor (can be thought of as dpr). Defaults to `1`. - `deviceScaleFactor` <[number]> Specify device scale factor (can be thought of as dpr). Defaults to `1`.
@ -217,7 +217,7 @@ Creates a new browser context. It won't share cookies/cache with other browser c
- `options` <[Object]> - `options` <[Object]>
- `ignoreHTTPSErrors` <?[boolean]> Whether to ignore HTTPS errors during navigation. Defaults to `false`. - `ignoreHTTPSErrors` <?[boolean]> Whether to ignore HTTPS errors during navigation. Defaults to `false`.
- `bypassCSP` <?[boolean]> Toggles bypassing page's Content-Security-Policy. - `bypassCSP` <?[boolean]> Toggles bypassing page's Content-Security-Policy.
- `viewport` <?[Object]> Sets a consistent viewport for each page. Defaults to an 800x600 viewport. `null` disables the default viewport. - `viewport` <?[Object]> Sets a consistent viewport for each page. Defaults to an 1280x720 viewport. `null` disables the default viewport.
- `width` <[number]> page width in pixels. - `width` <[number]> page width in pixels.
- `height` <[number]> page height in pixels. - `height` <[number]> page height in pixels.
- `deviceScaleFactor` <[number]> Specify device scale factor (can be thought of as dpr). Defaults to `1`. - `deviceScaleFactor` <[number]> Specify device scale factor (can be thought of as dpr). Defaults to `1`.

View file

@ -63,7 +63,7 @@ export class BrowserContext extends platform.EventEmitter {
this._timeoutSettings = new TimeoutSettings(); this._timeoutSettings = new TimeoutSettings();
this._options = { ...options }; this._options = { ...options };
if (!this._options.viewport && this._options.viewport !== null) if (!this._options.viewport && this._options.viewport !== null)
this._options.viewport = { width: 800, height: 600 }; this._options.viewport = { width: 1280, height: 720 };
if (this._options.viewport) if (this._options.viewport)
this._options.viewport = { ...this._options.viewport }; this._options.viewport = { ...this._options.viewport };
if (this._options.geolocation) if (this._options.geolocation)

View file

@ -67,13 +67,23 @@ export class FFBrowser extends platform.EventEmitter implements Browser {
} }
async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> { async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> {
const viewport = options.viewport ? { let viewport;
viewportSize: { width: options.viewport.width, height: options.viewport.height }, if (options.viewport) {
isMobile: !!options.viewport.isMobile, viewport = {
deviceScaleFactor: options.viewport.deviceScaleFactor || 1, viewportSize: { width: options.viewport.width, height: options.viewport.height },
hasTouch: !!options.viewport.isMobile, isMobile: !!options.viewport.isMobile,
} : undefined; deviceScaleFactor: options.viewport.deviceScaleFactor || 1,
const {browserContextId} = await this._connection.send('Target.createBrowserContext', { hasTouch: !!options.viewport.isMobile,
};
} else if (options.viewport !== null) {
viewport = {
viewportSize: { width: 1280, height: 720 },
isMobile: false,
deviceScaleFactor: 1,
hasTouch: false,
};
}
const { browserContextId } = await this._connection.send('Target.createBrowserContext', {
userAgent: options.userAgent, userAgent: options.userAgent,
bypassCSP: options.bypassCSP, bypassCSP: options.bypassCSP,
javaScriptDisabled: options.javaScriptEnabled === false ? true : undefined, javaScriptDisabled: options.javaScriptEnabled === false ? true : undefined,

View file

@ -25,11 +25,18 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
const iPhone = playwright.devices['iPhone 6']; const iPhone = playwright.devices['iPhone 6'];
const iPhoneLandscape = playwright.devices['iPhone 6 landscape']; const iPhoneLandscape = playwright.devices['iPhone 6 landscape'];
describe('Page.viewport', function() { describe('BrowserContext({viewport})', function() {
it('should get the proper viewport size', async({page, server}) => { it('should get the proper viewport size', async({page, server}) => {
expect(page.viewportSize()).toEqual({width: 800, height: 600}); expect(page.viewportSize()).toEqual({width: 1280, height: 720});
expect(await page.evaluate(() => window.innerWidth)).toBe(1280);
expect(await page.evaluate(() => window.innerHeight)).toBe(720);
});
it('should set the proper viewport size', async({page, server}) => {
expect(page.viewportSize()).toEqual({width: 1280, height: 720});
await page.setViewportSize({width: 123, height: 456}); await page.setViewportSize({width: 123, height: 456});
expect(page.viewportSize()).toEqual({width: 123, height: 456}); expect(page.viewportSize()).toEqual({width: 123, height: 456});
expect(await page.evaluate(() => window.innerWidth)).toBe(123);
expect(await page.evaluate(() => window.innerHeight)).toBe(456);
}); });
it('should support mobile emulation', async({newContext, server}) => { it('should support mobile emulation', async({newContext, server}) => {
const context = await newContext({ viewport: iPhone.viewport }); const context = await newContext({ viewport: iPhone.viewport });