|
| 1 | +--- |
| 2 | +title: 'waitForResponse(urlPattern[, options])' |
| 3 | +description: 'Browser module: page.waitForResponse(urlPattern[, options]) method' |
| 4 | +--- |
| 5 | + |
| 6 | +# waitForResponse(urlPattern[, options]) |
| 7 | + |
| 8 | +Waits for an HTTP response that matches the specified URL pattern. This method is particularly useful for waiting for responses from AJAX/fetch requests, API calls, or specific resources to be loaded before proceeding with the test. |
| 9 | + |
| 10 | +| Parameter | Type | Default | Description | |
| 11 | +| ---------------- | -------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 12 | +| urlPattern | string \| RegExp | - | Required. URL or URL pattern to match against responses. Can be an exact URL string, a regular expression, or an empty string to match any response. | |
| 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<[Response](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/response/)> | A Promise that fulfills with the [Response](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/response/) object when a URL matches and the response is received. | |
| 21 | + |
| 22 | +### Examples |
| 23 | + |
| 24 | +#### Wait for API response |
| 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 waitForResponse with user interaction |
| 50 | + const pizzaResponsePromise = page.waitForResponse('https://quickpizza.grafana.com/api/pizza'); |
| 51 | + |
| 52 | + await page.getByRole('button', { name: /pizza/i }).click(); |
| 53 | + |
| 54 | + const pizzaResponse = await pizzaResponsePromise; |
| 55 | + |
| 56 | + // Check that the pizza API call was successful |
| 57 | + check(pizzaResponse, { |
| 58 | + 'pizza API status is 200': (r) => r.status() === 200, |
| 59 | + 'pizza API URL is correct': (r) => r.url() === 'https://quickpizza.grafana.com/api/pizza', |
| 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 `waitForResponse` promise before triggering the action that causes the request: |
| 74 | + |
| 75 | + <!-- md-k6:skip --> |
| 76 | + |
| 77 | + ```javascript |
| 78 | + // Correct |
| 79 | + const responsePromise = page.waitForResponse('/api/data'); |
| 80 | + await page.click('#submit'); |
| 81 | + const response = await responsePromise; |
| 82 | + |
| 83 | + // Incorrect - may miss the response |
| 84 | + await page.click('#submit'); |
| 85 | + const response = await page.waitForResponse('/api/data'); |
| 86 | + ``` |
| 87 | + |
| 88 | +1. **Verify response content**: After waiting for the response, verify that the response status and content match your expectations. |
| 89 | + |
| 90 | +### Related |
| 91 | + |
| 92 | +- [page.waitForNavigation()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfornavigation/) - Wait for navigation events |
| 93 | +- [page.waitForURL()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforurl/) - Wait for URL changes |
| 94 | +- [page.waitForLoadState()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforloadstate/) - Wait for load states |
| 95 | +- [Response](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/response/) - Response object methods and properties |
0 commit comments