1 | export interface InitScript<Payload = undefined> {
|
2 | remove: () => Promise<void>;
|
3 | on: (event: 'data', listener: (data: Payload) => void) => void;
|
4 | }
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | type InitScriptCallback<Payload> = (data: Payload) => void;
|
21 | type InitScriptFunction<Payload> = ((emit: InitScriptCallback<Payload>) => void | Promise<void>);
|
22 | type InitScriptFunctionArg1<Payload, Arg1> = ((arg1: Arg1, emit: InitScriptCallback<Payload>) => void | Promise<void>);
|
23 | type InitScriptFunctionArg2<Payload, Arg1, Arg2> = ((arg1: Arg1, arg2: Arg2, emit: InitScriptCallback<Payload>) => void | Promise<void>);
|
24 | type InitScriptFunctionArg3<Payload, Arg1, Arg2, Arg3> = ((arg1: Arg1, arg2: Arg2, arg3: Arg3, emit: InitScriptCallback<Payload>) => void | Promise<void>);
|
25 | type InitScriptFunctionArg4<Payload, Arg1, Arg2, Arg3, Arg4> = ((arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, emit: InitScriptCallback<Payload>) => void | Promise<void>);
|
26 | type 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:
|
47 | console.log(await browser.execute(() => Math.random()))
|
48 |
|
49 | await reset()
|
50 | await browser.url('https:
|
51 | console.log(await browser.execute(() => Math.random()))
|
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)
|
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 | */
|
79 | export declare function addInitScript<Payload>(this: WebdriverIO.Browser, script: string | InitScriptFunction<Payload>): Promise<InitScript<Payload>>;
|
80 | export declare function addInitScript<Payload, Arg1>(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg1<Payload, Arg1>, arg1: Arg1): Promise<InitScript<Payload>>;
|
81 | export declare function addInitScript<Payload, Arg1, Arg2>(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg2<Payload, Arg1, Arg2>, arg1: Arg1, arg2: Arg2): Promise<InitScript<Payload>>;
|
82 | export 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>>;
|
83 | export 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>>;
|
84 | export 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>>;
|
85 | export {};
|
86 |
|
\ | No newline at end of file |