Skip to content

Commit bffb234

Browse files
committed
Add waitForRequest to simulate-user-input-delay
1 parent 7b027c2 commit bffb234

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

docs/sources/k6/next/using-k6-browser/recommended-practices/simulate-user-input-delay.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ In the browser modules there are various asynchronous APIs that can be used to w
4141
| [page.waitForLoadState](#pagewaitforloadstate) | Waits for the specified page life cycle event. |
4242
| [page.waitForNavigation](#pagewaitfornavigation) | Waits for the navigation to complete after one starts. |
4343
| [page.waitForResponse](#pagewaitforresponse) | Wait for an HTTP response that matches the specified URL pattern. |
44+
| [page.waitForRequest](#pagewaitforrequest) | Wait for an HTTP request that matches the specified URL pattern. |
4445
| [page.waitForTimeout](#pagewaitfortimeout) | Waits the given time. _Use this instead of `sleep` in your frontend tests_. |
4546
| [page.waitForURL](#pagewaitforurl) | Wait for the page to navigate to the specified URL. |
4647
| [locator.waitFor](#locatorwaitfor) | Wait for the element to be in a particular state. |
@@ -277,6 +278,51 @@ export default async function () {
277278
}
278279
```
279280

281+
### page.waitForRequest
282+
283+
[page.waitForRequest](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitforrequest) 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.
284+
285+
```js
286+
import { browser } from 'k6/browser';
287+
import { check } from 'k6';
288+
289+
export const options = {
290+
scenarios: {
291+
ui: {
292+
executor: 'shared-iterations',
293+
options: {
294+
browser: {
295+
type: 'chromium',
296+
},
297+
},
298+
},
299+
},
300+
};
301+
302+
export default async function () {
303+
const page = await browser.newPage();
304+
305+
try {
306+
await page.goto('https://quickpizza.grafana.com/');
307+
308+
// Test waitForRequest with user interaction
309+
const pizzaRequestPromise = page.waitForRequest('https://quickpizza.grafana.com/api/pizza');
310+
311+
await page.getByRole('button', { name: /pizza/i }).click();
312+
313+
const pizzaRequest = await pizzaRequestPromise;
314+
315+
// Check that the pizza API request was initiated
316+
check(pizzaRequest, {
317+
'pizza API URL is correct': (r) => r.url() === 'https://quickpizza.grafana.com/api/pizza',
318+
'pizza API method is POST': (r) => r.method() === 'POST',
319+
});
320+
} finally {
321+
await page.close();
322+
}
323+
}
324+
```
325+
280326
### page.waitForTimeout
281327

282328
[page.waitForTimeout](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/waitfortimeout) will wait the given amount of time. It's functionally the same as k6's [sleep](#whatissleep), but it's asynchronous, which means it will not block the event loop and allows the background tasks to continue to be worked on.

0 commit comments

Comments
 (0)