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

Open source

page.on(“requestfinished”)

Subscribe to the requestfinished event, which fires when a network request successfully completes (receives a response).

This event is triggered after the response is fully received, making it useful for tracking completed network activity, measuring request durations, or collecting statistics about successful requests.

ParameterTypeDescription
eventstringThe event name: "requestfinished"
handlerfunctionA callback function that receives a Request object

Handler Parameters

ParameterTypeDescription
requestRequestThe completed Request object

Request Object Properties

The handler receives a Request object with the following useful properties:

PropertyTypeDescription
request.url()stringThe URL of the request
request.method()stringThe HTTP method (GET, POST, PUT, DELETE, etc.)
request.resourceType()stringThe type of resource (document, stylesheet, image, etc.)
request.isNavigationRequest()booleanWhether this request is a navigation request

Examples

Basic usage

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();

  // Track all completed requests
  const finishedRequests = [];

  page.on('requestfinished', (request) => {
    finishedRequests.push({
      url: request.url(),
      method: request.method(),
      resourceType: request.resourceType(),
    });

    console.log(`✓ Request finished: ${request.method()} ${request.url()}`);
  });

  await page.goto('https://test.k6.io/');

  console.log(`Total requests completed: ${finishedRequests.length}`);

  await page.close();
}

Tracking API requests

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();

  // Track API requests specifically
  const apiRequests = [];

  page.on('requestfinished', (request) => {
    if (request.url().includes('/api/')) {
      apiRequests.push({
        url: request.url(),
        method: request.method(),
        timestamp: new Date().toISOString(),
      });
    }
  });

  // Navigate to a page that might trigger API calls
  await page.goto('https://test.k6.io/');

  // Wait for a bit to allow any API requests to complete
  await page.waitForTimeout(2000);

  console.log(`API requests completed: ${apiRequests.length}`);

  await page.close();
}

Best practices

  1. Setup before navigation: Always set up the event listener before performing actions that trigger network requests:

    JavaScript
    // Correct
    page.on('requestfinished', handler);
    await page.goto('https://example.com');
    
    // Incorrect - may miss early requests
    await page.goto('https://example.com');
    page.on('requestfinished', handler);
  2. Distinguish from other events:

    • Use page.on('request') to track when requests are initiated
    • Use page.on('response') to track when responses start arriving
    • Use page.on('requestfinished') to track when requests completely finish
  3. Performance monitoring: This event is ideal for measuring actual request completion times and tracking network performance.