Observing "GoError: launching browser: exec: no command while running browser tests on docker

Dockerfile used to build the image

# syntax=docker/dockerfile:1

FROM debian:bullseye as browser

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

ENV XK6_HEADLESS=true
FROM golang:1.18 as builder

# build k6 0.36.0 with prometheus support
ENV K6_VERSION=v0.36.0
RUN go install go.k6.io/xk6/cmd/xk6@latest && xk6 build --with github.com/grafana/xk6-browser

WORKDIR /build

ENV CGO_ENABLED=0 
ENV GOOS=linux
COPY . .
RUN cd cmd/agent;go build -o /build/runner -mod mod -a .

FROM loadimpact/k6:0.36.0
WORKDIR /home/k6
COPY --from=builder /go/k6 /usr/bin/k6
COPY --from=builder /build/runner /bin/runner
ENTRYPOINT ["/bin/runner"]

Output:
time=“2022-11-15T12:56:55Z” level=error msg=“GoError: launching browser: exec: no command\n\tat reflect.methodValueCall (native)\n\tat file:///data/test-content:11:34(8)\n\tat native\n” executor=per-vu-iterations scenario=default source=stacktrace

Script Used


import { sleep } from 'k6';

import { chromium } from 'k6/x/browser';
import { Counter, Rate, Trend } from "k6/metrics";

//let  timeToFirstByte = new Trend("time_to_first_byte", true);


export default function () {
  const browser = chromium.launch({
    headless: true,  // false if you wish to view the browser
    slowMo: '500ms',
  });

  const context = browser.newContext();
  const page = context.newPage();
  
  
  page.goto('https://test.k6.io/');
  const elem = page.$('a[href="/my_messages.php"]');
  page.screenshot({ path: `example-chromium.png` });
  elem.click();
  sleep(1)
  page.screenshot({ path: `example-chromium1.png` });
  sleep(10);
  page.close();
  browser.close();
}

Could you please let me know how can resolve the "GoError: launching browser: exec: no command " Error and run the browser test in non-gui mode

Hi @chandana194,

Welcome to the forum!

Thanks for your question. We need to get some more information from you to be able to replicate the issue on our side. Could you please fill in the gaps:

  1. What host OS is your computer running on?
  2. What is this agent that you are building and running alongside k6?
  3. Can you give us the exact step-by-step command line instructions as to how you’re building and running the docker image and test?

I tried your test (with some alterations to goto since it is now async) and it worked on my Mac laptop. These are the steps i took:

  1. Clone the xk6-browser github project:
    git clone https://github.com/grafana/xk6-browser.git
    
  2. Create a test file (test.js) in the root of the cloned project and copy the following test into it:
    import { sleep } from 'k6';
    
    import { chromium } from 'k6/x/browser';
    import { Counter, Rate, Trend } from "k6/metrics";
    
    //let  timeToFirstByte = new Trend("time_to_first_byte", true);
    
    export default function () {
      const browser = chromium.launch({
        headless: true,  // false if you wish to view the browser
        slowMo: '500ms',
      });
    
      const context = browser.newContext();
      const page = context.newPage();
      
      page.goto('https://test.k6.io/').then(() => {
        const elem = page.$('a[href="/my_messages.php"]');
        page.screenshot({ path: `example-chromium.png` });
        elem.click();
        sleep(1)
        page.screenshot({ path: `example-chromium1.png` });
        sleep(10);
      }).finally(() => {
        page.close();
        browser.close();
      })
    }
    
  3. Now build and run the test using docker-compose:
    docker-compose run -T xk6-browser run - <test.js
    
  4. It should run the test without any errors.

Cheers,
Ankur

Hi

I’ve encountered a similar issue.

Having installed k6 v0.43.1 ((devel), go1.20.1, darwin/arm64) on my Mac with the M1, I do receive:

ERRO[0001] Uncaught (in promise) GoError: launching browser: exec: no command
running at github.com/grafana/xk6-browser/browser.mapBrowserType.func1 (native)

