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
343 stars 41 forks source link

`browser.isConnected` doesn't seem to detect when the browser process is no longer running #453

Closed ankur22 closed 11 months ago

ankur22 commented 2 years ago

Tested against: https://github.com/grafana/xk6-browser/commit/dbede120c63df43995813a847a25b0e66e289592

The browser.isConnected function doesn't seem to fully work as expected -- it doesn't correctly detect that there is no browser process running and therefore no valid CDP connection. Here's the test script I ran. When the test goes to sleep for 20seconds, that's when I force quit the browser process:

import launcher from 'k6/x/browser';
import { check, sleep } from 'k6';

export default function() {
  const browser = launcher.launch('chromium', {
    headless: false,
  });

  check(browser, {
    'should be connected after launch': browser.isConnected(),
  });

  const page = browser.newPage();
  const res = page.goto('https://google.com');

  sleep(20); // Force quit the browser process now

  check(browser, {
    'should be connected before ending iteration': browser.isConnected(),
  });

  // Disconnect from the browser instance.
  browser.close();
}

I was expecting the final 'should be connected before ending iteration' to fail since the browser process was killed and therefore there shouldn't be a valid connection still open.

Please remember to update the k6-docs and remove reference to this issue (look for <BWIPT id="453"/> and xk6-browser/issues/453).

inancgumus commented 2 years ago

Have you tried it without a promise (without Browser.on)? If you haven't, we might so we can see if it's the on method or the isConnected method.

ankur22 commented 2 years ago

Have you tried it without a promise (without Browser.on)? If you haven't, we might so we can see if it's the on method or the isConnected method.

Good point. I've tested it without calling browser.on and it still exhibits the incorrect behaviour. I will update the script above to avoid any confusion.

inancgumus commented 11 months ago

@ankur22 Is this still relevant since we manage the browser processes?

ankur22 commented 11 months ago

Good point, let's close this 🎉