Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Evaluate and evaluateHandle cause NPD on nil pageFunc #1543

@ankur22

Description

@ankur22

Brief summary

The cause of this NPD is due to the browser module not correctly checking the arguments before trying to work with them. With JSHandle.evaluate the pageFunc argument is required, but the browser module doesn't correctly check whether it is null/undefined before working with it.

There are many other examples within the browser module where it doesn't first validate the argument before working with it (page, frame, elementHandle).

This issue can be replicated with:

import { browser } from 'k6/browser';

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      options: {
        browser: {
            type: 'chromium',
        },
      },
    },
  },
}

export default async function() {
  const page = await browser.newPage();

  try {
    await page.goto('https://test.k6.io/my_messages.php');

    const jsHandle = await page.waitForSelector('input[name="login"]');
    // This is incorrect, the pageFunction arg is required on evaluate and this is
    // why we get a NPD since we don't check it is nil first.
    await jsHandle.evaluate();
  } finally {
    await page.close();
  }
}

There's also an issue with waitForSelector -- it returns an ElementHandle, but it is mapped to a JSHandle 🤔

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x1606eea]

goroutine 1536 [running]:
github.com/grafana/xk6-browser/browser.mapJSHandle.func3.1()
github.com/grafana/[email protected]/browser/js_handle_mapping.go:27 +0x16a
github.com/grafana/xk6-browser/k6ext.promise.func1()
github.com/grafana/[email protected]/k6ext/promise.go:24 +0x2c
created by github.com/grafana/xk6-browser/k6ext.promise in goroutine 53
github.com/grafana/[email protected]/k6ext/promise.go:23 +0x9a

test run: 3526237

xk6-browser version

v1.9.1

### Tasks
- [ ] #1544
- [x] Update release notes

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions