"context canceled" error

I have a test where I log in (successfully) and see the page that comes up after. I want to click a button on that page, and was able to get it to work… one time.

Every time since, I get
err:read tcp> wsarecv: An existing connection was forcibly closed by the remote host.
followed by
wsURL:"ws://" sid:8B05ABE458CE3C4F3DA7D0F7A095425E err:context canceled
followed by
click "button.cartAddBtn": context canceled

I read this post and the Go github discussions it links to: Read TCP error - request failed - #3 by niklasbae

What I’m wondering is if there’s something I’m doing wrong in my code that’s causing it, or if it’s just a thing where it’s flaky and I should just run it until it succeeds.

My code looks like:

  const user = data[scenario.iterationInTest];

  const browser = launcher.launch('chromium', {
    headless: false,
    ignoreHTTPSErrors: true
  const context = browser.newContext();
  const page = context.newPage();

  // Log in
  page.goto('https://localhost/myApp/register', { waitUntil: 'load' });
  page.fill('#fname', user.user_name)

  // Add to Cart

Hi there, welcome to the forum :slight_smile:

It’s possible that the page.click('button.cartAddBtn') call is being interrupted by the script or iteration ending too early.

Can you try passing the slowMo option to launcher.launch()? E.g. slowMo: '500ms'.

Also, if the click() triggers a DOM change or navigation, you might want to call page.waitForSelector() after to wait for it to complete before ending the iteration or test run.

There are possibly other issues there, like a race condition between click() and waitForNavigation(), but I don’t think that’s the issue you’re running into.

I’m assuming you’re running xk6-browser v0.4.0, but if you’re comfortable with building your own binary, you might want to try the latest main branch, and let us know if it improves things for you. You can build it with xk6 build --with github.com/grafana/xk6-browser@main. Note that there are some breaking syntax changes, so you’ll have to update your script. See the v0.5.0 release notes we’re currently working on for details. Otherwise, you can wait for v0.5.0 to be released next week and use the pre-built binary.

Awesome - thank you. I will see if I can get some time to try out the 0.5.0 release when it’s out.

Side note: For now, I have a reprieve in that the requirements for my testing changed to omit the page that needed to run a payment processor’s js, so I’m dropping back to regular k6 for now. Love that xk6-browser is aiming for Playwright API compat :slight_smile: