-
Notifications
You must be signed in to change notification settings - Fork 0
isSelectorInViewport and isSelectorEntirelyInViewport #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+93
−15
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import {test} from 'autotests'; | ||
| import {Search} from 'autotests/pageObjects/pages'; | ||
| import {expect} from 'e2ed'; | ||
| import {navigateToPage, scroll} from 'e2ed/actions'; | ||
| import {isSelectorEntirelyInViewport, isSelectorInViewport} from 'e2ed/utils'; | ||
|
|
||
| test( | ||
| 'isSelectorInViewport, isSelectorEntirelyInViewport and expect().toBeInViewport', | ||
| {meta: {testId: '24'}}, | ||
| async () => { | ||
| const scrollValueForPartialInViewport = 50; | ||
| const scrollValueForNotInViewport = 500; | ||
|
|
||
| const searchQuery = 'foo'; | ||
| const searchPage = await navigateToPage(Search, {searchQuery}); | ||
|
|
||
| const searchInputSelector = searchPage.searchInput.input; | ||
|
|
||
| await expect( | ||
| isSelectorInViewport(searchInputSelector), | ||
| 'isSelectorInViewport: searchInput is in viewport after page load', | ||
| ).ok(); | ||
|
|
||
| await expect( | ||
| isSelectorEntirelyInViewport(searchInputSelector), | ||
| 'isSelectorEntirelyInViewport: searchInput is entirely in viewport after page load', | ||
| ).ok(); | ||
|
|
||
| await expect( | ||
| searchInputSelector, | ||
| 'toBeInViewport: searchInput is in viewport after page load', | ||
| ).toBeInViewport(); | ||
|
|
||
| await expect( | ||
| searchInputSelector, | ||
| 'toBeInViewport with ratio 1: searchInput is entirely in viewport after page load', | ||
| ).toBeInViewport({ratio: 1}); | ||
|
|
||
| await scroll(0, scrollValueForPartialInViewport); | ||
|
|
||
| await expect( | ||
| isSelectorInViewport(searchInputSelector), | ||
| 'isSelectorInViewport: searchInput is in viewport after smaill scroll', | ||
| ).ok(); | ||
|
|
||
| await expect( | ||
| isSelectorEntirelyInViewport(searchInputSelector), | ||
| 'isSelectorEntirelyInViewport: searchInput is not entirely in viewport after small scroll', | ||
| ).notOk(); | ||
|
|
||
| await expect( | ||
| searchInputSelector, | ||
| 'toBeInViewport with ratio 0.1: searchInput is not entirely in viewport after small scroll', | ||
| ).toBeInViewport({ratio: 0.1}); | ||
|
|
||
| await scroll(0, scrollValueForNotInViewport); | ||
|
|
||
| await expect( | ||
| isSelectorInViewport(searchInputSelector), | ||
| 'isSelectorInViewport: searchInput is not in viewport after big scroll', | ||
| ).notOk(); | ||
|
|
||
| await expect( | ||
| isSelectorEntirelyInViewport(searchInputSelector), | ||
| 'isSelectorEntirelyInViewport: searchInput is not entirely in viewport after big scroll', | ||
| ).notOk(); | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,20 @@ | ||
| import type {Selector} from '../../types/internal'; | ||
|
|
||
| import {expect as playwrightExpect} from '@playwright/test'; | ||
|
|
||
| /** | ||
| * Returns `true`, if the selector is entirely in the viewport | ||
| * (all selector points are in the viewport), and `false` otherwise. | ||
| */ | ||
| export const isSelectorEntirelyInViewport = async (selector: Selector): Promise<boolean> => { | ||
| const htmlElementSelector = selector.createSelector('html'); | ||
|
|
||
| const {height: clientHeight, width: clientWidth} = await htmlElementSelector.boundingClientRect; | ||
|
|
||
| const {bottom, left, right, top} = await selector.boundingClientRect; | ||
| try { | ||
| await playwrightExpect(selector.getPlaywrightLocator()).toBeInViewport({ | ||
| ratio: 1, | ||
| timeout: 1, | ||
| }); | ||
|
|
||
| return top >= 0 && bottom <= clientHeight && left >= 0 && right <= clientWidth; | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| import type {Selector} from '../../types/internal'; | ||
|
|
||
| import {expect as playwrightExpect} from '@playwright/test'; | ||
|
|
||
| /** | ||
| * Returns `true`, if the selector is in the viewport | ||
| * (intersects with the viewport at least in one point), and `false` otherwise. | ||
| */ | ||
| export const isSelectorInViewport = async (selector: Selector): Promise<boolean> => { | ||
| const htmlElementSelector = selector.createSelector('html'); | ||
|
|
||
| const {height: clientHeight, width: clientWidth} = await htmlElementSelector.boundingClientRect; | ||
|
|
||
| const {bottom, left, right, top} = await selector.boundingClientRect; | ||
| try { | ||
| await playwrightExpect(selector.getPlaywrightLocator()).toBeInViewport({timeout: 1}); | ||
|
|
||
| return top < clientHeight && bottom > 0 && left < clientWidth && right > 0; | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.