fix(screencast): support viewport with odd dimensions (#5399)

This commit is contained in:
Yury Semikhatsky 2021-02-10 13:37:27 -08:00 committed by GitHub
parent 7933cfc7df
commit a8ebe4d888
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 7 deletions

View File

@ -373,19 +373,22 @@ export function validateBrowserContextOptions(options: types.BrowserContextOptio
throw new Error(`"isMobile" option is not supported with null "viewport"`);
if (!options.viewport && !options.noDefaultViewport)
options.viewport = { width: 1280, height: 720 };
if (options.recordVideo && !options.recordVideo.size) {
if (options.recordVideo) {
if (!options.recordVideo.size) {
if (options.noDefaultViewport) {
options.recordVideo.size = { width: 800, height: 600 };
} else {
const size = options.viewport!;
const scale = 800 / Math.max(size.width, size.height);
if (scale < 1) {
const scale = Math.min(1, 800 / Math.max(size.width, size.height));
options.recordVideo.size = {
width: Math.floor(size.width * scale),
height: Math.floor(size.height * scale)
};
}
}
// Make sure both dimensions are odd, this is required for vp8
options.recordVideo.size!.width &= ~1;
options.recordVideo.size!.height &= ~1;
}
if (options.proxy) {
if (!browserOptions.proxy)

View File

@ -479,4 +479,26 @@ describe('screencast', suite => {
expectAll(pixels, almostRed);
}
});
it('should emulate an iphone', (test, { browserName }) => {
test.skip(browserName === 'firefox', 'isMobile is not supported in Firefox');
}, async ({contextFactory, playwright, contextOptions, testInfo}) => {
const device = playwright.devices['iPhone 6'];
const context = await contextFactory({
...contextOptions,
...device,
recordVideo: {
dir: testInfo.outputPath(''),
},
});
const page = await context.newPage();
await new Promise(r => setTimeout(r, 1000));
await context.close();
const videoFile = await page.video().path();
const videoPlayer = new VideoPlayer(videoFile);
expect(videoPlayer.videoWidth).toBe(374);
expect(videoPlayer.videoHeight).toBe(666);
});
});