Skip to content

Commit cdcd591

Browse files
committed
Add code review changes
- Examples updated to work with all route methods against quickpizza.
1 parent 4f0f969 commit cdcd591

File tree

5 files changed

+172
-81
lines changed

5 files changed

+172
-81
lines changed

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

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ description: 'Browser module: page.route(url, handler) method'
55

66
# route(url, handler)
77

8-
The method adds a route that allows modifying network requests matching the provided url. The handler is a function taking a [Route](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/) input that provides functions to continue, fulfill or abort the request. Once routing is enabled, every request matching the url pattern will stall unless one of these functions is called.
8+
The method adds a route that allows modifying network requests matching the provided url. The handler is a function taking a [Route](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/) input that provides functions to [continue](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/continue), [fulfill](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/fulfill) or [abort](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/abort) the request. Once routing is enabled, every request matching the url pattern will stall unless one of these functions is called.
99

1010
When several routes match the given pattern, they run in the order opposite to their registration. That way the last registered route can always override all the previous ones.
1111

12-
| Parameter | Type | Default | Description |
13-
| --------- | ------ | ------- | --------------------------------------------- |
14-
| url | string or Regexp | `''` | URL to match during routing. |
15-
| handler | function([Route](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/)) | null | Handler function executed when routing the request. |
12+
| Parameter | Type | Default | Description |
13+
| --------- | -------------------------------------------------------------------------------------------- | ------- | --------------------------------------------------- |
14+
| url | string or Regexp | `''` | URL to match during routing. |
15+
| handler | function([Route](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/)) | null | Handler function executed when routing the request. |
1616

1717
### Returns
1818

19-
| Type | Description |
20-
| --------------- | ------------------------------------------------------------------------------- |
21-
| `Promise<void>` | A Promise that fulfills when the route is added to the page. |
22-
19+
| Type | Description |
20+
| --------------- | ------------------------------------------------------------ |
21+
| `Promise<void>` | A Promise that fulfills when the route is added to the page. |
2322

2423
### Example
2524

@@ -47,9 +46,43 @@ export default async function () {
4746
// Abort all images requests
4847
await page.route(/(\.png$)|(\.jpg$)/, async function (route) {
4948
await route.abort();
50-
})
49+
});
50+
51+
// Fulfill request with the following response which
52+
// changes the quotes displayed on the page
53+
await page.route(/.*\/quotes$/, async function (route) {
54+
await route.fulfill({
55+
body: JSON.stringify({
56+
quotes: ['"We ❤️ pizza" - k6 team'],
57+
}),
58+
});
59+
});
60+
61+
// Change the pizza request when the button is clicked
62+
await page.route(/.*\/pizza$/, async function (route) {
63+
await route.continue({
64+
headers: {
65+
...route.request().headers(),
66+
foo: 'bar',
67+
},
68+
method: 'POST',
69+
postData: JSON.stringify({
70+
maxCaloriesPerSlice: 500,
71+
mustBeVegetarian: true,
72+
excludedIngredients: ['Pineapple'],
73+
excludedTools: ['Knife', 'Scissors'],
74+
maxNumberOfToppings: 1,
75+
minNumberOfToppings: 1,
76+
customName: 'Classic Pizza',
77+
}),
78+
});
79+
});
5180

5281
await page.goto('https://quickpizza.grafana.com/');
82+
83+
await page.getByRole('button', { name: 'Pizza, Please!' }).click();
84+
85+
await page.close();
5386
}
5487
```
5588

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ When several routes match the given pattern, they run in the order opposite to t
1212

1313
## Supported APIs
1414

15-
| Method | Description |
16-
| ---------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
17-
| [abort([errorCode])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/abort) | Aborts the route's request. |
18-
| [continue([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/continue) | Continues the request with optional overrides. |
19-
| [fulfill([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/fulfill) | Fulfills the request with the given response |
20-
| [request()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/request) | Returns the matching [Request](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/request) object. |
15+
| Method | Description |
16+
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
17+
| [abort([errorCode])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/abort) | Aborts the route's request. |
18+
| [continue([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/continue) | Continues the request with optional overrides. |
19+
| [fulfill([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/fulfill) | Fulfills the request with the given response |
20+
| [request()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/request) | Returns the matching [Request](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/request) object. |
2121

2222
### Example
2323

@@ -45,9 +45,44 @@ export default async function () {
4545
// Abort all images requests
4646
await page.route(/(\.png$)|(\.jpg$)/, async function (route) {
4747
await route.abort();
48-
})
48+
});
49+
50+
// Fulfill request with the following response which
51+
// changes the quotes displayed on the page
52+
await page.route(/.*\/quotes$/, async function (route) {
53+
await route.fulfill({
54+
body: JSON.stringify({
55+
quotes: ['"We ❤️ pizza" - k6 team'],
56+
}),
57+
});
58+
});
59+
60+
// Change the pizza request when the button is clicked
61+
await page.route(/.*\/pizza$/, async function (route) {
62+
await route.continue({
63+
headers: {
64+
...route.request().headers(),
65+
foo: 'bar',
66+
},
67+
method: 'POST',
68+
postData: JSON.stringify({
69+
maxCaloriesPerSlice: 500,
70+
mustBeVegetarian: true,
71+
excludedIngredients: ['Pineapple'],
72+
excludedTools: ['Knife', 'Scissors'],
73+
maxNumberOfToppings: 1,
74+
minNumberOfToppings: 1,
75+
customName: 'Classic Pizza',
76+
}),
77+
});
78+
});
4979

5080
await page.goto('https://quickpizza.grafana.com/');
81+
82+
await page.getByRole('button', { name: 'Pizza, Please!' }).click();
83+
84+
await page.close();
85+
}
5186
```
5287

