K6 Browser: method:GET err:fetching response body: context canceled"

Hello,
I’m trying to get k6-browser up and running but am hitting a problem running against localhost

I get this warning error
method:GET err:fetching response body: context canceled

My k6 script is

import { chromium } from 'k6/experimental/browser';

export default async function () {
  const browser = chromium.launch({
    headless: true,
    timeout: '60s',
  });
  const page = browser.newPage();

  try {
    await page.goto('http://localhost:5173/', { waitUntil: 'domcontentloaded' });
    page.screenshot({ path: 'screenshot.png' });
  } finally {
    page.close();
    browser.close();
  }
}

and I’m running within a docker file

FROM grafana/k6:0.43.1
USER root

RUN apk update && apk add --no-cache chromium

ENV K6_BROWSER_ENABLED=true

ENV XK6_HEADLESS=true

COPY . /k6-load-testing

WORKDIR /k6-load-testing

I’m running the docker container with the following command

docker run --name perf --network=host -i perftests run - <src/ui.js

If I change the url in the script to https://google.co.uk It works fine and I get a screenshot. when running against localhost, the test completes, the screenshot is taken but it’s a blank screen and I see the above warning.

I’m not very familiar with this so any ideas on what I may be doing wrong are welcome

Thanks

Hi @Ryhoward,

There might be an issue with your docker configuration or the localhost server. You might try the following steps.

  1. Check the Docker network configuration: Ensure that the Docker container is running on the same network as the localhost service you’re trying to access. Using the --network=host flag should allow the container to access services running on the host network, but double-check to ensure it’s set up correctly.

  2. Verify the correct host and port: Ensure that the http://localhost:5173/ URL is correct and corresponds to the service you want to test. Make sure the service is running and accessible.

  3. Check the server configuration: Make sure the server running on localhost:5173 is configured correctly to handle the request from the Docker container. It’s possible that the server may be blocking requests coming from Docker or is not responding properly.

  4. Consider the network security settings: If the server on localhost uses HTTPS, it’s possible that the browser in the Docker container is not able to establish a secure connection. You may need to configure the browser to trust the necessary certificates or use a self-signed certificate if applicable.

  5. Verify the waitUntil option: The waitUntil: 'domcontentloaded' option in page.goto() might be causing the issue. Try changing it to waitUntil: 'networkidle' or waitUntil: 'load' to wait for different conditions before taking the screenshot.