diff --git a/e2e/wdio/headless/test.e2e.ts b/e2e/wdio/headless/test.e2e.ts index 1606a4fb4..3f54d095f 100644 --- a/e2e/wdio/headless/test.e2e.ts +++ b/e2e/wdio/headless/test.e2e.ts @@ -430,6 +430,25 @@ describe('main suite 1', () => { expect(await browser.execute(() => Math.random())).not.toBe(42) }) + it('should escape the init script', async () => { + type WindowWithHello = Window & { + sayHello?: (name: string) => string; + } + const script = await browser.addInitScript(() => { + (window as WindowWithHello).sayHello = (name) => + `Hello ${name}` + }) + + await browser.url('https://webdriver.io') + expect( + await browser.execute(() => + (window as WindowWithHello).sayHello!('there'), + ), + ).toBe('Hello there') + + await script.remove() + }) + it('passed on callback function', async () => { const script = await browser.addInitScript((num, str, bool, emit) => { setTimeout(() => emit(JSON.stringify([num, str, bool])), 500) diff --git a/packages/webdriverio/src/commands/browser/addInitScript.ts b/packages/webdriverio/src/commands/browser/addInitScript.ts index 907fd495e..cc811a0ee 100644 --- a/packages/webdriverio/src/commands/browser/addInitScript.ts +++ b/packages/webdriverio/src/commands/browser/addInitScript.ts @@ -113,8 +113,9 @@ export async function addInitScript ( const serializedParameters = (args || []).map((arg: unknown) => JSON.stringify(arg)) const context = await this.getWindowHandle() + const src = 'return ' + script.toString() const fn = `(emit) => { - const closure = new Function(\`return ${script.toString()}\`) + const closure = new Function(${JSON.stringify(src)}) return closure()(${serializedParameters.length ? `${serializedParameters.join(', ')}, emit` : 'emit'}) }` const channel = btoa(fn.toString())