This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
waitForRequest(urlPattern[, options])
Waits for an HTTP request that matches the specified URL pattern. This method is particularly useful for waiting for requests to be initiated before proceeding with the test, such as verifying that form submissions or API calls are triggered.
Returns
Examples
Wait for API request
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/');
// Test waitForRequest with user interaction
const pizzaRequestPromise = page.waitForRequest('https://quickpizza.grafana.com/api/pizza');
await page.getByRole('button', { name: /pizza/i }).click();
const pizzaRequest = await pizzaRequestPromise;
// Check that the pizza API request was initiated
check(pizzaRequest, {
'pizza API URL is correct': (r) => r.url() === 'https://quickpizza.grafana.com/api/pizza',
'pizza API method is POST': (r) => r.method() === 'POST',
});
} finally {
await page.close();
}
}Best practices
Use appropriate patterns: Choose the right matching method based on your needs:
- Exact strings for known, static API endpoints
- RegExp for pattern-based matching and dynamic URLs
Set up promise before trigger: Always set up the
waitForRequestpromise before triggering the action that causes the request:
// Correct
const requestPromise = page.waitForRequest('/api/data');
await page.click('#submit');
const request = await requestPromise;
// Incorrect - may miss the request
await page.click('#submit');
const request = await page.waitForRequest('/api/data');- Verify request content: After waiting for the request, verify that the request URL, method, and headers match your expectations.
Related
- page.waitForResponse() - Wait for HTTP responses
- page.waitForNavigation() - Wait for navigation events
- page.waitForURL() - Wait for URL changes
- page.waitForLoadState() - Wait for load states
- Request - Request object methods and properties


