UNPKG

4.72 kBTypeScriptView Raw
1export interface InitScript<Payload = undefined> {
2 remove: () => Promise<void>;
3 on: (event: 'data', listener: (data: Payload) => void) => void;
4}
5/**
6 * Callback to emit data from the browser back to the Node.js environment. In order to receive the
7 * data returned by the callback function you have to listen to the `data` event, e.g.
8 *
9 * ```js
10 * const script = await browser.addInitScript((emit) => {
11 * emit('hello')
12 * })
13 * script.on('data', (data) => {
14 * console.log(data) // prints: hello
15 * })
16 * ```
17 *
18 * @param {any} data The data to emit.
19 */
20type InitScriptCallback<Payload> = (data: Payload) => void;
21type InitScriptFunction<Payload> = ((emit: InitScriptCallback<Payload>) => void | Promise<void>);
22type InitScriptFunctionArg1<Payload, Arg1> = ((arg1: Arg1, emit: InitScriptCallback<Payload>) => void | Promise<void>);
23type InitScriptFunctionArg2<Payload, Arg1, Arg2> = ((arg1: Arg1, arg2: Arg2, emit: InitScriptCallback<Payload>) => void | Promise<void>);
24type InitScriptFunctionArg3<Payload, Arg1, Arg2, Arg3> = ((arg1: Arg1, arg2: Arg2, arg3: Arg3, emit: InitScriptCallback<Payload>) => void | Promise<void>);
25type InitScriptFunctionArg4<Payload, Arg1, Arg2, Arg3, Arg4> = ((arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, emit: InitScriptCallback<Payload>) => void | Promise<void>);
26type InitScriptFunctionArg5<Payload, Arg1, Arg2, Arg3, Arg4, Arg5> = ((arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, emit: InitScriptCallback<Payload>) => void | Promise<void>);
27/**
28 * Adds a script which would be evaluated in one of the following scenarios:
29 *
30 * - Whenever the page is navigated.
31 * - Whenever the child frame is attached or navigated. In this case, the script is evaluated in
32 * the context of the newly attached frame.
33 *
34 * The script is evaluated after the document was created but before any of its scripts were run.
35 * In order to remove the initialization script from the page again, call the function that got
36 * returned by this function.
37 *
38 * This is useful to amend the JavaScript environment, e.g. to seed Math.random.
39 *
40 * <example>
41 :addInitScript.js
42 const script = await browser.addInitScript((seed) => {
43 Math.random = () => seed
44 }, 42)
45
46 await browser.url('https://webdriver.io')
47 console.log(await browser.execute(() => Math.random())) // returns 42
48
49 await reset()
50 await browser.url('https://webdriver.io')
51 console.log(await browser.execute(() => Math.random())) // returns a random number
52 * </example>
53 *
54 * Furthermore you can also use the `emit` function to send data back to the Node.js environment.
55 * This is useful if you want to observe certain events in the browser environment, e.g.:
56 *
57 * <example>
58 :addInitScriptWithEmit.js
59 const script = await browser.addInitScript((emit) => {
60 const observer = new MutationObserver((mutations) => {
61 for (const mutation of mutations) {
62 emit(mutation.target.nodeName)
63 }
64 })
65 observer.observe(document, { childList: true, subtree: true })
66 })
67
68 script.on('data', (data) => {
69 console.log(data) // prints: BODY, DIV, P, ...
70 })
71 * </example>
72 *
73 * @alias browser.addInitScript
74 * @param {Function} script function to be injected as initialization script
75 * @param {number|string|boolean} args parameters for the script
76 * @type utility
77 *
78 */
79export declare function addInitScript<Payload>(this: WebdriverIO.Browser, script: string | InitScriptFunction<Payload>): Promise<InitScript<Payload>>;
80export declare function addInitScript<Payload, Arg1>(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg1<Payload, Arg1>, arg1: Arg1): Promise<InitScript<Payload>>;
81export declare function addInitScript<Payload, Arg1, Arg2>(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg2<Payload, Arg1, Arg2>, arg1: Arg1, arg2: Arg2): Promise<InitScript<Payload>>;
82export declare function addInitScript<Payload, Arg1, Arg2, Arg3>(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg3<Payload, Arg1, Arg2, Arg3>, arg1: Arg1, arg2: Arg2, arg3: Arg3): Promise<InitScript<Payload>>;
83export declare function addInitScript<Payload, Arg1, Arg2, Arg3, Arg4>(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg4<Payload, Arg1, Arg2, Arg3, Arg4>, arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4): Promise<InitScript<Payload>>;
84export declare function addInitScript<Payload, Arg1, Arg2, Arg3, Arg4, Arg5>(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg5<Payload, Arg1, Arg2, Arg3, Arg4, Arg5>, arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5): Promise<InitScript<Payload>>;
85export {};
86//# sourceMappingURL=addInitScript.d.ts.map
\No newline at end of file