Class Watcher<T>

Watches a matcher function, and trigger whenever it changes from matching to not.

Remarks

Whenever wait resolves, it runs match, and triggers enter with the match result, or leave without, then wait again.

By default, wait is a function that resolves on every page navigation or mutation.

Example

async function step(ctx: Context) {
const spec = {
wait(): Promise<void> {
return new Promise((resolve, reject) => {
setTimeout(() => { reject() }, 5000)
})
},
async match() {
return await ctx.page.waitForSelector('#input:focus');
},
async enter(match): Promise<void> {
return await match.evaluate(el => {
alert(`you have just focused ${el.id}`);
});
},
leave(match): Promise<void> {
return new Promise(resolve => {
alert(`you are left the input so gonna be redirected to google, just in case`);
ctx.page.goto('https://google.com');
resolve();
})
}
};
const watcher = new Watcher(ctx, spec);
await watcher.start();
}

Param

Context object.

Param

Options for running the watcher.

Type Parameters

  • T

Hierarchy

  • Watcher

Constructors

Properties

ctx: Context
enter?: ((match) => Promise<void>)

Type declaration

    • (match): Promise<void>
    • Parameters

      • match: T

      Returns Promise<void>

ignoreMatchError: boolean = false
ignoreWaitError: boolean = false
lastMatch?: T
leave?: ((match?) => Promise<void>)

Type declaration

    • (match?): Promise<void>
    • Parameters

      • Optional match: T

      Returns Promise<void>

match: (() => Promise<undefined | null | T>)

Type declaration

    • (): Promise<undefined | null | T>
    • Returns Promise<undefined | null | T>

matching: boolean = false
running: boolean = false
wait: (() => Promise<void>)

Type declaration

    • (): Promise<void>
    • Returns Promise<void>

Methods

  • Parameters

    • retry: boolean = false

    Returns Promise<undefined | null | T>

  • Parameters

    • retry: boolean = false

    Returns Promise<void>

  • Returns the last stored match, if any.

    Returns undefined | T

  • Stops the watcher.

    Returns Promise<void>

    Remarks

    If matching when stopped, will also trigger leave.

Generated using TypeDoc