Error creating new page in browser context

Hello,

I’m having trouble getting a browser test to work. I am running k6 through Docker and have the following Dockerfile:

FROM grafana/k6:latest
USER root

RUN apk update && apk add --no-cache chromium
...
ENV K6_BROWSER_ENABLED=true
ENV LOAD_TEST_TARGET_VU_COUNT=1
ENV LOAD_TEST_SECRET_SEED=1

Here is my test script:

import { sleep, check } from "k6";
import { Rate } from "k6/metrics";
import { chromium } from "k6/experimental/browser";

export const errorRate = new Rate("errors");

export default async function () {
  const browser = chromium.launch({
    args: ["no-sandbox"],
    ignoreDefaultArgs: ["enable-automation"],
    headless: true,
    timeout: "60s", // Or whatever time you want to define
  });
  console.log("🚀launching browser");

  // I've tried using an explicit context resulting in a similar error 
  // const context = browser.newContext();
  // // const page = context.newPage();

  const page = browser.newPage();
  console.log("✨new page");

  try {
    await page.goto("https://somepage.com/login");
    let submitButton = page.locator('input[type="submit"]');
    ...
  } finally {
    page.screenshot({ path: "screenshot.png" });

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

  check(true, {
    "status is 200": () => true,
  }) || errorRate.add(1);
  sleep(1);
}

How I build and run the image:

docker build -t browser-k6 -f ./load-test/Dockerfile .
docker run --rm --network="host" -i browser-k6 run - <./load-test/src/test.js

Error:

time="2023-05-08T14:19:04Z" level=error msg="communicating with browser: websocket: close 1006 (abnormal closure): unexpected EOF" category=cdp elapsed="0 ms" goroutine=56
time="2023-05-08T14:19:04Z" level=error msg="process with PID 17 unexpectedly ended: signal: trace/breakpoint trap" category=browser elapsed="6 ms" goroutine=86
time="2023-05-08T14:19:04Z" level=error msg="Uncaught (in promise) GoError: creating new page in browser context: canceled\n\tat github.com/grafana/xk6-browser/browser.mapBrowser.func3 (native)\n\tat file:///-:49:15(26)\n" executor=per-vu-iterations scenario=default

Error when using explicit context:

time="2023-05-08T14:30:00Z" level=error msg="communicating with browser: read tcp 127.0.0.1:57700->127.0.0.1:33765: read: connection reset by peer" category=cdp elapsed="0 ms" goroutine=82
time="2023-05-08T14:30:00Z" level=error msg="process with PID 17 unexpectedly ended: signal: trace/breakpoint trap" category=browser elapsed="5 ms" goroutine=70
time="2023-05-08T14:30:00Z" level=error msg="Uncaught (in promise) GoError: creating new page in browser context: creating a new blank page: read tcp 127.0.0.1:57700->127.0.0.1:33765: read: connection reset by peer\n\tat github.com/grafana/xk6-browser/browser.mapBrowserContext.func4 (native)\n\tat file:///-:48:15(30)\n" executor=per-vu-iterations scenario=default

I’m enjoying using k6, thank you for all your hard work on the library and community. Thank you also for your help ahead of time.

Hi @urmit,

Welcome to the forum :wave:

Are you running this on an M series Apple computer (ARM architecture)? I’ve ran into the same issue on my Mac. grafana/k6:latest image is currently only built for amd64.

This dockerfile (which is part of the xk6-browser repo) works for me:

FROM golang:1.19-bullseye as builder

RUN go install -trimpath go.k6.io/xk6/cmd/xk6@latest

RUN  xk6 build --output "/tmp/k6" --with github.com/grafana/xk6-browser

FROM debian:bullseye

RUN apt-get update && \
    apt-get install -y chromium

COPY --from=builder /tmp/k6 /usr/bin/k6

ENV XK6_HEADLESS=true
ENV K6_BROWSER_ENABLED=true

ENTRYPOINT ["k6"]

Let me know if that works for you or not.

Cheers,
Ankur

Hey @ankur,

Thanks for your quick response. This was a huge help and it works great

1 Like

Hi all!

Thanks so much for being active in the k6 forum and I love the capabilities you provide us in testing!

Was having the same problem as well, in Apple M1.

Script (from the sample docs)

(have a setup script to extract test data from a source)

export default async function (data?: any) {
  if (!data) return;

  const browser = chromium.launch({
    args: ['no-sandbox'], 
    headless: true,
    timeout: '10m',
    slowMo: '100ms'
  });
  const context = browser.newContext({
    userAgent: 'k6/custom-userAgent AppleWebKit '
  
  });
  const page = context.newPage();

  try {
    await page.goto(data.url, { waitUntil: 'networkidle' });

    page.locator('input[name="login"]').type(data.username);
    page.locator('input[name="password"]').type(data.password);

    const submitButton = page.locator('input[type="submit"]');

    await Promise.all([page.waitForNavigation(), submitButton.click()]);

    check(page, {
      header: page.locator('h2').textContent() == 'Welcome, admin!'
    });
  } finally {
    page.close();
    browser.close();
  }
}


Dockerfile:

FROM golang:1.20.3-bullseye as builder

RUN .....

RUN go install -trimpath go.k6.io/xk6/cmd/xk6@v0.9.1
RUN xk6 build --output "/tmp/k6" --with github.com/grafana/xk6-browser


FROM alpine:3.17

COPY additional-ca-certs.crt /root/additional-ca-certs.crt
COPY additional-ca-certs.crt /usr/local/share/ca-certificates/additional-ca-certs.crt
RUN cat /root/additional-ca-certs.crt >> /etc/ssl/certs/ca-certificates.crt

RUN apk update && \
  apk --no-cache add ca-certificates curl && \
  apk --no-cache add gnupg && \
  apk --no-cache add chromium && \
  update-ca-certificates && \
  rm -rf /tmp/* /var/cache/apk/*

...

COPY --from=builder /tmp/k6 /usr/bin/k6
RUN adduser -D -u .... -g .... k6
WORKDIR /home/k6

USER k6
ENV XK6_HEADLESS=true
ENV K6_BROWSER_ENABLED=true
ENV RUN_IN_CONTAINER="True"
ENTRYPOINT ["/usr/bin/timeout", "15m", "./entrypoint.sh"]

To run this:

docker run -it --rm --platform linux/arm64 --env-file ./.env.local k6-platform:latest

But i get the error:

INFO[0004] k6_basic https://test.k6.io/my_messages.php admin ***  source=console
ERRO[0037] communicating with browser: read tcp 127.0.0.1:35342->127.0.0.1:45547: read: connection reset by peer  category=cdp elapsed="0 ms" goroutine=78
ERRO[0037] Uncaught (in promise) GoError: internal error while removing binding from page: websocket: close 1001 (going away)
running at github.com/grafana/xk6-browser/api.Page.Close-fm (native)
k6_basicat _callee$ (webpack://@cmctechnology/stockbroking-automation-k6-platforms/./src/tests/basic/k6Basic.test.ts:58:15(166))
	at call (native)
	at tryCatch (webpack://@cmctechnology/stockbroking-automation-k6-platforms/./src/tests/basic/k6Basic.test.ts:2:0(9))
	at webpack://@cmctechnology/stockbroking-automation-k6-platforms/./src/tests/basic/k6Basic.test.ts:2:0(101)
	at webpack://@cmctechnology/stockbroking-automation-k6-platforms/./src/tests/basic/k6Basic.test.ts:2:0(5)
	at asyncGeneratorStep (webpack://@cmctechnology/stockbroking-automation-k6-platforms/./src/tests/basic/k6Basic.test.ts:2:0(6))
	at _next (webpack://@cmctechnology/stockbroking-automation-k6-platforms/./src/tests/basic/k6Basic.test.ts:2:0(10))  executor=per-vu-iterations scenario=k6_basic
ERRO[0037] process with PID 22 unexpectedly ended: signal: killed  category=browser elapsed="5 ms" goroutine=72

The script works on

  • my local M1
  • the docker image works in my colleague’s apple intel i9

so seems to be the arm architecture that’s giving us trouble this time.

Hi @icedlatte,

This seems like a bug :thinking: Could you please create an issue about this on our Github? Thanks!

1 Like

Hi @icedlatte,

Have you tried working with debian instead of alpine? I’ve found that working with debian gets me better results when working on M1 Macs.

Cheers,
Ankur

1 Like

hello all, thank you so much for the guidance!

We managed to get things to work with alpine for M1, running it as:

docker run -it --rm --platform linux/arm64 ...

we also found that our firewall contributes to these issues we’re facing with M1, and we managed to work around that as well.

Cheers!

3 Likes