K6 browser is not able to detect new pages

I’m clicking a link on a webpage which opens a new page. k6 is not able to detect the new page. Though context.pages().length returns 2, it doesn’t identify any page other than the original page. So it is not able to navigate & operate on the new page.
Following code snippet can be used to experiment with this.
Can anyone guide if I’m missing something here?

  const page = browser.newPage();
  
  let baseUrl = "https://httpbin.org/"
  await page.goto(baseUrl, {waitUntil: 'load'});
  Logger.info("Home Page title:",page.title())

  const newLinkSelector = page.locator("a[href='https://kennethreitz.org']");
  Logger.info(newLinkSelector.innerText())
  newLinkSelector.click()
  sleep(5)
   
  console.log("Page Count:",context.pages().length);
  const pages = browser.context().pages()
  for (let i in pages){
    console.log(`Title${i}`, pages[i].title())
  }

I am using k6 version k6.exe v0.49.0 (commit/b5328aa782, go1.21.6, windows/amd64)

Hi @sumitbhowmick,

Thanks for bringing this to our attention. Unfortunately it’s a bug in the k6 browser module where by the new tab doesn’t open. We have an open issue for this: Clicking on a link that opens a new tab never navigates to the href link · Issue #827 · grafana/xk6-browser · GitHub which you can follow to see its progress. Please like or add more details to the issue which will help get it prioritised.

Cheers,
Ankur

Hi @ankur , the status of Clicking on a link that opens a new tab never navigates to the href link · Issue #827 · grafana/xk6-browser · GitHub says the issue was fixed by v0.52.0 release from a few days ago. But I am still having the exact same issue @sumitbhowmick reported here in this post.

Hi @ankur, can we get an update for this post? Thanks.

Hi @yli9mix,

Sorry for delay in getting back to you. Do you have a test script that points to a publicly accessible website that we can test against to replicate the incorrect behaviour you are seeing? Are you working with the test script that @sumitbhowmick posted initially?

I’ve amended the script to work with the latest non-experimental browser module API in k6 v0.53.0 (NOTE: the experimental API is deprecated and will be removed very soon):

import { browser } from 'k6/browser';
import { sleep } from 'k6';

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      iterations: 1,
      vus: 1,
      options: {
        browser: {
          type: 'chromium'
        },
      },
    },
  },
}

export default async function () {
  const page = await browser.newPage();
  
  let baseUrl = "https://httpbin.org/"
  await page.goto(baseUrl, {waitUntil: 'load'});

  const newLinkSelector = page.locator("a[href='https://kennethreitz.org']");
  await newLinkSelector.click()
  sleep(5)
   
  const pages = browser.context().pages()
  for (let i in pages){
    console.log(`Title${i}`, pages[i].title())
  }
}

The new tab is opened and the navigation occurs.

Best,
Ankur