Skip to content
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

refactor(tests): extract pollForSelector function #54

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/tests/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const maxRetries = 60;
export const sleepTime = 4000;
export const singleActionTimeout = 5000;
export const pollingTimeoutInSeconds = 30;
export const youtubeLandingPage = "https://www.youtube.com/";
export const youtubeTestVideoPage =
"https://www.youtube.com/watch?v=Czvldzei4DI";
Expand Down
58 changes: 35 additions & 23 deletions src/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
languageIconPathSelector,
maxRetries,
menuIconPathSelector,
pollingTimeoutInSeconds,
searchIconPathSelector,
singleActionTimeout,
sleepTime,
Expand All @@ -22,6 +23,31 @@ import { sleep } from "../utils/other.js";
* @typedef {import("@playwright/test").Page} Page
*/

/**
* @param {Page} page
* @param {string[]} selectors
* @returns {Promise<string?>}
*/
const pollForSelector = async (page, selectors) => {
const pollsPerSecond = 1000 / singleActionTimeout;
const numberOfPolls = pollsPerSecond * pollingTimeoutInSeconds;

for (let i = 0; i < numberOfPolls; i++) {
let selector = selectors[i % selectors.length];
let element = page.locator(selector);
try {
await expect(element).toBeVisible({ timeout: singleActionTimeout });
return selector;
} catch (error) {
if (error instanceof errors.TimeoutError)
console.warn(`Element not found with selector ${selector}.`);
}
}

console.error("Could not find any elements with selectors:", selectors);
return null;
};

/**
* @param {() => Promise<void>} callback
* @param {() => boolean} condition
Expand Down Expand Up @@ -114,30 +140,16 @@ export const clickOnAVideo = async (page) => {
* @param {Page} page
*/
const clickShareButton = async (page) => {
for (;;) {
let shareButton = page.locator(shareButtonSelector);
try {
await shareButton.click({ timeout: singleActionTimeout });
return;
} catch (error) {
if (error instanceof errors.TimeoutError) {
console.warn(
`Share button not found with selector ${shareButtonSelector}.`,
);
}
}
shareButton = page.locator(shareButtonSelector2);
try {
await shareButton.click({ timeout: singleActionTimeout });
return;
} catch (error) {
if (error instanceof errors.TimeoutError) {
console.warn(
`Share button not found with selector ${shareButtonSelector2}.`,
);
}
}
let selector = await pollForSelector(page, [
shareButtonSelector,
shareButtonSelector2,
]);
if (!selector) {
console.error("Could not find share button");
return;
}
let shareButton = page.locator(selector);
await shareButton.click();
};

/**
Expand Down