5388
{{< /code >}}

docs/sources/k6/next/javascript-api/k6-browser/route/abort.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
---
2-
title: 'abort(errorCode)'
2+
title: 'abort([errorCode])'
33
description: 'Browser module: Route.abort method'
44
---
55

6-
# abort(errorCode)
6+
# abort([errorCode])
77

88
Aborts the request with the given error code.
99

10-
| Parameter | Type | Default | Description |
11-
| ------------------- | ---------------- | ---------- | -------------------------------------------------------------------- |
12-
| errorCode | string | `'failed'` | Error code when aborting the request. Can be one of the following: `'aborted'`, `'accessdenied'`, `'addressunreachable'`, `'blockedbyclient'`, `'blockedbyresponse'`, `'connectionaborted'`,`'connectionclosed'`, `'connectionfailed'`, `'connectionrefused'`, `'connectionreset'`, `'internetdisconnected'`, `'namenotresolved'`, `'timedout'`, `'failed'`. |
10+
| Parameter | Type | Default | Description |
11+
| --------- | ------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
12+
| errorCode | string | `'failed'` | Error code when aborting the request. Can be one of the following: `'aborted'`, `'accessdenied'`, `'addressunreachable'`, `'blockedbyclient'`, `'blockedbyresponse'`, `'connectionaborted'`,`'connectionclosed'`, `'connectionfailed'`, `'connectionrefused'`, `'connectionreset'`, `'internetdisconnected'`, `'namenotresolved'`, `'timedout'`, `'failed'`. |
1313

1414
### Returns
1515

16-
| Type | Description |
17-
| --------------- | ------------------------------------------------------------------------------- |
18-
| `Promise<void>` | A Promise that fulfills when the request is aborted. |
16+
| Type | Description |
17+
| --------------- | ---------------------------------------------------- |
18+
| `Promise<void>` | A Promise that fulfills when the request is aborted. |
1919

2020
### Example
2121

@@ -42,10 +42,13 @@ export default async function () {
4242

4343
// Abort all images requests
4444
await page.route(/(\.png$)|(\.jpg$)/, async function (route) {
45-
await route.abort();
46-
})
45+
await route.abort('blockedbyclient');
46+
});
4747

4848
await page.goto('https://quickpizza.grafana.com/');
49+
50+
await page.close();
51+
}
4952
```
5053

5154
{{< /code >}}

docs/sources/k6/next/javascript-api/k6-browser/route/continue.md

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
---
2-
title: 'continue(options)'
2+
title: 'continue([options])'
33
description: 'Browser module: Route.continue method'
44
---
55

6-
# continue(options)
6+
# continue([options])
7+
8+
{{< admonition type="caution" >}}
9+
10+
This method has **known issues**, but it does work as intended. For details, refer to [#5012](https://github.com/grafana/k6/issues/5012).
11+
12+
{{< /admonition >}}
713

814
Sends the request to the network with optional overrides.
915

10-
| Parameter | Type | Default | Description |
11-
| ------------------- | ---------------- | ------- | -------------------------------------------------------------------- |
12-
| options | object | null | |
13-
| options.headers | object | null | Request headers. |
14-
| options.method | string | `''` | Request method (e.g. GET or POST). |
15-
| options.postData | string or Buffer | `''` | Post data of the request. |
16-
| options.url | string | `''` | Request URL. |
16+
| Parameter | Type | Default | Description |
17+
| ---------------- | ---------------- | ------- | ---------------------------------- |
18+
| options | object | null | |
19+
| options.headers | object | null | Request headers. |
20+
| options.method | string | `''` | Request method (e.g. GET or POST). |
21+
| options.postData | string or Buffer | `''` | Post data of the request. |
22+
| options.url | string | `''` | Request URL. |
1723

1824
### Returns
1925

20-
| Type | Description |
21-
| --------------- | ------------------------------------------------------------------------------- |
22-
| `Promise<void>` | A Promise that fulfills when the request is resumed. |
26+
| Type | Description |
27+
| --------------- | ---------------------------------------------------- |
28+
| `Promise<void>` | A Promise that fulfills when the request is resumed. |
2329

2430
### Example
2531

@@ -30,7 +36,7 @@ import { browser } from 'k6/browser';
3036

3137
export const options = {
3238
scenarios: {
33-
ui: {
39+
browser: {
3440
executor: 'shared-iterations',
3541
options: {
3642
browser: {
@@ -44,19 +50,31 @@ export const options = {
4450
export default async function () {
4551
const page = await browser.newPage();
4652

47-
try {
48-
await page.route(/.*\/api\/pizza/, async function (route) {
49-
await route.continue({
50-
postData: JSON.stringify({
51-
customName: 'My Pizza',
52-
}),
53-
});
53+
// Change the pizza request when the button is clicked
54+
await page.route(/.*\/pizza$/, async function (route) {
55+
await route.continue({
56+
headers: {
57+
...route.request().headers(),
58+
foo: 'bar',
59+
},
60+
method: 'POST',
61+
postData: JSON.stringify({
62+
maxCaloriesPerSlice: 500,
63+
mustBeVegetarian: true,
64+
excludedIngredients: ['Pineapple'],
65+
excludedTools: ['Knife', 'Scissors'],
66+
maxNumberOfToppings: 1,
67+
minNumberOfToppings: 1,
68+
customName: 'Classic Pizza',
69+
}),
5470
});
71+
});
72+
73+
await page.goto('https://quickpizza.grafana.com/');
74+
75+
await page.getByRole('button', { name: 'Pizza, Please!' }).click();
5576

56-
await page.goto('https://quickpizza.grafana.com/');
57-
} finally {
58-
await page.close();
59-
}
77+
await page.close();
6078
}
6179
```
6280

