Weird uncaught (in promise) error from using `page.url()`

Hi all.

I’m currently experiencing this error. Moreover, I cannot reproduce it as it is randomly occurred.

Uncaught (in promise) GoError: getting page URL: evaluating JS: Cannot find context with specified id running at github.com/grafana/xk6-browser/common.(*Page).URL-fm (native)

And the code of the error can be seen below.

    const waitingRoundThreshold = 60;
    let currentWaitingRound = 0;
    while ((await page.url()) !== urlStr)) { // Here is where the error occurred.
      if (currentWaitingRound > waitingRoundThreshold) {
        fail("Page did not load within 60 seconds. This iteration has been halted.");
      }
      console.log("Waiting for the page to be loaded...");
      currentWaitingRound++;
      sleep(1); // Wait for 1 second and check again
    }
    const checkbox1 = page.locator('#checkbox1');
    const checkbox2 = page.locator('#checkbox2');
    const checkbox3 = page.locator('#checkbox3');

    console.log("Waiting for all checkboxes to visible.");
    await checkbox1.waitFor({ state: 'visible' });
    await checkbox2.waitFor({ state: 'visible' });
    await checkbox3.waitFor({ state: 'visible' });

    if (await checkbox1.isVisible() &&
      await checkbox2.isVisible() &&
      await checkbox3.isVisible()
    ) {
      console.log("Check all the checkboxes.");
      await checkbox1.click({ force: true });
      await checkbox2.click({ force: true });
      await checkbox3.click({ force: true });
    }

    const nextButtonSelector = '#nextBtn';
    await Promise.all([
      page.waitForNavigation({ waitUntil: 'networkidle' }),
      page.locator(nextButtonSelector ).waitFor({ state: 'visible' }),
      console.log("Click next button"),
      page.locator(nextButtonSelector ).click()
    ]);

As you can see, it’s a super simple script that wait for the checkbox to show up and then checking on it and proceed to the next step.

This code is working most of the time, but somehow it was managed to randomly show the error that I mentioned above, which cause the end of the iteration.

I found this error since I was starting test the script on the test runner that I was hosted on the cloud instance as it runs on multiple VUs, but this issue is not about running the test on the cloud because I also experiencing this error while I’m working on the script on the local with 1 VU and 1 iteration on shared-iteration mode, so this issue is clearly about the script itself, not the environment.

For the testing site, it was located behind the Cloudflare firewall as it is a dev site, so before testing it we need to add the tester IP address to its whitelist, so that the tester can access the site.

The question is, what is the possible root cause for this issue? is it because the script, or the site that we test might be having some mechanism that possible trigger this error?

Thank you!

Hi @paritwai,

Did you try waiting for the page to load before checking its URL?

From the example here:

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

  try {
    await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });
    await Promise.all([
      page.waitForNavigation(),
      page.locator('a[href="/my_messages.php"]').click(),
    ]);
  ...
}

Hi, thank you for the reply.

I forgot to tell that the sample code was a function that was called after another function because the user on this web application needs to complete the step by step, so it cannot redirect to a specific page directly.

I also found that I got the same error from other code blocks as a result of the k6 browser was suddenly closed before it was processed.

Is there any ways to investigate the deeper errors in case of the k6 browser was crashed or closed before the script is actually completed?

Finally, I got the solution by just using await page.waitForNavigation() instead of the while loop with await page.url()

However, at the implementation time, I also tried to use await page.waitForNavigation(), but it did not work, that’s why I have to do the while loop for waiting await page.url() until it shows the right URL, but not sure why it is working now.

My finding about this issue is do not use await page.url() within the while loop because it could cause the k6 browser to crash while running the test for some reason.

If you’re encountering an uncaught (in promise) error when using page.url(), it could be due to the timing of when the method is called. Ensure that the promise is resolved before attempting to call page.url(). This might happen if you’re trying to access the URL before the page has fully loaded or if there’s a race condition in your code. You can try using await with page.waitForNavigation() or ensure the page is fully loaded by checking for specific elements before fetching the URL. For more troubleshooting tips, visit Hostingmella.