I have imported import { chromium } from 'k6/experimental/browser'; and trying to instanciate it with const browser = chromium.launch({ headless: true });

The same script does, however, work with your provided solution by using docker-compose. But in the end, it should be working either way

Hi @Lukas.kaeppeli,

welcome to the forum!

Can you share the Dockerfile you are working with? It looks like the image you are using doesn’t contain a chromium (or chrome) browser.

Try working with this Dockerfile:

FROM grafana/k6:0.43.1
USER root

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

ENV K6_BROWSER_ENABLED=true

Build it with:

docker build -t browser-k6 -f Dockerfile .

Run a test with:

docker run --rm -i browser-k6 run - </path/to/script.js

Cheers,
Ankur

EDIT: edited the docker run command above to work with -i and not -t.

Hi Ankur

Thanks for your quick response. I am not working with a Dockerfile at all. I have installed k6 with homebrew. As of version 0.43.0, it should come pre-installed with a browser (or I have misread the documentation).

However, when following your approach that you explained above (by using GitHub - grafana/xk6-browser: k6 extension that adds support for browser automation and end-to-end web testing via the Chrome Devtools Protocol), everything works fine

Hi @Lukas.kaeppeli,

k6 and the k6-browser module do not come with a pre-installed browser, it works with the existing Chrome/Chromium browser installed on your system.

I’m glad the docker method worked for you :slight_smile:

Cheers,
Ankur

Hi @ankur
I followed your steps to run with docker:

docker build -t browser-k6 -f Dockerfile .
docker run --rm -t browser-k6 run - <script.js

But the script is not running, seem it stays loading.I used your docker file example:

FROM grafana/k6:0.43.0
USER root

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

ENV K6_BROWSER_ENABLED=true

My script.js is :

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

export default async function () {
  const browser = chromium.launch({ headless: true });
  const page = browser.newPage();
  
  try {
    await page.goto('https://test.k6.io/my_messages.php', { waitUntil: 'networkidle' });

    // Enter login credentials
    page.locator('input[name="login"]').type('admin');
    page.locator('input[name="password"]').type('123');

    page.screenshot({ path: 'screenshot.png' });
  } finally {
    page.close();
    browser.close();
  }
}

could you please help with it ?

Hi @vsanabria ,

I think there was a confusion on the docker run command, you have to use the -i flag in order for the container to be able to read from stdin like:

docker run --rm -i browser-k6 run - <script.js

Let me know if that works.

2 Likes

yes, it worked, thank you!!!

I did the same steps

  • Cloned the repository
  • Build the docker image and run the script

I get this error , please help.

level=error msg=“TypeError: Cannot read property ‘launch’ of undefined or null\n\tat file:///-:9:20(3)\n” executor=per-vu-iterations scenario=default source=stacktrace

Hi @aashishk7,

We’ve changed our API recently, and launch() has been removed.

Please see our migration guide for the new API. For the cloned repository, remember that you should import from k6/x/browser instead of k6/experimental/browser.

Thanks.

Tried updating the script and am using the DockerFile from github

Error :

time=“2023-10-06T17:38:22Z” level=error msg="error building browser on IterStart: launching browser: exec: no command at

Script :

import { browser } from ‘k6/experimental/browser’;

export const options = {
scenarios: {
ui: {
executor: ‘shared-iterations’,
options: {
browser: {
type: ‘chromium’,
},
},
},
}
}

export default async function () {
const page = browser.newPage();

try {
    await page.goto('https://test.k6.io/');
    page.screenshot({ path: 'screenshot.png' });
} finally {
    page.close();
}

}

DockerFile

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 - grafana/xk6-browser: k6 extension that adds support for browser automation and end-to-end web testing via the Chrome Devtools Protocol

FROM debian:bullseye

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

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

ENV K6_BROWSER_HEADLESS=true

ENTRYPOINT [“k6”]

This might mean the Docker container doesn’t contain Chromium. Please ensure it has Chromium, or you can set its path using the environment variable K6_BROWSER_EXECUTABLE_PATH. Hope this helps.