Skip to content

Commit 4159434

Browse files
committed
Add page.waitForResponse doc
1 parent 8bbc3b3 commit 4159434

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

docs/sources/k6/next/javascript-api/k6-browser/page/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Page provides methods to interact with a single tab in a running web browser. A
7878
| [waitForFunction(pageFunction, arg[, options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforfunction/) | Returns when the `pageFunction` returns a truthy value. |
7979
| [waitForLoadState(state[, options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforloadstate/) {{< docs/bwipt id="880" >}} | Waits for the given load `state` to be reached. |
8080
| [waitForNavigation([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfornavigation/) | Waits for the given navigation lifecycle event to occur and returns the main resource response. |
81+
| [waitForResponse(urlPattern[, options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforresponse/) | Waits for an HTTP response that matches the specified URL pattern. |
8182
| [waitForSelector(selector[, options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforselector/) | Returns when element specified by selector satisfies `state` option. |
8283
| [waitForTimeout(timeout)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfortimeout) | Waits for the given `timeout` in milliseconds. |
8384
| [waitForURL(url[, options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforurl/) | Waits for the page to navigate to the specified URL. |
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 resource loads to complete before proceeding with the test.
9+
10+
<TableWithNestedRows>
11+
12+
| Parameter | Type | Default | Description |
13+
| ---------------- | -------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
14+
| 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. |
15+
| options | object | `null` | |
16+
| 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/). |
17+
18+
</TableWithNestedRows>
19+
20+
### Returns
21+
22+
| Type | Description |
23+
| ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
24+
| 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 recieved. |
25+
26+
### Examples
27+
28+
#### Wait for API response
29+
30+
```javascript
31+
import { browser } from 'k6/browser';
32+
import { check } from 'k6';
33+
34+
export const options = {
35+
scenarios: {
36+
ui: {
37+
executor: 'shared-iterations',
38+
options: {
39+
browser: {
40+
type: 'chromium',
41+
},
42+
},
43+
},
44+
},
45+
};
46+
47+
export default async function () {
48+
const page = await browser.newPage();
49+
50+
try {
51+
await page.goto('https://quickpizza.grafana.com/');
52+
53+
// Test waitForResponse with user interaction
54+
const pizzaResponsePromise = page.waitForResponse('https://quickpizza.grafana.com/api/pizza');
55+
56+
await page.getByRole('button', { name: /pizza/i }).click();
57+
58+
const pizzaResponse = await pizzaResponsePromise;
59+
60+
// Check that the pizza API call was successful
61+
check(pizzaResponse, {
62+
'pizza API status is 200': (r) => r.status() === 200,
63+
'pizza API URL is correct': (r) => r.url() === 'https://quickpizza.grafana.com/api/pizza',
64+
});
65+
} finally {
66+
await page.close();
67+
}
68+
}
69+
```
70+
71+
### Best practices
72+
73+
1. **Use appropriate patterns**: Choose the right matching method based on your needs:
74+
- Exact strings for known, static API endpoints
75+
- RegExp for pattern-based matching and dynamic URLs
76+
77+
2. **Set up promise before trigger**: Always set up the `waitForResponse` promise before triggering the action that causes the request:
78+
79+
```javascript
80+
// Correct
81+
const responsePromise = page.waitForResponse('/api/data');
82+
await page.click('#submit');
83+
const response = await responsePromise;
84+
85+
// Incorrect - may miss the response
86+
await page.click('#submit');
87+
const response = await page.waitForResponse('/api/data');
88+
```
89+
90+
3. **Verify response content**: After waiting for the response, verify that the response status and content match your expectations.
91+
92+
### Related
93+
94+
- [page.waitForNavigation()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfornavigation/) - Wait for navigation events
95+
- [page.waitForURL()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforurl/) - Wait for URL changes
96+
- [page.waitForLoadState()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforloadstate/) - Wait for load states
97+
- [Response](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/response/) - Response object methods and properties

0 commit comments

Comments
 (0)