Skip to content

Commit e8663ef

Browse files
committed
add isSelectorInViewport and isSelectorEntirelyInViewport tests; refactor their implementations to use Playwright
1 parent 4ebb3b1 commit e8663ef

File tree

6 files changed

+95
-21
lines changed

6 files changed

+95
-21
lines changed

autotests/pageObjects/pages/Search.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {MobilePage} from 'autotests/pageObjects';
2+
import {Input} from 'autotests/pageObjects/components';
23
import {Search as SearchRoute} from 'autotests/routes/pageRoutes';
34
import {setReadonlyProperty} from 'e2ed/utils';
45

@@ -16,6 +17,8 @@ export class Search extends MobilePage<CustomPageParams> {
1617
*/
1718
readonly mobileDevice = 'iphone' as const;
1819

20+
readonly searchInput: Input = new Input('q');
21+
1922
/**
2023
* The search query of the page.
2124
*/

autotests/tests/main/viewport.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {test} from 'autotests';
2+
import {Search} from 'autotests/pageObjects/pages';
3+
import {expect} from 'e2ed';
4+
import {navigateToPage, scroll} from 'e2ed/actions';
5+
import {isSelectorEntirelyInViewport, isSelectorInViewport} from 'e2ed/utils';
6+
7+
test(
8+
'isSelectorInViewport, isSelectorEntirelyInViewport and expect().toBeInViewport',
9+
{meta: {testId: '24'}, testIdleTimeout: 12_000, testTimeout: 20_000},
10+
async () => {
11+
const scrollValueForPartialInViewport = 50;
12+
const scrollValueForNotInViewport = 500;
13+
14+
const searchQuery = 'foo';
15+
const searchPage = await navigateToPage(Search, {searchQuery});
16+
17+
const searchInputSelector = searchPage.searchInput.input;
18+
19+
await expect(
20+
await isSelectorInViewport(searchInputSelector),
21+
'isSelectorInViewport: searchInput is in viewport after page load',
22+
).ok();
23+
24+
await expect(
25+
await isSelectorEntirelyInViewport(searchInputSelector),
26+
'isSelectorEntirelyInViewport: searchInput is entirely in viewport after page load',
27+
).ok();
28+
29+
await expect(
30+
searchInputSelector,
31+
'toBeInViewport: searchInput is in viewport after page load',
32+
).toBeInViewport();
33+
34+
await expect(
35+
searchInputSelector,
36+
'toBeInViewport with ratio 1: searchInput is entirely in viewport after page load',
37+
).toBeInViewport({ratio: 1});
38+
39+
await scroll(0, scrollValueForPartialInViewport);
40+
41+
await expect(
42+
await isSelectorInViewport(searchInputSelector),
43+
'isSelectorInViewport: searchInput is in viewport after smaill scroll',
44+
).ok();
45+
46+
await expect(
47+
await isSelectorEntirelyInViewport(searchInputSelector),
48+
'isSelectorEntirelyInViewport: searchInput is not entirely in viewport after small scroll',
49+
).notOk();
50+
51+
await expect(
52+
searchInputSelector,
53+
'toBeInViewport with ratio 0.1: searchInput is not entirely in viewport after small scroll',
54+
).toBeInViewport({ratio: 0.1});
55+
56+
await scroll(0, scrollValueForNotInViewport);
57+
58+
await expect(
59+
await isSelectorInViewport(searchInputSelector),
60+
'isSelectorInViewport: searchInput is not in viewport after big scroll',
61+
).notOk();
62+
63+
await expect(
64+
await isSelectorEntirelyInViewport(searchInputSelector),
65+
'isSelectorEntirelyInViewport: searchInput is not entirely in viewport after big scroll',
66+
).notOk();
67+
},
68+
);

bin/makeExecutable.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ IFS=$'\n\t'
44

55
files=("$@")
66

7-
for file in ${files[*]}; do
8-
sed -i '' '1s|^|#!/usr/bin/env node\'$'\n''\'$'\n''|' "$file" && chmod +x "$file"
7+
for file in "${files[@]}"; do
8+
printf '#!/usr/bin/env node\n\n' | cat - "$file" > "$file.tmp" && mv "$file.tmp" "$file" && chmod +x "$file"
99
done

src/utils/document/getDocumentUrl.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ let clientGetDocumentUrl: ClientFunction<[], Url> | undefined;
1313
*/
1414
export const getDocumentUrl = async (): Promise<Url> => {
1515
if (clientGetDocumentUrl === undefined) {
16-
clientGetDocumentUrl = createClientFunction<[], Url>(
17-
() => document.URL as Url,
18-
{
19-
name: 'getDocumentUrl',
20-
retries: 3,
21-
},
22-
);
16+
clientGetDocumentUrl = createClientFunction<[], Url>(() => document.URL as Url, {
17+
name: 'getDocumentUrl',
18+
});
2319
}
2420

2521
const url = await clientGetDocumentUrl();
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import type {Selector} from '../../types/internal';
22

3+
import {expect as playwrightExpect} from '@playwright/test';
4+
35
/**
46
* Returns `true`, if the selector is entirely in the viewport
57
* (all selector points are in the viewport), and `false` otherwise.
68
*/
79
export const isSelectorEntirelyInViewport = async (selector: Selector): Promise<boolean> => {
8-
const htmlElementSelector = selector.createSelector('html');
9-
10-
const {height: clientHeight, width: clientWidth} = await htmlElementSelector.boundingClientRect;
11-
12-
const {bottom, left, right, top} = await selector.boundingClientRect;
10+
try {
11+
await playwrightExpect(selector.getPlaywrightLocator()).toBeInViewport({
12+
ratio: 1,
13+
timeout: 1,
14+
});
1315

14-
return top >= 0 && bottom <= clientHeight && left >= 0 && right <= clientWidth;
16+
return true;
17+
} catch {
18+
return false;
19+
}
1520
};
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import type {Selector} from '../../types/internal';
22

3+
import {expect as playwrightExpect} from '@playwright/test';
4+
35
/**
46
* Returns `true`, if the selector is in the viewport
57
* (intersects with the viewport at least in one point), and `false` otherwise.
68
*/
79
export const isSelectorInViewport = async (selector: Selector): Promise<boolean> => {
8-
const htmlElementSelector = selector.createSelector('html');
9-
10-
const {height: clientHeight, width: clientWidth} = await htmlElementSelector.boundingClientRect;
11-
12-
const {bottom, left, right, top} = await selector.boundingClientRect;
10+
try {
11+
await playwrightExpect(selector.getPlaywrightLocator()).toBeInViewport({timeout: 1});
1312

14-
return top < clientHeight && bottom > 0 && left < clientWidth && right > 0;
13+
return true;
14+
} catch {
15+
return false;
16+
}
1517
};

0 commit comments

Comments
 (0)