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

Add HTTP Basic Auth #203

Closed imiric closed 2 years ago

imiric commented 2 years ago

The httpCredentials BrowserContext option doesn't seem to be working, and HTTP basic auth in general seems to be partially implemented.

Version: de8c9d2

Steps to reproduce

Run the following script:

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

export default function() {
  const browser = launcher.launch('chromium', {
    headless: false,
  });
  const context = browser.newContext({
    httpCredentials: {
      username: 'user',
      password: 'pass',
    }
  });
  const page = context.newPage();

  page.goto('https://authenticationtest.com/HTTPAuth/', { waitUntil: 'load' });
  sleep(3);

  page.close();
  browser.close();
}

On de8c9d2 (#202) this fails with:

ERRO[0001] Failed to load resource: the server responded with a status of 401 ()  browser_source=network line_number=0 source=browser stacktrace="<nil>" url="chrome-error://chromewebdata/"
ERRO[0001] navigation interrupted by another one
running at reflect.methodValueCall (native)
default at file:///home/ivan/Projects/grafana/xk6-browser/examples/test-105-cdp-fetch.js:16:56(34)

Commenting out the navigation interrupted by another one line, shows the browser window and credential prompt, and the above 401 error is logged.

Implementation suggestions

This seems to require request interception with the Fetch domain, which is partially implemented here. What's missing is a call to fetch.ContinueWithAuth().

unlikelyzero commented 2 years ago

Currently this is a blocker for our internal performance testing as everything is (presently, but will not always be) routed through basic auth.

imiric commented 2 years ago

Hi @unlikelyzero, basic auth support will be released in the upcoming v0.2.0, scheduled for later this week, or you can use it now by building the extension from main.