Skip to content

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

@Dinnerbone

Description

@Dinnerbone

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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions