| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2018 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 07:08:22 -07:00
										 |  |  | import { test as it, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2021-04-04 19:32:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 19:42:52 +01:00
										 |  |  | it('should emulate type @smoke', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false); | 
					
						
							|  |  |  |   await page.emulateMedia({ media: 'print' }); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true); | 
					
						
							|  |  |  |   await page.emulateMedia({}); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true); | 
					
						
							|  |  |  |   await page.emulateMedia({ media: null }); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should throw in case of bad media argument', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   let error = null; | 
					
						
							| 
									
										
										
										
											2020-09-09 03:06:52 -07:00
										 |  |  |   // @ts-expect-error 'bad' is not a valid media type
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await page.emulateMedia({ media: 'bad' }).catch(e => error = e); | 
					
						
							| 
									
										
										
										
											2022-10-31 09:09:52 -07:00
										 |  |  |   expect(error.message).toContain('media: expected one of (screen|print|no-override)'); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 19:42:52 +01:00
										 |  |  | it('should emulate colorScheme should work @smoke', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   await page.emulateMedia({ colorScheme: '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); | 
					
						
							|  |  |  |   await page.emulateMedia({ colorScheme: '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); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should default to light', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07: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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.emulateMedia({ colorScheme: '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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.emulateMedia({ colorScheme: null }); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches)).toBe(false); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: light)').matches)).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should throw in case of bad colorScheme argument', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   let error = null; | 
					
						
							| 
									
										
										
										
											2020-09-09 03:06:52 -07:00
										 |  |  |   // @ts-expect-error 'bad' is not a valid media type
 | 
					
						
							|  |  |  |   await page.emulateMedia({ colorScheme: 'bad' }).catch(e => error = e); | 
					
						
							| 
									
										
										
										
											2022-10-31 09:09:52 -07:00
										 |  |  |   expect(error.message).toContain('colorScheme: expected one of (dark|light|no-preference|no-override)'); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work during navigation', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   await page.emulateMedia({ colorScheme: 'light' }); | 
					
						
							|  |  |  |   const navigated = page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   for (let i = 0; i < 9; i++) { | 
					
						
							|  |  |  |     await Promise.all([ | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |       page.emulateMedia({ colorScheme: (['dark', 'light'] as const)[i & 1] }), | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |       new Promise(f => setTimeout(f, 1)), | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   await navigated; | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches)).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should change the actual colors in css', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   await page.setContent(`
 | 
					
						
							|  |  |  |     <style> | 
					
						
							|  |  |  |       @media (prefers-color-scheme: dark) { | 
					
						
							|  |  |  |         div { | 
					
						
							|  |  |  |           background: black; | 
					
						
							|  |  |  |           color: white; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       @media (prefers-color-scheme: light) { | 
					
						
							|  |  |  |         div { | 
					
						
							|  |  |  |           background: white; | 
					
						
							|  |  |  |           color: black; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     </style> | 
					
						
							|  |  |  |     <div>Hello</div> | 
					
						
							|  |  |  |   `);
 | 
					
						
							|  |  |  |   function getBackgroundColor() { | 
					
						
							|  |  |  |     return page.$eval('div', div => window.getComputedStyle(div).backgroundColor); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   await page.emulateMedia({ colorScheme: 'light' }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   expect(await getBackgroundColor()).toBe('rgb(255, 255, 255)'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   await page.emulateMedia({ colorScheme: 'dark' }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   expect(await getBackgroundColor()).toBe('rgb(0, 0, 0)'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   await page.emulateMedia({ colorScheme: 'light' }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   expect(await getBackgroundColor()).toBe('rgb(255, 255, 255)'); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-05-22 01:56:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should emulate reduced motion', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2021-05-22 01:56:09 +02:00
										 |  |  |   expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: no-preference)').matches)).toBe(true); | 
					
						
							|  |  |  |   await page.emulateMedia({ reducedMotion: '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.emulateMedia({ reducedMotion: 'no-preference' }); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: reduce)').matches)).toBe(false); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: no-preference)').matches)).toBe(true); | 
					
						
							|  |  |  |   await page.emulateMedia({ reducedMotion: null }); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-09-03 21:48:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-28 15:41:32 +02:00
										 |  |  | it('should emulate forcedColors ', async ({ page, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-09-03 21:48:06 +02:00
										 |  |  |   expect(await page.evaluate(() => matchMedia('(forced-colors: none)').matches)).toBe(true); | 
					
						
							|  |  |  |   await page.emulateMedia({ forcedColors: 'none' }); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('(forced-colors: none)').matches)).toBe(true); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('(forced-colors: active)').matches)).toBe(false); | 
					
						
							|  |  |  |   await page.emulateMedia({ forcedColors: 'active' }); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('(forced-colors: none)').matches)).toBe(false); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('(forced-colors: active)').matches)).toBe(true); | 
					
						
							|  |  |  |   await page.emulateMedia({ forcedColors: null }); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => matchMedia('(forced-colors: none)').matches)).toBe(true); | 
					
						
							|  |  |  | }); |