2018-11-21 03:17:56 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
const expect = require('expect');
|
|
|
|
const {getTestState,setupTestBrowserHooks, setupTestPageAndContextHooks} = require('./mocha-utils');
|
|
|
|
|
|
|
|
describe('Emulation', () => {
|
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
|
|
|
let iPhone;
|
|
|
|
let iPhoneLandscape;
|
|
|
|
|
|
|
|
before(() => {
|
|
|
|
const {puppeteer} = getTestState();
|
|
|
|
iPhone = puppeteer.devices['iPhone 6'];
|
|
|
|
iPhoneLandscape = puppeteer.devices['iPhone 6 landscape'];
|
|
|
|
});
|
2019-04-19 22:33:06 +00:00
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
describe('Page.viewport', function() {
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
it('should get the proper viewport size', async() => {
|
|
|
|
const { page } = getTestState();
|
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
expect(page.viewport()).toEqual({width: 800, height: 600});
|
|
|
|
await page.setViewport({width: 123, height: 456});
|
|
|
|
expect(page.viewport()).toEqual({width: 123, height: 456});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
it('should support mobile emulation', async() => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
|
|
|
expect(await page.evaluate(() => window.innerWidth)).toBe(800);
|
|
|
|
await page.setViewport(iPhone.viewport);
|
|
|
|
expect(await page.evaluate(() => window.innerWidth)).toBe(375);
|
|
|
|
await page.setViewport({width: 400, height: 300});
|
|
|
|
expect(await page.evaluate(() => window.innerWidth)).toBe(400);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
itFailsFirefox('should support touch emulation', async() => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
|
|
|
expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(false);
|
|
|
|
await page.setViewport(iPhone.viewport);
|
|
|
|
expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(true);
|
|
|
|
expect(await page.evaluate(dispatchTouch)).toBe('Received touch');
|
|
|
|
await page.setViewport({width: 100, height: 100});
|
|
|
|
expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(false);
|
|
|
|
|
|
|
|
function dispatchTouch() {
|
|
|
|
let fulfill;
|
|
|
|
const promise = new Promise(x => fulfill = x);
|
|
|
|
window.ontouchstart = function(e) {
|
|
|
|
fulfill('Received touch');
|
|
|
|
};
|
|
|
|
window.dispatchEvent(new Event('touchstart'));
|
|
|
|
|
|
|
|
fulfill('Did not receive touch');
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
itFailsFirefox('should be detectable by Modernizr', async() => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.goto(server.PREFIX + '/detect-touch.html');
|
|
|
|
expect(await page.evaluate(() => document.body.textContent.trim())).toBe('NO');
|
|
|
|
await page.setViewport(iPhone.viewport);
|
|
|
|
await page.goto(server.PREFIX + '/detect-touch.html');
|
|
|
|
expect(await page.evaluate(() => document.body.textContent.trim())).toBe('YES');
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
itFailsFirefox('should detect touch when applying viewport with touches', async() => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.setViewport({ width: 800, height: 600, hasTouch: true });
|
|
|
|
await page.addScriptTag({url: server.PREFIX + '/modernizr.js'});
|
|
|
|
expect(await page.evaluate(() => Modernizr.touchevents)).toBe(true);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
itFailsFirefox('should support landscape emulation', async() => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
|
|
|
expect(await page.evaluate(() => screen.orientation.type)).toBe('portrait-primary');
|
|
|
|
await page.setViewport(iPhoneLandscape.viewport);
|
|
|
|
expect(await page.evaluate(() => screen.orientation.type)).toBe('landscape-primary');
|
|
|
|
await page.setViewport({width: 100, height: 100});
|
|
|
|
expect(await page.evaluate(() => screen.orientation.type)).toBe('portrait-primary');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-02-09 03:53:27 +00:00
|
|
|
describe('Page.emulate', function() {
|
2020-04-09 05:56:25 +00:00
|
|
|
it('should work', async() => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
|
|
|
await page.emulate(iPhone);
|
|
|
|
expect(await page.evaluate(() => window.innerWidth)).toBe(375);
|
2019-02-09 03:53:27 +00:00
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain('iPhone');
|
2018-11-21 03:17:56 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
it('should support clicking', async() => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.emulate(iPhone);
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
|
|
|
const button = await page.$('button');
|
|
|
|
await page.evaluate(button => button.style.marginTop = '200px', button);
|
|
|
|
await button.click();
|
|
|
|
expect(await page.evaluate(() => result)).toBe('Clicked');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-06 10:25:09 +00:00
|
|
|
describe('Page.emulateMedia [deprecated]', function() {
|
2020-04-09 05:56:25 +00:00
|
|
|
/* emulateMedia is deprecated in favour of emulateMediaType but we
|
2020-04-06 10:25:09 +00:00
|
|
|
* don't want to remove it from Puppeteer just yet. We can't check
|
|
|
|
* that emulateMedia === emulateMediaType because when running tests
|
|
|
|
* with COVERAGE=1 the methods get rewritten. So instead we
|
|
|
|
* duplicate the tests for emulateMediaType and ensure they pass
|
|
|
|
* when calling the deprecated emulateMedia method.
|
|
|
|
*
|
|
|
|
* If you update these tests, you should update emulateMediaType's
|
|
|
|
* tests, and vice-versa.
|
|
|
|
*/
|
2020-04-09 05:56:25 +00:00
|
|
|
itFailsFirefox('should work', async() => {
|
|
|
|
const { page } = getTestState();
|
|
|
|
|
2020-04-06 10:25:09 +00:00
|
|
|
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false);
|
|
|
|
await page.emulateMedia('print');
|
|
|
|
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false);
|
|
|
|
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true);
|
|
|
|
await page.emulateMedia(null);
|
|
|
|
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
it('should throw in case of bad argument', async() => {
|
|
|
|
const { page } = getTestState();
|
|
|
|
|
2020-04-06 10:25:09 +00:00
|
|
|
let error = null;
|
|
|
|
await page.emulateMedia('bad').catch(e => error = e);
|
|
|
|
expect(error.message).toBe('Unsupported media type: bad');
|
2019-10-23 11:55:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Page.emulateMediaType', function() {
|
2020-04-09 05:56:25 +00:00
|
|
|
/* NOTE! Updating these tests? Update the emulateMedia tests above
|
2020-04-06 10:25:09 +00:00
|
|
|
* too (and see the big comment for why we have these duplicated).
|
|
|
|
*/
|
2020-04-09 05:56:25 +00:00
|
|
|
itFailsFirefox('should work', async() => {
|
|
|
|
const { page } = getTestState();
|
|
|
|
|
2019-10-23 11:55:00 +00:00
|
|
|
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false);
|
|
|
|
await page.emulateMediaType('print');
|
|
|
|
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false);
|
|
|
|
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true);
|
|
|
|
await page.emulateMediaType(null);
|
|
|
|
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false);
|
2018-11-21 03:17:56 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
it('should throw in case of bad argument', async() => {
|
|
|
|
const { page } = getTestState();
|
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
let error = null;
|
2019-10-23 11:55:00 +00:00
|
|
|
await page.emulateMediaType('bad').catch(e => error = e);
|
2018-11-21 03:17:56 +00:00
|
|
|
expect(error.message).toBe('Unsupported media type: bad');
|
|
|
|
});
|
|
|
|
});
|
2019-10-23 11:55:00 +00:00
|
|
|
|
2020-02-20 12:57:15 +00:00
|
|
|
describe('Page.emulateMediaFeatures', function() {
|
2020-04-09 05:56:25 +00:00
|
|
|
itFailsFirefox('should work', async() => {
|
|
|
|
const { page } = getTestState();
|
|
|
|
|
2019-10-23 11:55:00 +00:00
|
|
|
await page.emulateMediaFeatures([
|
|
|
|
{ name: 'prefers-reduced-motion', value: 'reduce' },
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: reduce)').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: no-preference)').matches)).toBe(false);
|
|
|
|
await page.emulateMediaFeatures([
|
|
|
|
{ name: 'prefers-color-scheme', value: 'light' },
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: light)').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches)).toBe(false);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: no-preference)').matches)).toBe(false);
|
|
|
|
await page.emulateMediaFeatures([
|
|
|
|
{ name: 'prefers-color-scheme', value: 'dark' },
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: light)').matches)).toBe(false);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: no-preference)').matches)).toBe(false);
|
|
|
|
await page.emulateMediaFeatures([
|
|
|
|
{ name: 'prefers-reduced-motion', value: 'reduce' },
|
|
|
|
{ name: 'prefers-color-scheme', value: 'light' },
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: reduce)').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: no-preference)').matches)).toBe(false);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: light)').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches)).toBe(false);
|
|
|
|
expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: no-preference)').matches)).toBe(false);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
it('should throw in case of bad argument', async() => {
|
|
|
|
const { page } = getTestState();
|
|
|
|
|
2019-10-23 11:55:00 +00:00
|
|
|
let error = null;
|
|
|
|
await page.emulateMediaFeatures([{ name: 'bad', value: '' }]).catch(e => error = e);
|
|
|
|
expect(error.message).toBe('Unsupported media feature: bad');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
describeFailsFirefox('Page.emulateTimezone', function() {
|
|
|
|
it('should work', async() => {
|
|
|
|
const { page } = getTestState();
|
|
|
|
|
2019-10-23 13:49:39 +00:00
|
|
|
page.evaluate(() => {
|
|
|
|
globalThis.date = new Date(1479579154987);
|
|
|
|
});
|
|
|
|
await page.emulateTimezone('America/Jamaica');
|
|
|
|
expect(await page.evaluate(() => date.toString())).toBe('Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)');
|
|
|
|
|
|
|
|
await page.emulateTimezone('Pacific/Honolulu');
|
|
|
|
expect(await page.evaluate(() => date.toString())).toBe('Sat Nov 19 2016 08:12:34 GMT-1000 (Hawaii-Aleutian Standard Time)');
|
|
|
|
|
|
|
|
await page.emulateTimezone('America/Buenos_Aires');
|
|
|
|
expect(await page.evaluate(() => date.toString())).toBe('Sat Nov 19 2016 15:12:34 GMT-0300 (Argentina Standard Time)');
|
|
|
|
|
|
|
|
await page.emulateTimezone('Europe/Berlin');
|
|
|
|
expect(await page.evaluate(() => date.toString())).toBe('Sat Nov 19 2016 19:12:34 GMT+0100 (Central European Standard Time)');
|
|
|
|
});
|
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
it('should throw for invalid timezone IDs', async() => {
|
|
|
|
const { page } = getTestState();
|
|
|
|
|
2019-10-23 13:49:39 +00:00
|
|
|
let error = null;
|
|
|
|
await page.emulateTimezone('Foo/Bar').catch(e => error = e);
|
|
|
|
expect(error.message).toBe('Invalid timezone ID: Foo/Bar');
|
|
|
|
await page.emulateTimezone('Baz/Qux').catch(e => error = e);
|
|
|
|
expect(error.message).toBe('Invalid timezone ID: Baz/Qux');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|