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

fix: predicate arguments for waitForFunction #7845

Merged
merged 2 commits into from Dec 15, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/common/DOMWorld.ts
Expand Up @@ -645,6 +645,7 @@ export class DOMWorld {
const waitTaskOptions: WaitTaskOptions = {
domWorld: this,
predicateBody: helper.makePredicateString(predicate, queryOne),
predicateAcceptsContextElement: true,
title,
polling,
timeout,
Expand Down Expand Up @@ -691,6 +692,7 @@ export class DOMWorld {
const waitTaskOptions: WaitTaskOptions = {
domWorld: this,
predicateBody: helper.makePredicateString(predicate),
predicateAcceptsContextElement: true,
title,
polling,
timeout,
Expand All @@ -717,6 +719,7 @@ export class DOMWorld {
const waitTaskOptions: WaitTaskOptions = {
domWorld: this,
predicateBody: pageFunction,
predicateAcceptsContextElement: false,
title: 'function',
polling,
timeout,
Expand All @@ -737,6 +740,7 @@ export class DOMWorld {
export interface WaitTaskOptions {
domWorld: DOMWorld;
predicateBody: Function | string;
predicateAcceptsContextElement: boolean;
title: string;
polling: string | number;
timeout: number;
Expand All @@ -753,6 +757,7 @@ export class WaitTask {
_polling: string | number;
_timeout: number;
_predicateBody: string;
_predicateAcceptsContextElement: boolean;
_args: SerializableOrJSHandle[];
_binding: PageBinding;
_runCount = 0;
Expand Down Expand Up @@ -786,6 +791,8 @@ export class WaitTask {
this._timeout = options.timeout;
this._root = options.root;
this._predicateBody = getPredicateBody(options.predicateBody);
this._predicateAcceptsContextElement =
options.predicateAcceptsContextElement;
this._args = options.args;
this._binding = options.binding;
this._runCount = 0;
Expand Down Expand Up @@ -835,6 +842,7 @@ export class WaitTask {
success = await this._root.evaluateHandle(
waitForPredicatePageFunction,
this._predicateBody,
this._predicateAcceptsContextElement,
this._polling,
this._timeout,
...this._args
Expand All @@ -844,6 +852,7 @@ export class WaitTask {
waitForPredicatePageFunction,
null,
this._predicateBody,
this._predicateAcceptsContextElement,
this._polling,
this._timeout,
...this._args
Expand Down Expand Up @@ -911,6 +920,7 @@ export class WaitTask {
async function waitForPredicatePageFunction(
root: Element | Document | null,
predicateBody: string,
predicateAcceptsContextElement: boolean,
polling: string,
timeout: number,
...args: unknown[]
Expand All @@ -927,7 +937,9 @@ async function waitForPredicatePageFunction(
* @returns {!Promise<*>}
*/
async function pollMutation(): Promise<unknown> {
const success = await predicate(root, ...args);
const success = predicateAcceptsContextElement
? await predicate(root, ...args)
: await predicate(...args);
if (success) return Promise.resolve(success);

let fulfill;
Expand All @@ -937,7 +949,9 @@ async function waitForPredicatePageFunction(
observer.disconnect();
fulfill();
}
const success = await predicate(root, ...args);
const success = predicateAcceptsContextElement
? await predicate(root, ...args)
: await predicate(...args);
if (success) {
observer.disconnect();
fulfill(success);
Expand All @@ -962,7 +976,9 @@ async function waitForPredicatePageFunction(
fulfill();
return;
}
const success = await predicate(root, ...args);
const success = predicateAcceptsContextElement
? await predicate(root, ...args)
: await predicate(...args);
if (success) fulfill(success);
else requestAnimationFrame(onRaf);
}
Expand All @@ -979,7 +995,9 @@ async function waitForPredicatePageFunction(
fulfill();
return;
}
const success = await predicate(root, ...args);
const success = predicateAcceptsContextElement
? await predicate(root, ...args)
: await predicate(...args);
if (success) fulfill(success);
else setTimeout(onTimeout, pollInterval);
}
Expand Down
6 changes: 5 additions & 1 deletion test/waittask.spec.ts
Expand Up @@ -293,7 +293,11 @@ describe('waittask specs', function () {
const div = await page.$('div');
let resolved = false;
const waitForFunction = page
.waitForFunction((element) => !element.parentElement, {}, div)
.waitForFunction(
(element) => element.localName === 'div' && !element.parentElement,
{},
div
)
.then(() => (resolved = true));
expect(resolved).toBe(false);
await page.evaluate((element: HTMLElement) => element.remove(), div);
Expand Down