Skip to content

fix: move to waitFor #3933

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
merged 2 commits into from
Oct 21, 2023
Merged
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
76 changes: 40 additions & 36 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -2365,10 +2365,11 @@ class Playwright extends Helper {
locator = new Locator(locator, 'css');

const context = await this._getContext();
const waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'attached' });
return waiter.catch((err) => {
throw new Error(`element (${locator.toString()}) still not present on page after ${waitTimeout / 1000} sec\n${err.message}`);
});
try {
await context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'attached' });
} catch (e) {
throw new Error(`element (${locator.toString()}) still not present on page after ${waitTimeout / 1000} sec\n${e.message}`);
}
}

/**
Expand All @@ -2380,10 +2381,10 @@ class Playwright extends Helper {
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
locator = new Locator(locator, 'css');
const context = await this._getContext();
let waiter;
let count = 0;

// we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented
let waiter;
if (this.frame) {
do {
waiter = await this.frame.locator(buildLocatorString(locator)).first().isVisible();
Expand All @@ -2393,13 +2394,13 @@ class Playwright extends Helper {
} while (count <= waitTimeout);

if (!waiter) throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec.`);
return;
}

waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'visible' });
return waiter.catch((err) => {
throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec\n${err.message}`);
});
try {
await context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'visible' });
} catch (e) {
throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec\n${e.message}`);
}
}

/**
Expand All @@ -2425,10 +2426,11 @@ class Playwright extends Helper {
return;
}

waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'hidden' });
return waiter.catch((err) => {
throw new Error(`element (${locator.toString()}) still visible after ${waitTimeout / 1000} sec\n${err.message}`);
});
try {
await context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'hidden' });
} catch (e) {
throw new Error(`element (${locator.toString()}) still visible after ${waitTimeout / 1000} sec\n${e.message}`);
}
}

/**
Expand All @@ -2438,7 +2440,6 @@ class Playwright extends Helper {
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
locator = new Locator(locator, 'css');
const context = await this._getContext();

let waiter;
let count = 0;

Expand All @@ -2455,7 +2456,7 @@ class Playwright extends Helper {
return;
}

return context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'hidden' }).catch((err) => {
return context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'hidden' }).catch((err) => {
throw new Error(`element (${locator.toString()}) still not hidden after ${waitTimeout / 1000} sec\n${err.message}`);
});
}
Expand Down Expand Up @@ -2522,7 +2523,12 @@ class Playwright extends Helper {
if (context) {
const locator = new Locator(context, 'css');
if (!locator.isXPath()) {
waiter = contextObject.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()} >> text=${text}`, { timeout: waitTimeout, state: 'visible' });
try {
await contextObject.locator(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()} >> text=${text}`).first().waitFor({ timeout: waitTimeout, state: 'visible' });
} catch (e) {
console.log(e);
throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec\n${e.message}`);
}
}

if (locator.isXPath()) {
Expand All @@ -2535,23 +2541,17 @@ class Playwright extends Helper {
}
} else {
// we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented
if (this.frame) {
let count = 0;
do {
waiter = await this.frame.locator(`:has-text('${text}')`).first().isVisible();
await this.wait(1);
count += 1000;
} while (count <= waitTimeout);

if (!waiter) throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec`);
return;
}
// eslint-disable-next-line no-lonely-if
const _contextObject = this.frame ? this.frame : contextObject;
let count = 0;
do {
waiter = await _contextObject.locator(`:has-text('${text}')`).first().isVisible();
await this.wait(1);
count += 1000;
} while (count <= waitTimeout);

waiter = contextObject.waitForFunction(text => document.body && document.body.innerText.indexOf(text) > -1, text, { timeout: waitTimeout });
if (!waiter) throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec`);
}
return waiter.catch((err) => {
throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec\n${err.message}`);
});
}

/**
Expand Down Expand Up @@ -2710,17 +2710,21 @@ class Playwright extends Helper {
let waiter;
const context = await this._getContext();
if (!locator.isXPath()) {
waiter = context.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`, { timeout: waitTimeout, state: 'detached' });
try {
await context.locator(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`).first().waitFor({ timeout: waitTimeout, state: 'detached' });
} catch (e) {
throw new Error(`element (${locator.toString()}) still on page after ${waitTimeout / 1000} sec\n${e.message}`);
}
} else {
const visibleFn = function ([locator, $XPath]) {
eval($XPath); // eslint-disable-line no-eval
return $XPath(null, locator).length === 0;
};
waiter = context.waitForFunction(visibleFn, [locator.value, $XPath.toString()], { timeout: waitTimeout });
return waiter.catch((err) => {
throw new Error(`element (${locator.toString()}) still on page after ${waitTimeout / 1000} sec\n${err.message}`);
});
}
return waiter.catch((err) => {
throw new Error(`element (${locator.toString()}) still on page after ${waitTimeout / 1000} sec\n${err.message}`);
});
}

async _waitForAction() {
Expand Down