This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
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.
Handler Parameters
Request Object Properties
The handler receives a Request object with the following useful properties:
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:
// 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

