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-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
2022-10-10 13:30:12 +00:00
|
|
|
import {KnownDevices, PredefinedNetworkConditions} from 'puppeteer';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-06-21 19:41:09 +00:00
|
|
|
import {getTestState} from './mocha-utils.js';
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-10-10 13:30:12 +00:00
|
|
|
const iPhone = KnownDevices['iPhone 6'];
|
|
|
|
const iPhoneLandscape = KnownDevices['iPhone 6 landscape'];
|
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
describe('Emulation', () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.viewport', function () {
|
|
|
|
it('should get the proper viewport size', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +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});
|
2018-11-21 03:17:56 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should support mobile emulation', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return window.innerWidth;
|
|
|
|
})
|
|
|
|
).toBe(800);
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.setViewport(iPhone.viewport);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return window.innerWidth;
|
|
|
|
})
|
|
|
|
).toBe(375);
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setViewport({width: 400, height: 300});
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return window.innerWidth;
|
|
|
|
})
|
|
|
|
).toBe(400);
|
2018-11-21 03:17:56 +00:00
|
|
|
});
|
2020-12-07 07:18:32 +00:00
|
|
|
it('should support touch emulation', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return 'ontouchstart' in window;
|
|
|
|
})
|
|
|
|
).toBe(false);
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.setViewport(iPhone.viewport);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return 'ontouchstart' in window;
|
|
|
|
})
|
|
|
|
).toBe(true);
|
2018-11-21 03:17:56 +00:00
|
|
|
expect(await page.evaluate(dispatchTouch)).toBe('Received touch');
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setViewport({width: 100, height: 100});
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return 'ontouchstart' in window;
|
|
|
|
})
|
|
|
|
).toBe(false);
|
2018-11-21 03:17:56 +00:00
|
|
|
|
|
|
|
function dispatchTouch() {
|
2022-06-15 10:09:22 +00:00
|
|
|
let fulfill!: (value: string) => void;
|
2022-06-22 13:25:44 +00:00
|
|
|
const promise = new Promise(x => {
|
2022-06-15 10:09:22 +00:00
|
|
|
fulfill = x;
|
|
|
|
});
|
2020-06-23 11:55:42 +00:00
|
|
|
window.ontouchstart = () => {
|
2018-11-21 03:17:56 +00:00
|
|
|
fulfill('Received touch');
|
|
|
|
};
|
|
|
|
window.dispatchEvent(new Event('touchstart'));
|
|
|
|
|
|
|
|
fulfill('Did not receive touch');
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
});
|
2020-12-07 07:18:32 +00:00
|
|
|
it('should be detectable by Modernizr', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.goto(server.PREFIX + '/detect-touch.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.body.textContent!.trim();
|
|
|
|
})
|
|
|
|
).toBe('NO');
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.setViewport(iPhone.viewport);
|
|
|
|
await page.goto(server.PREFIX + '/detect-touch.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.body.textContent!.trim();
|
|
|
|
})
|
|
|
|
).toBe('YES');
|
2018-11-21 03:17:56 +00:00
|
|
|
});
|
2020-12-07 07:18:32 +00:00
|
|
|
it('should detect touch when applying viewport with touches', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setViewport({width: 800, height: 600, hasTouch: true});
|
|
|
|
await page.addScriptTag({url: server.PREFIX + '/modernizr.js'});
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).Modernizr.touchevents;
|
|
|
|
})
|
|
|
|
).toBe(true);
|
2020-12-07 07:18:32 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should support landscape emulation', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return screen.orientation.type;
|
|
|
|
})
|
|
|
|
).toBe('portrait-primary');
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.setViewport(iPhoneLandscape.viewport);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return screen.orientation.type;
|
|
|
|
})
|
|
|
|
).toBe('landscape-primary');
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setViewport({width: 100, height: 100});
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return screen.orientation.type;
|
|
|
|
})
|
|
|
|
).toBe('portrait-primary');
|
2018-11-21 03:17:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.emulate', function () {
|
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
|
|
|
await page.emulate(iPhone);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return window.innerWidth;
|
|
|
|
})
|
|
|
|
).toBe(375);
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return navigator.userAgent;
|
|
|
|
})
|
|
|
|
).toContain('iPhone');
|
2018-11-21 03:17:56 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should support clicking', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:17:56 +00:00
|
|
|
await page.emulate(iPhone);
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const button = (await page.$('button'))!;
|
|
|
|
await page.evaluate((button: HTMLElement) => {
|
|
|
|
return (button.style.marginTop = '200px');
|
|
|
|
}, button);
|
2018-11-21 03:17:56 +00:00
|
|
|
await button.click();
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).result;
|
|
|
|
})
|
|
|
|
).toBe('Clicked');
|
2018-11-21 03:17:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.emulateMediaType', function () {
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('screen').matches;
|
|
|
|
})
|
|
|
|
).toBe(true);
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('print').matches;
|
|
|
|
})
|
|
|
|
).toBe(false);
|
2019-10-23 11:55:00 +00:00
|
|
|
await page.emulateMediaType('print');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('screen').matches;
|
|
|
|
})
|
|
|
|
).toBe(false);
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('print').matches;
|
|
|
|
})
|
|
|
|
).toBe(true);
|
|
|
|
await page.emulateMediaType();
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('screen').matches;
|
|
|
|
})
|
|
|
|
).toBe(true);
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('print').matches;
|
|
|
|
})
|
|
|
|
).toBe(false);
|
2018-11-21 03:17:56 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw in case of bad argument', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.emulateMediaType('bad').catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
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-05-07 10:54:55 +00:00
|
|
|
describe('Page.emulateMediaFeatures', function () {
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-10-23 11:55:00 +00:00
|
|
|
await page.emulateMediaFeatures([
|
2022-06-22 13:25:44 +00:00
|
|
|
{name: 'prefers-reduced-motion', value: 'reduce'},
|
2019-10-23 11:55:00 +00:00
|
|
|
]);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(true);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(prefers-reduced-motion: no-preference)').matches;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(false);
|
2019-10-23 11:55:00 +00:00
|
|
|
await page.emulateMediaFeatures([
|
2022-06-22 13:25:44 +00:00
|
|
|
{name: 'prefers-color-scheme', value: 'light'},
|
2019-10-23 11:55:00 +00:00
|
|
|
]);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(prefers-color-scheme: light)').matches;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(true);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(prefers-color-scheme: dark)').matches;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(false);
|
2019-10-23 11:55:00 +00:00
|
|
|
await page.emulateMediaFeatures([
|
2022-06-22 13:25:44 +00:00
|
|
|
{name: 'prefers-color-scheme', value: 'dark'},
|
2019-10-23 11:55:00 +00:00
|
|
|
]);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(prefers-color-scheme: dark)').matches;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(true);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(prefers-color-scheme: light)').matches;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(false);
|
2019-10-23 11:55:00 +00:00
|
|
|
await page.emulateMediaFeatures([
|
2022-06-22 13:25:44 +00:00
|
|
|
{name: 'prefers-reduced-motion', value: 'reduce'},
|
|
|
|
{name: 'prefers-color-scheme', value: 'light'},
|
2019-10-23 11:55:00 +00:00
|
|
|
]);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(true);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(prefers-reduced-motion: no-preference)').matches;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(false);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(prefers-color-scheme: light)').matches;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(true);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(prefers-color-scheme: dark)').matches;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(false);
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.emulateMediaFeatures([{name: 'color-gamut', value: 'srgb'}]);
|
2021-02-11 13:59:50 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(color-gamut: p3)').matches;
|
|
|
|
})
|
2021-02-11 13:59:50 +00:00
|
|
|
).toBe(false);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(color-gamut: srgb)').matches;
|
|
|
|
})
|
2021-02-11 13:59:50 +00:00
|
|
|
).toBe(true);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(color-gamut: rec2020)').matches;
|
|
|
|
})
|
2021-02-11 13:59:50 +00:00
|
|
|
).toBe(false);
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.emulateMediaFeatures([{name: 'color-gamut', value: 'p3'}]);
|
2021-02-11 13:59:50 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(color-gamut: p3)').matches;
|
|
|
|
})
|
2021-02-11 13:59:50 +00:00
|
|
|
).toBe(true);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(color-gamut: srgb)').matches;
|
|
|
|
})
|
2021-02-11 13:59:50 +00:00
|
|
|
).toBe(true);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(color-gamut: rec2020)').matches;
|
|
|
|
})
|
2021-02-11 13:59:50 +00:00
|
|
|
).toBe(false);
|
|
|
|
await page.emulateMediaFeatures([
|
2022-06-22 13:25:44 +00:00
|
|
|
{name: 'color-gamut', value: 'rec2020'},
|
2021-02-11 13:59:50 +00:00
|
|
|
]);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(color-gamut: p3)').matches;
|
|
|
|
})
|
2021-02-11 13:59:50 +00:00
|
|
|
).toBe(true);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(color-gamut: srgb)').matches;
|
|
|
|
})
|
2021-02-11 13:59:50 +00:00
|
|
|
).toBe(true);
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return matchMedia('(color-gamut: rec2020)').matches;
|
|
|
|
})
|
2021-02-11 13:59:50 +00:00
|
|
|
).toBe(true);
|
2019-10-23 11:55:00 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw in case of bad argument', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
2022-06-22 13:25:44 +00:00
|
|
|
.emulateMediaFeatures([{name: 'bad', value: ''}])
|
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2019-10-23 11:55:00 +00:00
|
|
|
expect(error.message).toBe('Unsupported media feature: bad');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Page.emulateTimezone', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-12-07 07:18:32 +00:00
|
|
|
await page.evaluate(() => {
|
2022-06-15 10:09:22 +00:00
|
|
|
(globalThis as any).date = new Date(1479579154987);
|
2019-10-23 13:49:39 +00:00
|
|
|
});
|
|
|
|
await page.emulateTimezone('America/Jamaica');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).date.toString();
|
|
|
|
})
|
|
|
|
).toBe('Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)');
|
2019-10-23 13:49:39 +00:00
|
|
|
|
|
|
|
await page.emulateTimezone('Pacific/Honolulu');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).date.toString();
|
|
|
|
})
|
|
|
|
).toBe(
|
2020-05-07 10:54:55 +00:00
|
|
|
'Sat Nov 19 2016 08:12:34 GMT-1000 (Hawaii-Aleutian Standard Time)'
|
|
|
|
);
|
2019-10-23 13:49:39 +00:00
|
|
|
|
|
|
|
await page.emulateTimezone('America/Buenos_Aires');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).date.toString();
|
|
|
|
})
|
|
|
|
).toBe('Sat Nov 19 2016 15:12:34 GMT-0300 (Argentina Standard Time)');
|
2019-10-23 13:49:39 +00:00
|
|
|
|
|
|
|
await page.emulateTimezone('Europe/Berlin');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).date.toString();
|
|
|
|
})
|
|
|
|
).toBe(
|
2020-05-07 10:54:55 +00:00
|
|
|
'Sat Nov 19 2016 19:12:34 GMT+0100 (Central European Standard Time)'
|
|
|
|
);
|
2019-10-23 13:49:39 +00:00
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw for invalid timezone IDs', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.emulateTimezone('Foo/Bar').catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2019-10-23 13:49:39 +00:00
|
|
|
expect(error.message).toBe('Invalid timezone ID: Foo/Bar');
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.emulateTimezone('Baz/Qux').catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2019-10-23 13:49:39 +00:00
|
|
|
expect(error.message).toBe('Invalid timezone ID: Baz/Qux');
|
|
|
|
});
|
|
|
|
});
|
2020-05-26 15:14:20 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Page.emulateVisionDeficiency', function () {
|
2020-05-26 15:14:20 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-05-26 15:14:20 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2020-05-26 15:14:20 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
|
|
|
|
|
|
|
{
|
|
|
|
await page.emulateVisionDeficiency('none');
|
|
|
|
const screenshot = await page.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('screenshot-sanity.png');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
await page.emulateVisionDeficiency('achromatopsia');
|
|
|
|
const screenshot = await page.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('vision-deficiency-achromatopsia.png');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
await page.emulateVisionDeficiency('blurredVision');
|
|
|
|
const screenshot = await page.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('vision-deficiency-blurredVision.png');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
await page.emulateVisionDeficiency('deuteranopia');
|
|
|
|
const screenshot = await page.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('vision-deficiency-deuteranopia.png');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
await page.emulateVisionDeficiency('protanopia');
|
|
|
|
const screenshot = await page.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('vision-deficiency-protanopia.png');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
await page.emulateVisionDeficiency('tritanopia');
|
|
|
|
const screenshot = await page.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('vision-deficiency-tritanopia.png');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
await page.emulateVisionDeficiency('none');
|
|
|
|
const screenshot = await page.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('screenshot-sanity.png');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw for invalid vision deficiencies', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-05-26 15:14:20 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-05-26 15:14:20 +00:00
|
|
|
await page
|
2022-10-06 15:30:00 +00:00
|
|
|
// @ts-expect-error deliberately passing invalid deficiency
|
2020-05-26 15:14:20 +00:00
|
|
|
.emulateVisionDeficiency('invalid')
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2020-05-26 15:14:20 +00:00
|
|
|
expect(error.message).toBe('Unsupported vision deficiency: invalid');
|
|
|
|
});
|
|
|
|
});
|
2021-05-27 13:17:12 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Page.emulateNetworkConditions', function () {
|
2021-05-27 13:17:12 +00:00
|
|
|
it('should change navigator.connection.effectiveType', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2021-05-27 13:17:12 +00:00
|
|
|
|
2022-10-06 15:30:00 +00:00
|
|
|
const slow3G = PredefinedNetworkConditions['Slow 3G']!;
|
|
|
|
const fast3G = PredefinedNetworkConditions['Fast 3G']!;
|
2021-05-27 13:17:12 +00:00
|
|
|
|
|
|
|
expect(
|
|
|
|
await page.evaluate('window.navigator.connection.effectiveType')
|
|
|
|
).toBe('4g');
|
|
|
|
await page.emulateNetworkConditions(fast3G);
|
|
|
|
expect(
|
|
|
|
await page.evaluate('window.navigator.connection.effectiveType')
|
|
|
|
).toBe('3g');
|
|
|
|
await page.emulateNetworkConditions(slow3G);
|
|
|
|
expect(
|
|
|
|
await page.evaluate('window.navigator.connection.effectiveType')
|
|
|
|
).toBe('2g');
|
|
|
|
await page.emulateNetworkConditions(null);
|
|
|
|
});
|
|
|
|
});
|
2021-06-22 11:43:11 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Page.emulateCPUThrottling', function () {
|
2021-06-22 11:43:11 +00:00
|
|
|
it('should change the CPU throttling rate successfully', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2021-06-22 11:43:11 +00:00
|
|
|
|
|
|
|
await page.emulateCPUThrottling(100);
|
|
|
|
await page.emulateCPUThrottling(null);
|
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|