| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2018 Google Inc. All rights reserved. | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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-02-10 13:20:13 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @type {PageTestSuite} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-02-13 15:12:54 -08:00
										 |  |  | module.exports.describe = function({testRunner, expect, playwright, headless, FFOX, CHROMIUM, WEBKIT, MAC, WIN}) { | 
					
						
							| 
									
										
										
										
											2019-11-22 14:46:34 -08:00
										 |  |  |   const {describe, xdescribe, fdescribe} = testRunner; | 
					
						
							| 
									
										
										
										
											2019-12-19 15:47:35 -08:00
										 |  |  |   const {it, fit, xit, dit} = testRunner; | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; | 
					
						
							|  |  |  |   const iPhone = playwright.devices['iPhone 6']; | 
					
						
							|  |  |  |   const iPhoneLandscape = playwright.devices['iPhone 6 landscape']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-18 09:16:32 -08:00
										 |  |  |   describe('BrowserContext({viewport})', function() { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     it('should get the proper viewport size', async({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-02-18 09:16:32 -08:00
										 |  |  |       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}); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       await page.setViewportSize({width: 123, height: 456}); | 
					
						
							|  |  |  |       expect(page.viewportSize()).toEqual({width: 123, height: 456}); | 
					
						
							| 
									
										
										
										
											2020-02-18 09:16:32 -08:00
										 |  |  |       expect(await page.evaluate(() => window.innerWidth)).toBe(123); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => window.innerHeight)).toBe(456); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-26 21:40:38 -07:00
										 |  |  |     it('should emulate device width', async({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-09 14:58:38 -07:00
										 |  |  |       expect(page.viewportSize()).toEqual({width: 1280, height: 720}); | 
					
						
							| 
									
										
										
										
											2020-03-09 15:53:31 -07:00
										 |  |  |       await page.setViewportSize({width: 200, height: 200}); | 
					
						
							| 
									
										
										
										
											2020-03-26 21:40:38 -07:00
										 |  |  |       expect(await page.evaluate(() => window.screen.width)).toBe(200); | 
					
						
							| 
									
										
										
										
											2020-03-09 15:53:31 -07:00
										 |  |  |       expect(await page.evaluate(() => matchMedia('(min-device-width: 100px)').matches)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(min-device-width: 300px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(max-device-width: 100px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(max-device-width: 300px)').matches)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(device-width: 500px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(device-width: 200px)').matches)).toBe(true); | 
					
						
							|  |  |  |       await page.setViewportSize({width: 500, height: 500}); | 
					
						
							| 
									
										
										
										
											2020-03-26 21:40:38 -07:00
										 |  |  |       expect(await page.evaluate(() => window.screen.width)).toBe(500); | 
					
						
							| 
									
										
										
										
											2020-03-09 15:53:31 -07:00
										 |  |  |       expect(await page.evaluate(() => matchMedia('(min-device-width: 400px)').matches)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(min-device-width: 600px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(max-device-width: 400px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(max-device-width: 600px)').matches)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(device-width: 200px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(device-width: 500px)').matches)).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-26 21:40:38 -07:00
										 |  |  |     it('should emulate device height', async({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-09 15:53:31 -07:00
										 |  |  |       expect(page.viewportSize()).toEqual({width: 1280, height: 720}); | 
					
						
							|  |  |  |       await page.setViewportSize({width: 200, height: 200}); | 
					
						
							| 
									
										
										
										
											2020-03-26 21:40:38 -07:00
										 |  |  |       expect(await page.evaluate(() => window.screen.height)).toBe(200); | 
					
						
							| 
									
										
										
										
											2020-03-09 15:53:31 -07:00
										 |  |  |       expect(await page.evaluate(() => matchMedia('(min-device-height: 100px)').matches)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(min-device-height: 300px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(max-device-height: 100px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(max-device-height: 300px)').matches)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(device-height: 500px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(device-height: 200px)').matches)).toBe(true); | 
					
						
							|  |  |  |       await page.setViewportSize({width: 500, height: 500}); | 
					
						
							| 
									
										
										
										
											2020-03-26 21:40:38 -07:00
										 |  |  |       expect(await page.evaluate(() => window.screen.height)).toBe(500); | 
					
						
							| 
									
										
										
										
											2020-03-09 15:53:31 -07:00
										 |  |  |       expect(await page.evaluate(() => matchMedia('(min-device-height: 400px)').matches)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(min-device-height: 600px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(max-device-height: 400px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(max-device-height: 600px)').matches)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(device-height: 200px)').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(device-height: 500px)').matches)).toBe(true); | 
					
						
							| 
									
										
										
										
											2020-03-09 14:58:38 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-02 15:08:15 -08:00
										 |  |  |     it('should not have touch by default', async({page, server}) => { | 
					
						
							|  |  |  |       await page.goto(server.PREFIX + '/mobile.html'); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(false); | 
					
						
							|  |  |  |       await page.goto(server.PREFIX + '/detect-touch.html'); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => document.body.textContent.trim())).toBe('NO'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe.skip(FFOX)('viewport.isMobile', () => { | 
					
						
							|  |  |  |     // Firefox does not support isMobile.
 | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |     it('should support mobile emulation', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 16:04:42 -07:00
										 |  |  |       const context = await browser.newContext({ ...iPhone }); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       await page.goto(server.PREFIX + '/mobile.html'); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => window.innerWidth)).toBe(375); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       await page.setViewportSize({width: 400, height: 300}); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       expect(await page.evaluate(() => window.innerWidth)).toBe(400); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |     it('should support touch emulation', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 16:04:42 -07:00
										 |  |  |       const context = await browser.newContext({ ...iPhone }); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       const page = await context.newPage(); | 
					
						
							|  |  |  |       await page.goto(server.PREFIX + '/mobile.html'); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(dispatchTouch)).toBe('Received touch'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       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-02-20 15:15:48 -08:00
										 |  |  |     it('should be detectable by Modernizr', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 16:04:42 -07:00
										 |  |  |       const context = await browser.newContext({ ...iPhone }); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       await page.goto(server.PREFIX + '/detect-touch.html'); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => document.body.textContent.trim())).toBe('YES'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |     it('should detect touch when applying viewport with touches', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 18:21:02 -07:00
										 |  |  |       const context = await browser.newContext({ viewport: { width: 800, height: 600 }, hasTouch: true }); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       const page = await context.newPage(); | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       await page.addScriptTag({url: server.PREFIX + '/modernizr.js'}); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => Modernizr.touchevents)).toBe(true); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-02 15:08:15 -08:00
										 |  |  |     it('should support landscape emulation', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 16:04:42 -07:00
										 |  |  |       const context1 = await browser.newContext({ ...iPhone }); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       const page1 = await context1.newPage(); | 
					
						
							|  |  |  |       await page1.goto(server.PREFIX + '/mobile.html'); | 
					
						
							|  |  |  |       expect(await page1.evaluate(() => matchMedia('(orientation: landscape)').matches)).toBe(false); | 
					
						
							| 
									
										
										
										
											2020-03-17 16:04:42 -07:00
										 |  |  |       const context2 = await browser.newContext({ ...iPhoneLandscape }); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       const page2 = await context2.newPage(); | 
					
						
							|  |  |  |       expect(await page2.evaluate(() => matchMedia('(orientation: landscape)').matches)).toBe(true); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context1.close(); | 
					
						
							|  |  |  |       await context2.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-02 15:08:15 -08:00
										 |  |  |     it.fail(WEBKIT)('should fire orientationchange event', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 18:21:02 -07:00
										 |  |  |       const context = await browser.newContext({ viewport: { width: 300, height: 400 }, isMobile: true }); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-03 11:01:13 -08:00
										 |  |  |       await page.goto(server.PREFIX + '/mobile.html'); | 
					
						
							|  |  |  |       await page.evaluate(() => { | 
					
						
							|  |  |  |         window.counter = 0; | 
					
						
							|  |  |  |         window.addEventListener('orientationchange', () => console.log(++window.counter)); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const event1 = page.waitForEvent('console'); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       await page.setViewportSize({width: 400, height: 300}); | 
					
						
							| 
									
										
										
										
											2020-01-03 11:01:13 -08:00
										 |  |  |       expect((await event1).text()).toBe('1'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const event2 = page.waitForEvent('console'); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       await page.setViewportSize({width: 300, height: 400}); | 
					
						
							| 
									
										
										
										
											2020-01-03 11:01:13 -08:00
										 |  |  |       expect((await event2).text()).toBe('2'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2020-01-03 11:01:13 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-02 15:08:15 -08:00
										 |  |  |     it('default mobile viewports to 980 width', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 18:21:02 -07:00
										 |  |  |       const context = await browser.newContext({ viewport: {width: 320, height: 480 }, isMobile: true }); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-08 20:15:30 -08:00
										 |  |  |       await page.goto(server.PREFIX + '/empty.html'); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => window.innerWidth)).toBe(980); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2020-01-08 20:15:30 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-02 15:08:15 -08:00
										 |  |  |     it('respect meta viewport tag', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 18:21:02 -07:00
										 |  |  |       const context = await browser.newContext({ viewport: {width: 320, height: 480 }, isMobile: true }); | 
					
						
							| 
									
										
										
										
											2020-02-06 19:02:55 -08:00
										 |  |  |       const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-01-08 20:15:30 -08:00
										 |  |  |       await page.goto(server.PREFIX + '/mobile.html'); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => window.innerWidth)).toBe(320); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2020-01-08 20:15:30 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:08:15 -08:00
										 |  |  |   describe.skip(FFOX)('Page.emulate', function() { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |     it('should work', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 16:04:42 -07:00
										 |  |  |       const context = await browser.newContext({ ...iPhone }); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       await page.goto(server.PREFIX + '/mobile.html'); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => window.innerWidth)).toBe(375); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => navigator.userAgent)).toContain('iPhone'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |     it('should support clicking', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 16:04:42 -07:00
										 |  |  |       const context = await browser.newContext({ ...iPhone }); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       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-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 16:36:56 -08:00
										 |  |  |   describe('Page.emulateMedia type', function() { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     it('should work', async({page, server}) => { | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false); | 
					
						
							| 
									
										
										
										
											2020-01-05 14:39:16 -08:00
										 |  |  |       await page.emulateMedia({ media: 'print' }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true); | 
					
						
							| 
									
										
										
										
											2019-11-22 11:24:56 -08:00
										 |  |  |       await page.emulateMedia({}); | 
					
						
							| 
									
										
										
										
											2019-11-25 15:00:04 -08:00
										 |  |  |       expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true); | 
					
						
							| 
									
										
										
										
											2020-01-05 14:39:16 -08:00
										 |  |  |       await page.emulateMedia({ media: '' }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-22 11:24:56 -08:00
										 |  |  |     it('should throw in case of bad type argument', async({page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       let error = null; | 
					
						
							| 
									
										
										
										
											2020-01-05 14:39:16 -08:00
										 |  |  |       await page.emulateMedia({ media: 'bad' }).catch(e => error = e); | 
					
						
							|  |  |  |       expect(error.message).toBe('Unsupported media: bad'); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 17:34:02 -08:00
										 |  |  |   describe('Page.emulateMedia colorScheme', function() { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     it('should work', async({page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-25 15:00:04 -08:00
										 |  |  |       await page.emulateMedia({ colorScheme: 'light' }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       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); | 
					
						
							| 
									
										
										
										
											2019-11-25 15:00:04 -08:00
										 |  |  |       await page.emulateMedia({ colorScheme: 'dark' }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       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); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:34:02 -08:00
										 |  |  |       if (!WEBKIT) { | 
					
						
							|  |  |  |         // WebKit will always provide the value.
 | 
					
						
							|  |  |  |         await page.emulateMedia({ colorScheme: 'no-preference' }); | 
					
						
							|  |  |  |         expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches)).toBe(false); | 
					
						
							|  |  |  |         expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: light)').matches)).toBe(false); | 
					
						
							|  |  |  |         expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: no-preference)').matches)).toBe(true); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |     it('should throw in case of bad argument', async({page, server}) => { | 
					
						
							|  |  |  |       let error = null; | 
					
						
							| 
									
										
										
										
											2019-11-25 15:00:04 -08:00
										 |  |  |       await page.emulateMedia({ colorScheme: 'bad' }).catch(e => error = e); | 
					
						
							|  |  |  |       expect(error.message).toBe('Unsupported color scheme: bad'); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-22 22:16:39 -07:00
										 |  |  |     it('should work during navigation', async({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-01-10 15:53:48 -08:00
										 |  |  |       await page.emulateMedia({ colorScheme: 'light' }); | 
					
						
							|  |  |  |       const navigated = page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       for (let i = 0; i < 9; i++) { | 
					
						
							| 
									
										
										
										
											2020-02-12 16:27:45 -08:00
										 |  |  |         await Promise.all([ | 
					
						
							|  |  |  |           page.emulateMedia({ colorScheme: ['dark', 'light'][i & 1] }), | 
					
						
							|  |  |  |           new Promise(f => setTimeout(f, 1)), | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:53:48 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |       await navigated; | 
					
						
							|  |  |  |       expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches)).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 13:47:08 -08:00
										 |  |  |   describe.fail(FFOX)('BrowserContext({timezoneId})', function() { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |     it('should work', async ({ browser }) => { | 
					
						
							| 
									
										
										
										
											2019-12-18 12:23:33 -08:00
										 |  |  |       const func = () => new Date(1479579154987).toString(); | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         const context = await browser.newContext({ timezoneId: 'America/Jamaica' }); | 
					
						
							|  |  |  |         const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-12 21:25:17 -08:00
										 |  |  |         expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         await context.close(); | 
					
						
							| 
									
										
										
										
											2019-12-18 12:23:33 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         const context = await browser.newContext({ timezoneId: 'Pacific/Honolulu' }); | 
					
						
							|  |  |  |         const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-12 21:25:17 -08:00
										 |  |  |         expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 08:12:34 GMT-1000 (Hawaii-Aleutian Standard Time)'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         await context.close(); | 
					
						
							| 
									
										
										
										
											2019-12-18 12:23:33 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         const context = await browser.newContext({ timezoneId: 'America/Buenos_Aires' }); | 
					
						
							|  |  |  |         const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-12 21:25:17 -08:00
										 |  |  |         expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 15:12:34 GMT-0300 (Argentina Standard Time)'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         await context.close(); | 
					
						
							| 
									
										
										
										
											2019-12-18 12:23:33 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         const context = await browser.newContext({ timezoneId: 'Europe/Berlin' }); | 
					
						
							|  |  |  |         const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-12 21:25:17 -08:00
										 |  |  |         expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 19:12:34 GMT+0100 (Central European Standard Time)'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         await context.close(); | 
					
						
							| 
									
										
										
										
											2019-12-18 12:23:33 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |     it('should throw for invalid timezone IDs when creating pages', async({browser}) => { | 
					
						
							|  |  |  |       for (const timezoneId of ['Foo/Bar', 'Baz/Qux']) { | 
					
						
							|  |  |  |         let error = null; | 
					
						
							|  |  |  |         const context = await browser.newContext({ timezoneId }); | 
					
						
							|  |  |  |         const page = await context.newPage().catch(e => error = e); | 
					
						
							|  |  |  |         expect(error.message).toBe(`Invalid timezone ID: ${timezoneId}`); | 
					
						
							|  |  |  |         await context.close(); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-23 17:23:47 -07:00
										 |  |  |     it('should work for multiple pages sharing same process', async({browser, server}) => { | 
					
						
							|  |  |  |       const context = await browser.newContext({ timezoneId: 'Europe/Moscow' }); | 
					
						
							|  |  |  |       const page = await context.newPage(); | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       let [popup] = await Promise.all([ | 
					
						
							|  |  |  |         page.waitForEvent('popup'), | 
					
						
							|  |  |  |         page.evaluate(url => { window.open(url); }, server.EMPTY_PAGE), | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  |       [popup] = await Promise.all([ | 
					
						
							|  |  |  |         popup.waitForEvent('popup'), | 
					
						
							|  |  |  |         popup.evaluate(url => { window.open(url); }, server.EMPTY_PAGE), | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  |       await context.close(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 19:32:27 -07:00
										 |  |  |   describe('BrowserContext({locale})', function() { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |     it('should affect accept-language header', async({browser, server}) => { | 
					
						
							|  |  |  |       const context = await browser.newContext({ locale: 'fr-CH' }); | 
					
						
							|  |  |  |       const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |       const [request] = await Promise.all([ | 
					
						
							|  |  |  |         server.waitForRequest('/empty.html'), | 
					
						
							|  |  |  |         page.goto(server.EMPTY_PAGE), | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2020-02-13 15:12:54 -08:00
										 |  |  |       expect(request.headers['accept-language'].substr(0, 5)).toBe('fr-CH'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2020-02-13 15:12:54 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |     it('should affect navigator.language', async({browser, server}) => { | 
					
						
							|  |  |  |       const context = await browser.newContext({ locale: 'fr-CH' }); | 
					
						
							|  |  |  |       const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |       expect(await page.evaluate(() => navigator.language)).toBe('fr-CH'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await context.close(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-26 18:14:38 -07:00
										 |  |  |     it('should format number', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         const context = await browser.newContext({ locale: 'en-US' }); | 
					
						
							|  |  |  |         const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |         await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |         expect(await page.evaluate(() => (1000000.50).toLocaleString())).toBe('1,000,000.5'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         await context.close(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         const context = await browser.newContext({ locale: 'fr-CH' }); | 
					
						
							|  |  |  |         const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |         await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-02-13 17:03:35 -08:00
										 |  |  |         expect(await page.evaluate(() => (1000000.50).toLocaleString().replace(/\s/g, ' '))).toBe('1 000 000,5'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         await context.close(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:32:27 -07:00
										 |  |  |     it.fail(FFOX)('should format date', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         const context = await browser.newContext({ locale: 'en-US', timezoneId: 'America/Los_Angeles' }); | 
					
						
							|  |  |  |         const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |         await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-02-13 15:32:39 -08:00
										 |  |  |         const formatted = 'Sat Nov 19 2016 10:12:34 GMT-0800 (Pacific Standard Time)'; | 
					
						
							| 
									
										
										
										
											2020-02-13 15:12:54 -08:00
										 |  |  |         expect(await page.evaluate(() => new Date(1479579154987).toString())).toBe(formatted); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         await context.close(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         const context = await browser.newContext({ locale: 'de-DE', timezoneId: 'Europe/Berlin' }); | 
					
						
							|  |  |  |         const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |         await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |         expect(await page.evaluate(() => new Date(1479579154987).toString())).toBe( | 
					
						
							|  |  |  |             'Sat Nov 19 2016 19:12:34 GMT+0100 (Mitteleuropäische Normalzeit)'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |         await context.close(); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-26 18:14:38 -07:00
										 |  |  |     it('should format number in popups', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-20 19:32:27 -07:00
										 |  |  |       const context = await browser.newContext({ locale: 'fr-CH' }); | 
					
						
							|  |  |  |       const page = await context.newPage(); | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const [popup] = await Promise.all([ | 
					
						
							|  |  |  |         page.waitForEvent('popup'), | 
					
						
							|  |  |  |         page.evaluate(url => window._popup = window.open(url), server.PREFIX + '/formatted-number.html'), | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2020-03-23 13:51:11 -07:00
										 |  |  |       await popup.waitForLoadState('domcontentloaded'); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:32:27 -07:00
										 |  |  |       const result = await popup.evaluate(() => window.result); | 
					
						
							|  |  |  |       expect(result).toBe('1 000 000,5'); | 
					
						
							|  |  |  |       await context.close(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-23 13:50:04 -07:00
										 |  |  |     it('should affect navigator.language in popups', async({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-03-22 08:56:50 -07:00
										 |  |  |       const context = await browser.newContext({ locale: 'fr-CH' }); | 
					
						
							|  |  |  |       const page = await context.newPage(); | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       const [popup] = await Promise.all([ | 
					
						
							|  |  |  |         page.waitForEvent('popup'), | 
					
						
							|  |  |  |         page.evaluate(url => window._popup = window.open(url), server.PREFIX + '/formatted-number.html'), | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2020-03-23 13:51:11 -07:00
										 |  |  |       await popup.waitForLoadState('domcontentloaded'); | 
					
						
							| 
									
										
										
										
											2020-03-22 08:56:50 -07:00
										 |  |  |       const result = await popup.evaluate(() => window.initialNavigatorLanguage); | 
					
						
							|  |  |  |       expect(result).toBe('fr-CH'); | 
					
						
							|  |  |  |       await context.close(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-23 17:23:47 -07:00
										 |  |  |     it('should work for multiple pages sharing same process', async({browser, server}) => { | 
					
						
							|  |  |  |       const context = await browser.newContext({ locale: 'ru-RU' }); | 
					
						
							|  |  |  |       const page = await context.newPage(); | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       let [popup] = await Promise.all([ | 
					
						
							|  |  |  |         page.waitForEvent('popup'), | 
					
						
							|  |  |  |         page.evaluate(url => { window.open(url); }, server.EMPTY_PAGE), | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  |       [popup] = await Promise.all([ | 
					
						
							|  |  |  |         popup.waitForEvent('popup'), | 
					
						
							|  |  |  |         popup.evaluate(url => { window.open(url); }, server.EMPTY_PAGE), | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  |       await context.close(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-13 12:24:17 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 16:13:11 -07:00
										 |  |  |   describe.fail(!headless)('focus', function() { | 
					
						
							|  |  |  |     it('should think that it is focused by default', async({page}) => { | 
					
						
							| 
									
										
										
										
											2020-02-11 19:10:57 -08:00
										 |  |  |       expect(await page.evaluate('document.hasFocus()')).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-26 16:13:11 -07:00
										 |  |  |     it.fail(FFOX)('should think that all pages are focused', async({page}) => { | 
					
						
							|  |  |  |       const page2 = await page.context().newPage(); | 
					
						
							|  |  |  |       expect(await page.evaluate('document.hasFocus()')).toBe(true); | 
					
						
							|  |  |  |       expect(await page2.evaluate('document.hasFocus()')).toBe(true); | 
					
						
							|  |  |  |       await page2.close(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-11 19:10:57 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | }; |