docs/sources/k6/next/javascript-api/k6-browser/route/fulfill.md

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,27 @@ description: 'Browser module: Route.fulfill method'
55

66
# fulfill(options)
77

8+
{{< admonition type="caution" >}}
9+
10+
This method has **known issues**, but it does work as intended. For details, refer to [#5012](https://github.com/grafana/k6/issues/5012).
11+
12+
{{< /admonition >}}
13+
814
Fulfills the request with the given response.
915

10-
| Parameter | Type | Default | Description |
11-
| ------------------- | ---------------- | ------- | -------------------------------------------------------------------- |
12-
| options | object | null | |
13-
| options.body | string or Buffer | `''` | Response body. |
14-
| options.contentType | string | `''` | Same as setting `Content-Type` response header. |
15-
| options.headers | object | null | Response headers. |
16-
| options.status | number | `200` | Response status code. |
16+
| Parameter | Type | Default | Description |
17+
| ------------------- | ---------------- | ------- | ----------------------------------------------- |
18+
| options | object | null | |
19+
| options.body | string or Buffer | `''` | Response body. |
20+
| options.contentType | string | `''` | Same as setting `Content-Type` response header. |
21+
| options.headers | object | null | Response headers. |
22+
| options.status | number | `200` | Response status code. |
1723

1824
### Returns
1925

20-
| Type | Description |
21-
| --------------- | ------------------------------------------------------------------------------- |
22-
| `Promise<void>` | A Promise that fulfills when the request is fulfilled. |
23-
26+
| Type | Description |
27+
| --------------- | ------------------------------------------------------ |
28+
| `Promise<void>` | A Promise that fulfills when the request is fulfilled. |
2429

2530
### Example
2631

@@ -31,7 +36,7 @@ import { browser } from 'k6/browser';
3136

3237
export const options = {
3338
scenarios: {
34-
ui: {
39+
browser: {
3540
executor: 'shared-iterations',
3641
options: {
3742
browser: {
@@ -46,24 +51,21 @@ export default async function () {
4651
const page = await browser.newPage();
4752

4853
try {
49-
await page.route(
50-
"https://jsonplaceholder.typicode.com/todos/1",
51-
async function (route) {
52-
await route.fulfill({
53-
status: 200,
54-
body: JSON.stringify({
55-
"id": 1,
56-
"title": "Test Todo",
57-
"completed": false
58-
}),
59-
contentType: "application/json",
60-
headers: {
61-
"Access-Control-Allow-Origin": "*",
62-
"Access-Control-Allow-Credentials": "true",
63-
},
64-
});
65-
}
66-
);
54+
await page.route('https://jsonplaceholder.typicode.com/todos/1', async function (route) {
55+
await route.fulfill({
56+
status: 200,
57+
body: JSON.stringify({
58+
id: 1,
59+
title: 'Test Todo',
60+
completed: false,
61+
}),
62+
contentType: 'application/json',
63+
headers: {
64+
'Access-Control-Allow-Origin': '*',
65+
'Access-Control-Allow-Credentials': 'true',
66+
},
67+
});
68+
});
6769

6870
await page.goto('https://quickpizza.grafana.com/browser.php');
6971
} finally {

0 commit comments

Comments
 (0)