chore: fixes to toBeInViewport (#20870)

This commit is contained in:
Andrey Lushnikov 2023-02-13 15:21:40 -08:00 committed by GitHub
parent 94b859f471
commit 72942e81d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 13 deletions

View file

@ -694,7 +694,7 @@ const locator = page.locator('button.submit');
await expect(locator).toBeInViewport(); await expect(locator).toBeInViewport();
// Make sure element is fully outside of viewport. // Make sure element is fully outside of viewport.
await expect(locator).not.toBeInViewport(); await expect(locator).not.toBeInViewport();
// Make sure strictly more than half of the element intersects viewport. // Make sure that at least half of the element intersects viewport.
await expect(locator).toBeInViewport({ ratio: 0.5 }); await expect(locator).toBeInViewport({ ratio: 0.5 });
``` ```
@ -704,7 +704,7 @@ Locator locator = page.locator("button.submit");
assertThat(locator).isInViewport(); assertThat(locator).isInViewport();
// Make sure element is fully outside of viewport. // Make sure element is fully outside of viewport.
assertThat(locator).not().isInViewport(); assertThat(locator).not().isInViewport();
// Make sure strictly more than half of the element intersects viewport. // Make sure that at least half of the element intersects viewport.
assertThat(locator).isInViewport(new LocatorAssertions.IsInViewportOptions().setRatio(0.5)); assertThat(locator).isInViewport(new LocatorAssertions.IsInViewportOptions().setRatio(0.5));
``` ```
@ -714,7 +714,7 @@ var locator = Page.Locator("button.submit");
await Expect(locator).ToBeInViewportAsync(); await Expect(locator).ToBeInViewportAsync();
// Make sure element is fully outside of viewport. // Make sure element is fully outside of viewport.
await Expect(locator).Not.ToBeInViewportAsync(); await Expect(locator).Not.ToBeInViewportAsync();
// Make sure strictly more than half of the element intersects viewport. // Make sure that at least half of the element intersects viewport.
await Expect(locator).ToBeInViewportAsync(new() { Ratio = 0.5 }); await Expect(locator).ToBeInViewportAsync(new() { Ratio = 0.5 });
``` ```
@ -726,7 +726,7 @@ locator = page.locator("button.submit")
await expect(locator).to_be_in_viewport() await expect(locator).to_be_in_viewport()
# Make sure element is fully outside of viewport. # Make sure element is fully outside of viewport.
await expect(locator).not_to_be_in_viewport() await expect(locator).not_to_be_in_viewport()
# Make sure strictly more than half of the element intersects viewport. # Make sure that at least half of the element intersects viewport.
await expect(locator).to_be_in_viewport(ratio=0.5); await expect(locator).to_be_in_viewport(ratio=0.5);
``` ```
@ -738,7 +738,7 @@ locator = page.locator("button.submit")
expect(locator).to_be_in_viewport() expect(locator).to_be_in_viewport()
# Make sure element is fully outside of viewport. # Make sure element is fully outside of viewport.
expect(locator).not_to_be_in_viewport() expect(locator).not_to_be_in_viewport()
# Make sure strictly more than half of the element intersects viewport. # Make sure that at least half of the element intersects viewport.
expect(locator).to_be_in_viewport(ratio=0.5); expect(locator).to_be_in_viewport(ratio=0.5);
``` ```
@ -747,8 +747,8 @@ expect(locator).to_be_in_viewport(ratio=0.5);
* since: v1.31 * since: v1.31
- `ratio` <[float]> - `ratio` <[float]>
The minimal ratio of the element to intersect viewport. Element's ratio should be strictly greater than The minimal ratio of the element to intersect viewport. If equals to `0`, then
this number. Defaults to `0`. element should intersect viewport at any positive ratio. Defaults to `0`.
### option: LocatorAssertions.toBeInViewport.timeout = %%-js-assertions-timeout-%% ### option: LocatorAssertions.toBeInViewport.timeout = %%-js-assertions-timeout-%%
* since: v1.31 * since: v1.31

View file

@ -1191,7 +1191,7 @@ export class InjectedScript {
// Viewport intersection // Viewport intersection
if (expression === 'to.be.in.viewport') { if (expression === 'to.be.in.viewport') {
const ratio = await this.viewportRatio(element); const ratio = await this.viewportRatio(element);
return { received: `viewport ratio ${ratio}`, matches: ratio > (options.viewportRatio ?? 0) }; return { received: `viewport ratio ${ratio}`, matches: ratio > 0 && ratio > (options.viewportRatio ?? 0) - 1e-9 };
} }
} }

View file

@ -4480,7 +4480,7 @@ interface LocatorAssertions {
* await expect(locator).toBeInViewport(); * await expect(locator).toBeInViewport();
* // Make sure element is fully outside of viewport. * // Make sure element is fully outside of viewport.
* await expect(locator).not.toBeInViewport(); * await expect(locator).not.toBeInViewport();
* // Make sure strictly more than half of the element intersects viewport. * // Make sure that at least half of the element intersects viewport.
* await expect(locator).toBeInViewport({ ratio: 0.5 }); * await expect(locator).toBeInViewport({ ratio: 0.5 });
* ``` * ```
* *
@ -4488,8 +4488,8 @@ interface LocatorAssertions {
*/ */
toBeInViewport(options?: { toBeInViewport(options?: {
/** /**
* The minimal ratio of the element to intersect viewport. Element's ratio should be strictly greater than this * The minimal ratio of the element to intersect viewport. If equals to `0`, then element should intersect viewport at
* number. Defaults to `0`. * any positive ratio. Defaults to `0`.
*/ */
ratio?: number; ratio?: number;

View file

@ -296,6 +296,7 @@ test.describe('toBeInViewport', () => {
await expect(page.locator('#small')).not.toBeInViewport(); await expect(page.locator('#small')).not.toBeInViewport();
await page.locator('#small').scrollIntoViewIfNeeded(); await page.locator('#small').scrollIntoViewIfNeeded();
await expect(page.locator('#small')).toBeInViewport(); await expect(page.locator('#small')).toBeInViewport();
await expect(page.locator('#small')).toBeInViewport({ ratio: 1 });
}); });
test('should respect ratio option', async ({ page }) => { test('should respect ratio option', async ({ page }) => {
@ -307,9 +308,10 @@ test.describe('toBeInViewport', () => {
await expect(page.locator('div')).toBeInViewport({ ratio: 0.1 }); await expect(page.locator('div')).toBeInViewport({ ratio: 0.1 });
await expect(page.locator('div')).toBeInViewport({ ratio: 0.2 }); await expect(page.locator('div')).toBeInViewport({ ratio: 0.2 });
// In this test, element's ratio is 0.25. Make sure `ratio` is compared strictly.
await expect(page.locator('div')).toBeInViewport({ ratio: 0.24 }); await expect(page.locator('div')).toBeInViewport({ ratio: 0.24 });
await expect(page.locator('div')).not.toBeInViewport({ ratio: 0.25 }); // In this test, element's ratio is 0.25.
await expect(page.locator('div')).toBeInViewport({ ratio: 0.25 });
await expect(page.locator('div')).not.toBeInViewport({ ratio: 0.26 });
await expect(page.locator('div')).not.toBeInViewport({ ratio: 0.3 }); await expect(page.locator('div')).not.toBeInViewport({ ratio: 0.3 });
await expect(page.locator('div')).not.toBeInViewport({ ratio: 0.7 }); await expect(page.locator('div')).not.toBeInViewport({ ratio: 0.7 });