export interface InitScript { remove: () => Promise; on: (event: 'data', listener: (data: Payload) => void) => void; } /** * Callback to emit data from the browser back to the Node.js environment. In order to receive the * data returned by the callback function you have to listen to the `data` event, e.g. * * ```js * const script = await browser.addInitScript((emit) => { * emit('hello') * }) * script.on('data', (data) => { * console.log(data) // prints: hello * }) * ``` * * @param {any} data The data to emit. */ type InitScriptCallback = (data: Payload) => void; type InitScriptFunction = ((emit: InitScriptCallback) => void | Promise); type InitScriptFunctionArg1 = ((arg1: Arg1, emit: InitScriptCallback) => void | Promise); type InitScriptFunctionArg2 = ((arg1: Arg1, arg2: Arg2, emit: InitScriptCallback) => void | Promise); type InitScriptFunctionArg3 = ((arg1: Arg1, arg2: Arg2, arg3: Arg3, emit: InitScriptCallback) => void | Promise); type InitScriptFunctionArg4 = ((arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, emit: InitScriptCallback) => void | Promise); type InitScriptFunctionArg5 = ((arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, emit: InitScriptCallback) => void | Promise); /** * Adds a script which would be evaluated in one of the following scenarios: * * - Whenever the page is navigated. * - Whenever the child frame is attached or navigated. In this case, the script is evaluated in * the context of the newly attached frame. * * The script is evaluated after the document was created but before any of its scripts were run. * In order to remove the initialization script from the page again, call the function that got * returned by this function. * * This is useful to amend the JavaScript environment, e.g. to seed Math.random. * * :addInitScript.js const script = await browser.addInitScript((seed) => { Math.random = () => seed }, 42) await browser.url('https://webdriver.io') console.log(await browser.execute(() => Math.random())) // returns 42 await reset() await browser.url('https://webdriver.io') console.log(await browser.execute(() => Math.random())) // returns a random number * * * Furthermore you can also use the `emit` function to send data back to the Node.js environment. * This is useful if you want to observe certain events in the browser environment, e.g.: * * :addInitScriptWithEmit.js const script = await browser.addInitScript((emit) => { const observer = new MutationObserver((mutations) => { for (const mutation of mutations) { emit(mutation.target.nodeName) } }) observer.observe(document, { childList: true, subtree: true }) }) script.on('data', (data) => { console.log(data) // prints: BODY, DIV, P, ... }) * * * @alias browser.addInitScript * @param {Function} script function to be injected as initialization script * @param {number|string|boolean} args parameters for the script * @type utility * */ export declare function addInitScript(this: WebdriverIO.Browser, script: string | InitScriptFunction): Promise>; export declare function addInitScript(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg1, arg1: Arg1): Promise>; export declare function addInitScript(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg2, arg1: Arg1, arg2: Arg2): Promise>; export declare function addInitScript(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg3, arg1: Arg1, arg2: Arg2, arg3: Arg3): Promise>; export declare function addInitScript(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg4, arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4): Promise>; export declare function addInitScript(this: WebdriverIO.Browser, script: string | InitScriptFunctionArg5, arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5): Promise>; export {}; //# sourceMappingURL=addInitScript.d.ts.map