Wait is not working

Hi Team,

Came across a problem where none of the wait is working-
sleep(30)
waitForLoadState(‘domcontentloaded’)
waitForTimeout(10000);
waitForNavigation({ waitUntil: ‘networkidle’ })

I have written a script where after clicking a link it opens other page which take some time to load…but after clicking on like, after loading some content of page, browser abnormal close with below error -

communicating with browser: websocket: close 1006 (abnormal closure): unexpected EOF  category=cdp elapsed="4899 ms" goroutine=184 iteration_id=ceea2a010e6aaa67

the last command shown in logs before it crash -

fid:1D04C5F52146B3CA7D4DDA0EFEC25BCA furl:"pageurl" rid:31124.325  category="Frame:deleteRequest" elapsed="0 ms" goroutine=577 iteration_id=ceea2a010e6aaa67

in order to manage that first I used all types of wait but nothing work, so finally I used sleep() and it worked-

page.waitForSelector(xpath),
        page.hover(xpath)
        page.waitForNavigation(),
        page.$(xpath).click(),
        sleep(30)
        page.waitForSelector(xpath)

Now I have build k6 using main branch as -

xk6 build --output xk6-browser --with github.com/grafana/xk6-browser@main

and now converted script as -

 await page.hover(xpath)
  await page.locator(xpath).click()
  //sleep(30)
  //await page.waitForLoadState('domcontentloaded')
  //await page.waitForTimeout(10000);
  //await page.waitForNavigation({ waitUntil: 'networkidle' })
  await page.waitForSelector(xpath)
  await page.locator(xpath).click(),

here I tried all the waits but nothing is working and browser is crashing with same error -

communicating with browser: websocket: close 1006 (abnormal closure): unexpected EOF  category=cdp elapsed="4899 ms" goroutine=184 iteration_id=ceea2a010e6aaa67

and the last command shown in logs before it crash -

fid:1D04C5F52146B3CA7D4DDA0EFEC25BCA furl:"pageurl" rid:31124.325  category="Frame:deleteRequest" elapsed="0 ms" goroutine=577 iteration_id=ceea2a010e6aaa67

Hi @vish_s02,

I’m unable to reproduce the issue. If you can paste the full test script (pointing to a publicly accessible website) then that could help me reproduce the issue.

This is the script that I ran, which worked for me:

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

export default async function () {
  const browser = chromium.launch({headless: false});
  const context = browser.newContext();
  const page = context.newPage();

  try {
    await page.goto('https://test.k6.io/flip_coin.php');
    
    const xpath = "input[value='Bet on heads!']";

    await page.hover(xpath);
    
    await Promise.all([
      page.waitForNavigation({ waitUntil: 'networkidle' }),
      page.locator(xpath).click()
    ]);
    
    await page.waitForSelector(xpath);

    await Promise.all([
      page.waitForNavigation({ waitUntil: 'networkidle' }),
      page.locator(xpath).click()
    ]);
  } finally {
    page.close();
    browser.close();
  }
}

Can you try this script and let me know how it goes?

Cheers,
Ankur