grafana / xk6-browser

The browser module adds support for browser automation and end-to-end web testing via the Chrome Devtools Protocol to k6.
https://grafana.com/docs/k6/latest/javascript-api/k6-browser/
GNU Affero General Public License v3.0
344 stars 41 forks source link

Evaluate and evaluateHandle cause NPD on nil pageFunc #1543

Open ankur22 opened 3 days ago

ankur22 commented 3 days ago

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/xk6-browser@v1.9.1/browser/js_handle_mapping.go:27 +0x16a
github.com/grafana/xk6-browser/k6ext.promise.func1()
github.com/grafana/xk6-browser@v1.9.1/k6ext/promise.go:24 +0x2c
created by github.com/grafana/xk6-browser/k6ext.promise in goroutine 53
github.com/grafana/xk6-browser@v1.9.1/k6ext/promise.go:23 +0x9a

test run: 3526237

xk6-browser version

v1.9.1

### Tasks
- [ ] #1544
- [ ] Update k6 docs
inancgumus commented 2 days ago

Thanks for the explanation, a fix is here.