Skip to content

Commit 7b027c2

Browse files
committed
Add Page.waitForRequest
1 parent 1b7ced5 commit 7b027c2

File tree

3 files changed

+98
-0
lines changed

3 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
@@ -80,6 +80,7 @@ Page provides methods to interact with a single tab in a running web browser. A
8080
| [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. |
8181
| [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. |
8282
| [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. |
83+
| [waitForRequest(urlPattern[, options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforrequest/) | Waits for an HTTP request that matches the specified URL pattern. |
8384
| [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. |
8485
| [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. |
8586
| [waitForTimeout(timeout)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfortimeout) | Waits for the given `timeout` in milliseconds. |
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export default async function () {
8989

9090
### Related
9191

92+
- [page.waitForRequest()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforrequest/) - Wait for HTTP requests
9293
- [page.waitForNavigation()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfornavigation/) - Wait for navigation events
9394
- [page.waitForURL()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforurl/) - Wait for URL changes
9495
- [page.waitForLoadState()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforloadstate/) - Wait for load states

0 commit comments

Comments
 (0)