Skip to content

Commit

Permalink
fix: predicate arguments for waitForFunction (#7845)
Browse files Browse the repository at this point in the history
The same predicate function is used by the waitForFunction
API that does not need the context element.

Issues: #7836
  • Loading branch information
OrKoN committed Dec 15, 2021
1 parent 36207c5 commit 1c44551
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
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

0 comments on commit 1c44551

Please sign in to comment.