| 
									
										
										
										
											2020-08-03 15:23:53 -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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-08-19 21:32:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 07:08:22 -07:00
										 |  |  | import { test as it, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2021-04-02 14:23:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 19:42:52 +01:00
										 |  |  | it('should work @smoke', async ({ page, server, isAndroid }) => { | 
					
						
							| 
									
										
										
										
											2021-04-09 07:59:09 -07:00
										 |  |  |   it.fixme(isAndroid); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/offscreenbuttons.html'); | 
					
						
							|  |  |  |   for (let i = 0; i < 11; ++i) { | 
					
						
							|  |  |  |     const button = await page.$('#btn' + i); | 
					
						
							|  |  |  |     const before = await button.evaluate(button => { | 
					
						
							|  |  |  |       return button.getBoundingClientRect().right - window.innerWidth; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     expect(before).toBe(10 * i); | 
					
						
							|  |  |  |     await button.scrollIntoViewIfNeeded(); | 
					
						
							|  |  |  |     const after = await button.evaluate(button => { | 
					
						
							|  |  |  |       return button.getBoundingClientRect().right - window.innerWidth; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     expect(after <= 0).toBe(true); | 
					
						
							|  |  |  |     await page.evaluate(() => window.scrollTo(0, 0)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should throw for detached element', async ({ page, server }) => { | 
					
						
							|  |  |  |   await page.setContent('<div>Hello</div>'); | 
					
						
							|  |  |  |   const div = await page.$('div'); | 
					
						
							|  |  |  |   await div.evaluate(div => div.remove()); | 
					
						
							|  |  |  |   const error = await div.scrollIntoViewIfNeeded().catch(e => e); | 
					
						
							|  |  |  |   expect(error.message).toContain('Element is not attached to the DOM'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function testWaiting(page, after) { | 
					
						
							|  |  |  |   const div = await page.$('div'); | 
					
						
							|  |  |  |   let done = false; | 
					
						
							|  |  |  |   const promise = div.scrollIntoViewIfNeeded().then(() => done = true); | 
					
						
							| 
									
										
										
										
											2024-05-31 14:44:26 -07:00
										 |  |  |   await page.waitForTimeout(1000); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   expect(done).toBe(false); | 
					
						
							|  |  |  |   await div.evaluate(after); | 
					
						
							|  |  |  |   await promise; | 
					
						
							|  |  |  |   expect(done).toBe(true); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-04-23 21:48:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should wait for display:none to become visible', async ({ page, server }) => { | 
					
						
							|  |  |  |   await page.setContent('<div style="display:none">Hello</div>'); | 
					
						
							|  |  |  |   await testWaiting(page, div => div.style.display = 'block'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 16:05:09 -07:00
										 |  |  | it('should scroll display:contents into view', async ({ page, browserName, browserMajorVersion }) => { | 
					
						
							|  |  |  |   it.skip(browserName === 'chromium' && browserMajorVersion < 105, 'Needs https://chromium-review.googlesource.com/c/chromium/src/+/3758670'); | 
					
						
							| 
									
										
										
										
											2022-07-18 13:36:45 -07:00
										 |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/15034' }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.setContent(`
 | 
					
						
							| 
									
										
										
										
											2022-08-08 16:05:09 -07:00
										 |  |  |     <style> | 
					
						
							|  |  |  |       html, body { margin: 0; padding: 0; width: 100%; height: 100%; } | 
					
						
							|  |  |  |       ::-webkit-scrollbar { display: none; } | 
					
						
							|  |  |  |       * { scrollbar-width: none; } | 
					
						
							|  |  |  |     </style> | 
					
						
							| 
									
										
										
										
											2022-07-18 13:36:45 -07:00
										 |  |  |     <div id=container style="width:200px;height:200px;overflow:scroll;border:1px solid black;"> | 
					
						
							|  |  |  |       <div style="margin-top:500px;background:red;"> | 
					
						
							|  |  |  |         <div style="height:50px;width:100px;background:cyan;"> | 
					
						
							|  |  |  |           <div id=target style="display:contents">Hello</div> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       <div> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   `);
 | 
					
						
							|  |  |  |   const div = await page.$('#target'); | 
					
						
							|  |  |  |   await div.scrollIntoViewIfNeeded(); | 
					
						
							| 
									
										
										
										
											2024-08-05 08:29:13 -07:00
										 |  |  |   const scrollTop = await page.$eval('#container', e => e.scrollTop); | 
					
						
							|  |  |  |   // On Android the value is not exact due to various scale conversions.
 | 
					
						
							|  |  |  |   expect(Math.abs(scrollTop - 350)).toBeLessThan(1); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-23 21:48:36 +01:00
										 |  |  | it('should work for visibility:hidden element', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.setContent('<div style="visibility:hidden">Hello</div>'); | 
					
						
							| 
									
										
										
										
											2022-04-23 21:48:36 +01:00
										 |  |  |   const div = await page.$('div'); | 
					
						
							|  |  |  |   await div.scrollIntoViewIfNeeded(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-23 21:48:36 +01:00
										 |  |  | it('should work for zero-sized element', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.setContent('<div style="height:0">Hello</div>'); | 
					
						
							| 
									
										
										
										
											2022-04-23 21:48:36 +01:00
										 |  |  |   const div = await page.$('div'); | 
					
						
							|  |  |  |   await div.scrollIntoViewIfNeeded(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should wait for nested display:none to become visible', async ({ page, server }) => { | 
					
						
							|  |  |  |   await page.setContent('<span style="display:none"><div>Hello</div></span>'); | 
					
						
							|  |  |  |   await testWaiting(page, div => div.parentElement.style.display = 'block'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | it('should wait for element to stop moving', async ({ page, server }) => { | 
					
						
							|  |  |  |   await page.setContent(`
 | 
					
						
							|  |  |  |     <style> | 
					
						
							|  |  |  |       @keyframes move { | 
					
						
							|  |  |  |         from { margin-left: 0; } | 
					
						
							|  |  |  |         to { margin-left: 200px; } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       div.animated { | 
					
						
							|  |  |  |         animation: 2s linear 0s infinite alternate move; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     </style> | 
					
						
							|  |  |  |     <div class=animated>moving</div> | 
					
						
							|  |  |  |     `);
 | 
					
						
							|  |  |  |   await testWaiting(page, div => div.classList.remove('animated')); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | it('should timeout waiting for visible', async ({ page, server }) => { | 
					
						
							|  |  |  |   await page.setContent('<div style="display:none">Hello</div>'); | 
					
						
							|  |  |  |   const div = await page.$('div'); | 
					
						
							|  |  |  |   const error = await div.scrollIntoViewIfNeeded({ timeout: 3000 }).catch(e => e); | 
					
						
							| 
									
										
										
										
											2024-01-16 19:11:41 -08:00
										 |  |  |   expect(error.message).toContain('element is not visible'); | 
					
						
							|  |  |  |   expect(error.message).toContain('retrying scroll into view action'); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | }); |