Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/latest/javascript-api/k6-browser/requestfinished.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/latest/javascript-api/k6-browser/requestfinished/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
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.
| Parameter | Type | Description |
|---|---|---|
| event | string | The event name: "requestfinished" |
| handler | function | A callback function that receives a Request object |
Handler Parameters
| Parameter | Type | Description |
|---|---|---|
| request | Request | The completed Request object |
Request Object Properties
The handler receives a Request object with the following useful properties:
| Property | Type | Description |
|---|---|---|
| request.url() | string | The URL of the request |
| request.method() | string | The HTTP method (GET, POST, PUT, DELETE, etc.) |
| request.resourceType() | string | The type of resource (document, stylesheet, image, etc.) |
| request.isNavigationRequest() | boolean | Whether this request is a navigation request |
Examples
Basic usage
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
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
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);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
- Use
Performance monitoring: This event is ideal for measuring actual request completion times and tracking network performance.
Related
- page.on() - Register handlers for page events
- page.on(“request”) - Subscribe to request initiation events
- page.on(“response”) - Subscribe to response start events
- page.on(“requestfailed”) - Subscribe to request failure events
- page.waitForEvent() - Wait for page events with predicate functions
- page.waitForRequest() - Wait for HTTP requests with URL pattern matching
- page.waitForResponse() - Wait for HTTP responses with URL pattern matching
- Request - Request object methods and properties
Was this page helpful?
Related resources from Grafana Labs

