| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 07:05:50 +08:00
										 |  |  | import { browserTest as it, expect } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2023-04-27 08:22:01 +08:00
										 |  |  | import { attachFrame } from '../config/utils'; | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('should not be visible in context.pages', async ({ contextFactory }) => { | 
					
						
							| 
									
										
										
										
											2021-04-05 10:32:14 +08:00
										 |  |  |   const context = await contextFactory(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  |   expect(context.pages()).toContain(page); | 
					
						
							|  |  |  |   await page.close(); | 
					
						
							|  |  |  |   expect(context.pages()).not.toContain(page); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('page.context should return the correct instance', async function({ contextFactory }) { | 
					
						
							| 
									
										
										
										
											2021-04-05 10:32:14 +08:00
										 |  |  |   const context = await contextFactory(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  |   expect(page.context()).toBe(context); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 10:32:14 +08:00
										 |  |  | it('frame.focus should work multiple times', async ({ contextFactory }) => { | 
					
						
							|  |  |  |   const context = await contextFactory(); | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  |   const page1 = await context.newPage(); | 
					
						
							|  |  |  |   const page2 = await context.newPage(); | 
					
						
							|  |  |  |   for (const page of [page1, page2]) { | 
					
						
							|  |  |  |     await page.setContent(`<button id="foo" onfocus="window.gotFocus=true"></button>`); | 
					
						
							|  |  |  |     await page.focus('#foo'); | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |     expect(await page.evaluate(() => !!(window as any)['gotFocus'])).toBe(true); | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('should click with disabled javascript', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  |   const context = await browser.newContext({ javaScriptEnabled: false }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/wrappedlink.html'); | 
					
						
							|  |  |  |   await Promise.all([ | 
					
						
							|  |  |  |     page.click('a'), | 
					
						
							|  |  |  |     page.waitForNavigation() | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(page.url()).toBe(server.PREFIX + '/wrappedlink.html#clicked'); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('should not hang with touch-enabled viewports', async ({ browser, playwright }) => { | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  |   // @see https://github.com/GoogleChrome/puppeteer/issues/161
 | 
					
						
							|  |  |  |   const { viewport, hasTouch } = playwright.devices['iPhone 6']; | 
					
						
							|  |  |  |   const context = await browser.newContext({ viewport, hasTouch }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.mouse.down(); | 
					
						
							|  |  |  |   await page.mouse.move(100, 10); | 
					
						
							|  |  |  |   await page.mouse.up(); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('should click the button with deviceScaleFactor set', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  |   const context = await browser.newContext({ viewport: { width: 400, height: 400 }, deviceScaleFactor: 5 }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window.devicePixelRatio)).toBe(5); | 
					
						
							|  |  |  |   await page.setContent('<div style="width:100px;height:100px">spacer</div>'); | 
					
						
							|  |  |  |   await attachFrame(page, 'button-test', server.PREFIX + '/input/button.html'); | 
					
						
							|  |  |  |   const frame = page.frames()[1]; | 
					
						
							|  |  |  |   const button = await frame.$('button'); | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |   await button!.click(); | 
					
						
							|  |  |  |   expect(await frame.evaluate(() => (window as any)['result'])).toBe('Clicked'); | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-27 08:22:01 +08:00
										 |  |  | it('should click the button with offset with page scale', async ({ browser, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-04-05 10:32:14 +08:00
										 |  |  |   it.skip(browserName === 'firefox'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  |   const context = await browser.newContext({ viewport: { width: 400, height: 400 }, isMobile: true }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/button.html'); | 
					
						
							|  |  |  |   await page.$eval('button', button => { | 
					
						
							|  |  |  |     button.style.borderWidth = '8px'; | 
					
						
							|  |  |  |     document.body.style.margin = '0'; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.click('button', { position: { x: 20, y: 10 } }); | 
					
						
							|  |  |  |   expect(await page.evaluate('result')).toBe('Clicked'); | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |   const expectCloseTo = (expected: number, actual: number) => { | 
					
						
							| 
									
										
										
										
											2023-04-27 08:22:01 +08:00
										 |  |  |     if (Math.abs(expected - actual) > 2) | 
					
						
							|  |  |  |       throw new Error(`Expected: ${expected}, received: ${actual}`); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   // Expect 20;10 + 8px of border in each direction. Allow some delta as different
 | 
					
						
							|  |  |  |   // browsers round up or down differently during css -> dip -> css conversion.
 | 
					
						
							|  |  |  |   expectCloseTo(28, await page.evaluate('pageX')); | 
					
						
							|  |  |  |   expectCloseTo(18, await page.evaluate('pageY')); | 
					
						
							| 
									
										
										
										
											2021-03-19 07:20:35 +08:00
										 |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 02:07:56 +08:00
										 |  |  | it('should return bounding box with page scale', async ({ browser, server, browserName }) => { | 
					
						
							|  |  |  |   it.skip(browserName === 'firefox'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const context = await browser.newContext({ viewport: { width: 400, height: 400 }, isMobile: true }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/button.html'); | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |   const button = (await page.$('button'))!; | 
					
						
							| 
									
										
										
										
											2021-05-11 02:07:56 +08:00
										 |  |  |   await button.evaluate(button => { | 
					
						
							|  |  |  |     document.body.style.margin = '0'; | 
					
						
							|  |  |  |     button.style.borderWidth = '0'; | 
					
						
							|  |  |  |     button.style.width = '200px'; | 
					
						
							|  |  |  |     button.style.height = '20px'; | 
					
						
							|  |  |  |     button.style.marginLeft = '17px'; | 
					
						
							|  |  |  |     button.style.marginTop = '23px'; | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |   const box = (await button.boundingBox())!; | 
					
						
							| 
									
										
										
										
											2021-05-11 02:07:56 +08:00
										 |  |  |   expect(Math.round(box.x * 100)).toBe(17 * 100); | 
					
						
							|  |  |  |   expect(Math.round(box.y * 100)).toBe(23 * 100); | 
					
						
							|  |  |  |   expect(Math.round(box.width * 100)).toBe(200 * 100); | 
					
						
							|  |  |  |   expect(Math.round(box.height * 100)).toBe(20 * 100); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('should not leak listeners during navigation of 20 pages', async ({ contextFactory, server }) => { | 
					
						
							| 
									
										
										
										
											2021-05-21 00:51:09 +08:00
										 |  |  |   it.slow(true, 'We open 20 pages here!'); | 
					
						
							| 
									
										
										
										
											2021-05-08 06:25:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 10:10:28 +08:00
										 |  |  |   const context = await contextFactory(); | 
					
						
							|  |  |  |   let warning = null; | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |   const warningHandler = (w: string) => warning = w; | 
					
						
							| 
									
										
										
										
											2021-05-06 10:10:28 +08:00
										 |  |  |   process.on('warning', warningHandler); | 
					
						
							|  |  |  |   const pages = await Promise.all([...Array(20)].map(() => context.newPage())); | 
					
						
							|  |  |  |   await Promise.all(pages.map(page => page.goto(server.EMPTY_PAGE))); | 
					
						
							|  |  |  |   await Promise.all(pages.map(page => page.close())); | 
					
						
							|  |  |  |   process.off('warning', warningHandler); | 
					
						
							|  |  |  |   expect(warning).toBe(null); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2023-10-13 03:52:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-17 00:47:05 +08:00
										 |  |  | it('should keep selection in multiple pages', async ({ context }) => { | 
					
						
							| 
									
										
										
										
											2023-10-13 03:52:06 +08:00
										 |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/27475' }); | 
					
						
							|  |  |  |   const page1 = await context.newPage(); | 
					
						
							|  |  |  |   const page2 = await context.newPage(); | 
					
						
							|  |  |  |   for (const page of [page1, page2]) { | 
					
						
							|  |  |  |     await page.setContent('<div id="container">lorem ipsum dolor sit amet</div>'); | 
					
						
							|  |  |  |     await page.evaluate(() => { | 
					
						
							|  |  |  |       const element = document.getElementById('container') as HTMLDivElement; | 
					
						
							|  |  |  |       const textNode = element.firstChild as Text; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-17 00:47:05 +08:00
										 |  |  |       const range = document.createRange(); | 
					
						
							| 
									
										
										
										
											2023-10-13 03:52:06 +08:00
										 |  |  |       range.setStart(textNode, 6); | 
					
						
							|  |  |  |       range.setEnd(textNode, 11); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-17 00:47:05 +08:00
										 |  |  |       const selection = document.getSelection(); | 
					
						
							| 
									
										
										
										
											2023-10-13 03:52:06 +08:00
										 |  |  |       selection?.removeAllRanges(); | 
					
						
							|  |  |  |       selection?.addRange(range); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-10-17 00:47:05 +08:00
										 |  |  |   // In WebKit, selection is updated via IPC, give it enough time to take effect.
 | 
					
						
							|  |  |  |   await new Promise(f => setTimeout(f, 1_000)); | 
					
						
							| 
									
										
										
										
											2023-10-13 03:52:06 +08:00
										 |  |  |   for (const page of [page1, page2]) { | 
					
						
							|  |  |  |     const range = await page.evaluate(() => { | 
					
						
							|  |  |  |       const selection = document.getSelection(); | 
					
						
							|  |  |  |       const range = selection?.getRangeAt(0); | 
					
						
							|  |  |  |       return { | 
					
						
							|  |  |  |         rangeCount: selection?.rangeCount, | 
					
						
							|  |  |  |         startOffset: range?.startOffset, | 
					
						
							|  |  |  |         endOffset: range?.endOffset, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     expect(range).toEqual({ rangeCount: 1, startOffset: 6, endOffset: 11 }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); |