grafana / xk6-browser

The browser module adds support for browser automation and end-to-end web testing via the Chrome Devtools Protocol to k6.
https://grafana.com/docs/k6/latest/javascript-api/k6-browser/
GNU Affero General Public License v3.0
341 stars 42 forks source link

First element (0) of `pages` from `BrowserContext` is `undefined` but the second element (1) is correctly defined #444

Open ankur22 opened 2 years ago

ankur22 commented 2 years ago

Tested against: dbede120c63df43995813a847a25b0e66e289592

It seems that the array does not start at index 0, and instead starts at index 1. In PW it correctly starts at 0. I think there's an underlying issue which is causing that should be fixed.

NOTE: In the code below it works against localhost:8080, which is a test server. It can be found in the following link along with Instructions on how to run it locally: https://github.com/ankur22/testserver.

Script:

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

export default function () {
  const browser = launcher.launch('chromium', {
      headless: false,
  });
  const context = browser.newContext();
  context.newPage();
  const pages = context.pages();
  const res = pages[0].goto('http://localhost:8080/unprotected');

  sleep(10);
  browser.close();
}

PW script:

// @ts-check
const { test, chromium } = require('@playwright/test');

test.describe('Editing', () => {
  test('Type login creds', async () => {
    const browser = await chromium.launch({ headless: false, slowMo: 500 });
    const ctx = await browser.newContext();
    await ctx.newPage();
    const pages = ctx.pages();
    await pages[0].goto('http://localhost:8080/unprotected');

    await new Promise(resolve => setTimeout(resolve, 10000));

    await browser.close();
  });
});
ankur22 commented 4 months ago

I found another issue when working on a fix for https://github.com/grafana/xk6-browser/issues/827. It seems that pages on browserContext will not guarantee that the 0th page will be the first page the test opened. In the test i'm writing in https://github.com/grafana/xk6-browser/pull/1334 i've had to account for this unexpected behaviour.