This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.

Documentationbreadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/browserbreadcrumb arrow Pagebreadcrumb arrow waitForEvent(event[, optionsOrPredicate])
Open source

waitForEvent(event[, optionsOrPredicate])

Waits for the specified event to be emitted. This is a generic method that can wait for any page event such as console, request, or response.

ParameterTypeDefaultDescription
eventstring-Required. Event name. Supported events: console, request, response.
optionsOrPredicatefunction | objectnullEither a predicate function or an options object. If a function, it will be used as the predicate.
options.predicatefunctionnullA function that returns true when the expected event is received. The event object is passed as argument.
options.timeoutnumber30000Maximum time in milliseconds. Pass 0 to disable the timeout. Default is overridden by the setDefaultTimeout option on BrowserContext or Page.

Returns

TypeDescription
PromiseA Promise that fulfills with the event data when the event is emitted. The return type depends on the event: ConsoleMessage for console, Request for request, Response for response.

Examples

Wait for console message

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

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

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

  try {
    // Set up the wait before triggering the action
    const consolePromise = page.waitForEvent('console');

    // Trigger action that causes a console message
    await page.evaluate(() => console.log('hello from page'));

    const msg = await consolePromise;
    console.log(`Console message: ${msg.text()}`);
  } finally {
    await page.close();
  }
}

Wait for response with predicate

JavaScript
import { browser } from 'k6/browser';
import { check } from 'k6';

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

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

  try {
    await page.goto('https://quickpizza.grafana.com/');

    // Wait for a specific response using a predicate function
    const responsePromise = page.waitForEvent('response', (res) =>
      res.url().includes('/api/pizza')
    );

    await page.getByRole('button', { name: /pizza/i }).click();

    const response = await responsePromise;

    check(response, {
      'response status is 200': (r) => r.status() === 200,
    });
  } finally {
    await page.close();
  }
}

Best practices

  1. Set up promise before trigger: Always set up the waitForEvent promise before triggering the action that causes the event:
JavaScript
// Correct
const eventPromise = page.waitForEvent('console');
await page.evaluate(() => console.log('test'));
const msg = await eventPromise;

// Incorrect - may miss the event
await page.evaluate(() => console.log('test'));
const msg = await page.waitForEvent('console');
  1. Use specific methods when available: For common use cases, prefer the more specific methods like waitForRequest or waitForResponse which provide URL pattern matching.