K6 Browser shuts down prematurely

import { chromium } from 'k6/experimental/browser';
import { check } from 'k6'
//XK6_BROWSER_LOG=trace K6_BROWSER_ENABLED=true k6 run  Browser3.js

export default async function () {
  const browser = chromium.launch({ headless: false });
  const page = browser.newPage();
  console.log("We are at 0!");

  try {
    console.log("We are at 1!");
    await page.goto('https://search.apps-staging.epo.org/', { waitUntil: 'networkidle' });
    console.log("We are at 2!");
    await page.waitForTimeout(35000);
    page.screenshot({ path: '../img/Credentials.png' });

    console.log("We are at 3!");
    await Promise.all([
      page.waitForNavigation(),
      //page.locator('input[type="submit"]').click(),
      //page.screenshot({ path: '../img/Logon.png' }),

      page.locator("//button[@data-testid='marker-search-button']").click(),
      page.waitForSelector("//div[contains(@style, 'display: block;') and contains(@class, 'Spinner__spinner')]", {visible:false}),
    ]);

    page.screenshot({ path: '../img/Log.png' });

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

in the logs i see we only ever reach We are at 1! We never reach INFO[0017] We are at 2!
I can see th browser opened and the page loads when i run the script, then browser just shuts down before we “We are at 2!”

I can see the following error in the logs
ERRO[0113] communicating with browser: read tcp 127.0.0.1:16261->127.0.0.1:16260: wsarecv: An existing connection was forcibly closed by the
remote host. category=cdp elapsed=“0 ms” goroutine=33

Hi @pbains1,

I’ve tried running your test script and it’s able to navigate to the website under test that is in the test script, and so I see We are at 2! in the console logs.

  1. What OS are you running this script on?
  2. Are you running it in Docker? If so which version of Docker?

Cheers,
Ankur

Hi Ankur,

  1. The OS details are:
    Edition Windows 10 Pro
    Version 22H2
    Installed on ‎29-‎06-‎2021
    OS build 19045.2728
    Experience Windows Feature Experience Pack 120.2212.4190.0

  2. I am not running in docker.

Hi,

I have been playing with the script.
If i remove the //await page.waitForTimeout(15000); and replace it with a page.waitForNavigation(). The script proceeds to "We are at 3! " but fails with the following error when trying to click //button[@data-testid='marker-search-button. The browser is closed prematurely. The following error is logged:
Uncaught (in promise) GoError: clicking on “//button[@data-testid=‘marker-search-button’]”: getting new document handle: getting
document element handle: execution context changed; most likely because of a navigation
at reflect.methodValueCall (native)
at file:///D:/k6/scripts/k6/ansera/Ansera_TC1.js:24:6(57)

If i leave the await page.waitForTimeout(15000); then the script works.

Hi @pbains1,

I think you nearly solved your own problem :slight_smile:

I hadn’t noticed the await keyword on page.waitForTimeout(35000); and the page.waitForSelector in the promise.All. Currently the only browser async APIs are:

  • locator.click
  • elementHandle.click
  • frame.click
  • frame.goto
  • frame.waitForFunction
  • frame.waitForNavigation
  • page.click
  • page.goto
  • page.waitForFunction
  • page.waitForNavigation
  • page.setExtraHTTPHeaders
  • browser.on
  • locator.click

So using await or working with promises on any of the other APIs will not work as expected.

If you remove the await from page.waitForTimeout(35000); and move page.waitForSelector outside of promise.All, then hopefully it will work. I tried on a Windows 11 computer and i was able to get to “We are at 3!”. After that, it fails to locate //button[@data-testid='marker-search-button and times out.

Hope this helps,
Ankur

Hi Ankur,

The following scripts works for me:
try {
await page.goto(‘https://search.apps-staging.epo.org/’, { waitUntil: ‘networkidle’ });
page.waitForTimeout(15000);
page.screenshot({ path: ‘…/img/Start.png’ });

await Promise.all([
page.locator(“//button[@data-testid=‘marker-search-button’]”).click(),
]);

page.waitForSelector(“//div[contains(@style, ‘display: block;’) and contains(@class, ‘Spinner__spinner’)]”, {visible:false}),
page.screenshot({ path: ‘…/img/End.png’ }),

check(page, {
‘header’: page.locator(‘h2’).textContent() == ‘Ansera’,
});
}

But if i remove the page.waitForTimeout(15000) the script will fail. Why is this? Shouldn’t await page.goto(‘https://search.apps-staging.epo.org/’, { waitUntil: ‘networkidle’ });
be sufficient? Obviously having a fied wait such as page.waitForTimeout is not ideal. The following script works in playwright without any page.waitForTimeout.

test(‘has title’, async ({ page }) => {
await page.goto(‘https://search.apps-staging.epo.org/’, { waitUntil: ‘networkidle’ });

await Promise.all([
page.locator(“//button[@data-testid=‘marker-search-button’]”).click(),
]);

page.waitForSelector(“//div[contains(@style, ‘display: block;’) and contains(@class, ‘Spinner__spinner’)]”, {visible:false});

});

BTW. If i remove the page.waitForTimeout(15000) i get the following error

Uncaught (in promise) GoError: clicking on “//button[@data-testid=‘marker-search-button’]”: execution context changed; most likely because of a navigation

Hi @pbains1,

We can detect the problem better if you could share us an HTML page similar to the one you are testing.

In the meantime, could you try the following:

await page.goto('https://search.apps-staging.epo.org/', {
    waitUntil: 'networkidle'
});

page.screenshot({ path: '../img/Start.png' });

await Promise.all([
    page.locator("//button[@data-testid='marker-search-button']").click(),
    page.waitForNavigation({ waitUntil: 'networkidle' }),
]);

// we don't have an option like `visible: false`. the selector should be visible to be waited.
page.waitForSelector("//div[contains(@style, 'display: block;') and contains(@class, 'Spinner__spinner')]"),
page.screenshot({ path: '../img/End.png' }),

check(page, {
    'header': page.locator('h2').textContent() == 'Ansera',
});

Thanks.

Hello,

I tried the script you sent. But it shuts down prematurely at
page.locator(“//button[@data-testid=‘marker-search-button’]”).click(),

with the following error:

ERRO[0006] communicating with browser: read tcp 127.0.0.1:21236->127.0.0.1:21235: wsarecv: An existing connection was forcibly closed by the remote host. category=cdp elapsed=“75 ms” goroutine=61
ERRO[0006] process with PID 6404 unexpectedly ended: exit status 1 category=browser elapsed=“55 ms” goroutine=58

I do not have a link to a similar application. I have created a video of the application and steps required in the script i.e.

  1. Navigate/logon to “https://search.apps-staging.epo.org/
  2. Click the “Re-run Marker” button

Is there anyway i can send you the video?