|
| 1 | +--- |
| 2 | +title: 'waitForRequest(urlPattern[, options])' |
| 3 | +description: 'Browser module: page.waitForRequest(urlPattern[, options]) method' |
| 4 | +--- |
| 5 | + |
| 6 | +# waitForRequest(urlPattern[, options]) |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +| Parameter | Type | Default | Description | |
| 11 | +| ---------------- | -------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 12 | +| urlPattern | string \| RegExp | - | Required. URL or URL pattern to match against requests. Can be an exact URL string, a regular expression, or an empty string to match any request. | |
| 13 | +| options | object | `null` | | |
| 14 | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/). | |
| 15 | + |
| 16 | +### Returns |
| 17 | + |
| 18 | +| Type | Description | |
| 19 | +| ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | |
| 20 | +| Promise<[Request](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/request/)> | A Promise that fulfills with the [Request](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/request/) object when a URL matches and the request is initiated. | |
| 21 | + |
| 22 | +### Examples |
| 23 | + |
| 24 | +#### Wait for API request |
| 25 | + |
| 26 | +```javascript |
| 27 | +import { browser } from 'k6/browser'; |
| 28 | +import { check } from 'k6'; |
| 29 | + |
| 30 | +export const options = { |
| 31 | + scenarios: { |
| 32 | + ui: { |
| 33 | + executor: 'shared-iterations', |
| 34 | + options: { |
| 35 | + browser: { |
| 36 | + type: 'chromium', |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | +}; |
| 42 | + |
| 43 | +export default async function () { |
| 44 | + const page = await browser.newPage(); |
| 45 | + |
| 46 | + try { |
| 47 | + await page.goto('https://quickpizza.grafana.com/'); |
| 48 | + |
| 49 | + // Test waitForRequest with user interaction |
| 50 | + const pizzaRequestPromise = page.waitForRequest('https://quickpizza.grafana.com/api/pizza'); |
| 51 | + |
| 52 | + await page.getByRole('button', { name: /pizza/i }).click(); |
| 53 | + |
| 54 | + const pizzaRequest = await pizzaRequestPromise; |
| 55 | + |
| 56 | + // Check that the pizza API request was initiated |
| 57 | + check(pizzaRequest, { |
| 58 | + 'pizza API URL is correct': (r) => r.url() === 'https://quickpizza.grafana.com/api/pizza', |
| 59 | + 'pizza API method is POST': (r) => r.method() === 'POST', |
| 60 | + }); |
| 61 | + } finally { |
| 62 | + await page.close(); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### Best practices |
| 68 | + |
| 69 | +1. **Use appropriate patterns**: Choose the right matching method based on your needs: |
| 70 | + - Exact strings for known, static API endpoints |
| 71 | + - RegExp for pattern-based matching and dynamic URLs |
| 72 | + |
| 73 | +1. **Set up promise before trigger**: Always set up the `waitForRequest` promise before triggering the action that causes the request: |
| 74 | + |
| 75 | + <!-- md-k6:skip --> |
| 76 | + |
| 77 | + ```javascript |
| 78 | + // Correct |
| 79 | + const requestPromise = page.waitForRequest('/api/data'); |
| 80 | + await page.click('#submit'); |
| 81 | + const request = await requestPromise; |
| 82 | + |
| 83 | + // Incorrect - may miss the request |
| 84 | + await page.click('#submit'); |
| 85 | + const request = await page.waitForRequest('/api/data'); |
| 86 | + ``` |
| 87 | + |
| 88 | +1. **Verify request content**: After waiting for the request, verify that the request URL, method, and headers match your expectations. |
| 89 | + |
| 90 | +### Related |
| 91 | + |
| 92 | +- [page.waitForResponse()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforresponse/) - Wait for HTTP responses |
| 93 | +- [page.waitForNavigation()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfornavigation/) - Wait for navigation events |
| 94 | +- [page.waitForURL()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforurl/) - Wait for URL changes |
| 95 | +- [page.waitForLoadState()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforloadstate/) - Wait for load states |
| 96 | +- [Request](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/request/) - Request object methods and properties |
0 commit comments