webdriverio: [🐛 Bug]: browser.execute breaks when declaring functions

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

9.0.7

Node.js Version

v20.13.1

Mode

WDIO Testrunner

Which capabilities are you using?

{
    browserName: 'chrome'
}

What happened?

When using browser.execute with a script that declares a function, the script gets transformed in such a way that no longer runs inside a browser. This works fine in v8.

For example:

browser.execute(() => {
    function fn() {}
});

The above gets transformed, and the script that’s injected onto the page becomes:

((...args) => {
      function callFunction(f, args) {
        const deserializedThis = args.shift();
        const deserializedArgs = args;
        return f.apply(deserializedThis, deserializedArgs);
      }
      return callFunction((
        ()=>{function fn(){}__name(fn,"fn")}
      ), args);
    })

__name doesn’t exist, so it throws an error.

What is your expected behavior?

It should work.

How to reproduce the bug.

I’ve made a minimal reproducible example at https://github.com/Dinnerbone/wdio-bug-repro/

The actual test that breaks is https://github.com/Dinnerbone/wdio-bug-repro/blob/main/test/specs/test.e2e.ts

This was generated with npx create-wdio@latest just moments ago, the only change is that test.

Relevant log output

[0-0] 2024-08-23T18:20:53.484Z INFO webdriver: BIDI COMMAND script.callFunction {"functionDeclaration":"<Function[53 bytes]>","awaitPromise":false,"arguments":[],"target":{"context":"29984D43E1634C4142BF795BC9F87764"}}
[0-0] AssertionError in "Javascript execution.works"
AssertionError [ERR_ASSERTION]: {
  error: {
    columnNumber: 28,
    exception: {
      type: 'error'
    },
    lineNumber: 6,
    message: 'ReferenceError: __name is not defined',
    stackTrace: {
      callFrames: [
        {
          columnNumber: 28,
          functionName: '',
          lineNumber: 6,
          url: ''
        },
        {
          columnNumber: 17,
          functionName: 'callFunction',
          lineNumber: 3,
          url: ''
        },
        {
          columnNumber: 13,
          functionName: '',
 ... == 'Success!'
    at Context.<anonymous> (C:\Users\Dinnerbone\workspace\personal\wdio-bug\test\specs\test.e2e.ts:13:16)
    at Context.executeAsync (C:\Users\Dinnerbone\workspace\personal\wdio-bug\node_modules\@wdio\utils\build\index.js:1488:20)
    at Context.testFrameworkFnWrapper (C:\Users\Dinnerbone\workspace\personal\wdio-bug\node_modules\@wdio\utils\build\index.js:1559:14)

Code of Conduct

  • I agree to follow this project’s Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 24 (24 by maintainers)

Commits related to this issue

Most upvoted comments

Interesting. I wonder if this is a Chromedriver issue as the Chrome team doesn’t really focus on Electron compatibility right now. I think it is totally fine to go the CDP route since Electron is Chromium only.

You’re right, it seems to be related to using TS specs:

I reproduced this in the service E2Es: https://github.com/webdriverio-community/wdio-electron-service/actions/runs/10763880538/job/29845930112?pr=763

I also added JS versions of the E2Es, they pass but the TS versions fail: https://github.com/webdriverio-community/wdio-electron-service/actions/runs/10775300006/job/29879387541?pr=763

The reproduction case now works with the latest version of WebdriverIO. Can you please provide a simple reproduction case for the use case that doesn’t work for you?

await browser.addInitScript(() => {
    const closure = new Function(
        'var __defProp = Object.defineProperty;' +
        'var __name = (target, value) => __defProp(target, \'name\', { value, configurable: true });' +
        'window.__name = __name;'
    )
    return closure()
})

If that were the only workaround Ruffle needed it may be worth it, but it seems pretty hacky so we may just stay on webdriverio v8 until this and https://github.com/webdriverio/webdriverio/issues/13218 are resolved.

Hello. I use wdio for testing Electron app. Having the same issue after upgrade to wdio v9.0.x

Here is the error:

[0-0] 2024-08-23T22:02:04.347Z INFO webdriver: DATA {
[0-0]   script: 'return (imgurPluginId=>{const uploadStub=__name(()=>Promise.resolve("https://i.imgur.com/w88wB4I.png"),"uploadStub");app.plugins.plugins[imgurPluginId].imgUploader.upload=uploadStub}).apply(null, arguments)',
[0-0]   args: [ 'obsidian-imgur-plugin' ]
[0-0] }
[0-0] 2024-08-23T22:02:04.350Z ERROR webdriver: Request failed with status 500 due to javascript error: javascript error: __name is not defined
[0-0]   (Session info: chrome=120.0.6099.283)
[0-0] javascript error in "Electron Testing.blank note.uploads clipboard image on PASTE shortcut"
javascript error: javascript error: __name is not defined
  (Session info: chrome=120.0.6099.283)
    at async Context.<anonymous> (/Users/user/Documents/projects/obsidian-imgur-plugin/test/e2e/specs/test.e2e.ts:22:7)

The function my e2e test executes is:

await browser.execute((imgurPluginId: typeof IMGUR_PLUGIN_ID) => {
  // @ts-expect-error 'app' exists in Obsidian
  declare const app: App
  const uploadStub = () => Promise.resolve('https://i.imgur.com/w88wB4I.png')
  app.plugins.plugins[imgurPluginId].imgUploader.upload = uploadStub
}, IMGUR_PLUGIN_ID)

Rolling back wdio to the latest v8.x solves the issue.

For reference, possibly relevant esbuild issue (closed as unsupported): https://github.com/evanw/esbuild/issues/607