Skip to content

Commit 7a897b0

Browse files
AgnesTouletankur22
authored andcommitted
Browser: add route-related doc
1 parent 3b49259 commit 7a897b0

File tree

7 files changed

+360
-0
lines changed

7 files changed

+360
-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
@@ -48,6 +48,7 @@ Page provides methods to interact with a single tab in a running web browser. A
4848
| [opener()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/opener) | Returns the `page` that opened the current `page`. |
4949
| [press(selector, key[, options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/press/) | Focuses the element, and then presses the given `key` on the [Keyboard](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/keyboard). |
5050
| [reload([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/reload/) | Reloads the current page. |
51+
| [route(url, handler)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/route/) | Adds a route to the page to modify network requests made by that page. |
5152
| [screenshot([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/screenshot/) | Returns a buffer with the captured screenshot from the web browser. |
5253
| [selectOption(selector, values[, options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/selectoption/) | Selects one or more options which match the values from a `<select>` element. |
5354
| [setContent(html[, options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/setcontent/) | Sets the supplied HTML string to the current page. |
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: 'route(url, handler)'
3+
description: 'Browser module: page.route(url, handler) method'
4+
---
5+
6+
# route(url, handler)
7+
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.
9+
10+
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.
11+
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. |
16+
17+
### Returns
18+
19+
| Type | Description |
20+
| --------------- | ------------------------------------------------------------------------------- |
21+
| `Promise<void>` | A Promise that fulfills when the route is added to the page. |
22+
23+
24+
### Example
25+
26+
{{< code >}}
27+
28+
```javascript
29+
import { browser } from 'k6/browser';
30+
31+
export const options = {
32+
scenarios: {
33+
browser: {
34+
executor: 'shared-iterations',
35+
options: {
36+
browser: {
37+
type: 'chromium',
38+
},
39+
},
40+
},
41+
},
42+
};
43+
44+
export default async function () {
45+
const page = await browser.newPage();
46+
47+
// Abort all images requests
48+
await page.route(/(\.png$)|(\.jpg$)/, async function (route) {
49+
await route.abort();
50+
})
51+
52+
await page.goto('https://quickpizza.grafana.com/');
53+
}
54+
```
55+
56+
{{< /code >}}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: 'Route'
3+
description: 'Browser module: Route Class'
4+
weight: 12
5+
---
6+
7+
# Route
8+
9+
Route represents a network request intercepted by the [`page.route()`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/route) function and allows to modify its behavior. Once routing is enabled, every request intercepted by a route will stall unless it's continued, fulfilled or aborted.
10+
11+
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.
12+
13+
## Supported APIs
14+
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. |
21+
22+
### Example
23+
24+
{{< code >}}
25+
26+
```javascript
27+
import { browser } from 'k6/browser';
28+
29+
export const options = {
30+
scenarios: {
31+
browser: {
32+
executor: 'shared-iterations',
33+
options: {
34+
browser: {
35+
type: 'chromium',
36+
},
37+
},
38+
},
39+
},
40+
};
41+
42+
export default async function () {
43+
const page = await browser.newPage();
44+
45+
// Abort all images requests
46+
await page.route(/(\.png$)|(\.jpg$)/, async function (route) {
47+
await route.abort();
48+
})
49+
50+
await page.goto('https://quickpizza.grafana.com/');
51+
```
52+
53+
{{< /code >}}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: 'abort(errorCode)'
3+
description: 'Browser module: Route.abort method'
4+
---
5+
6+
# abort(errorCode)
7+
8+
Aborts the request with the given error code.
9+
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'`. |
13+
14+
### Returns
15+
16+
| Type | Description |
17+
| --------------- | ------------------------------------------------------------------------------- |
18+
| `Promise<void>` | A Promise that fulfills when the request is aborted. |
19+
20+
### Example
21+
22+
{{< code >}}
23+
24+
```javascript
25+
import { browser } from 'k6/browser';
26+
27+
export const options = {
28+
scenarios: {
29+
browser: {
30+
executor: 'shared-iterations',
31+
options: {
32+
browser: {
33+
type: 'chromium',
34+
},
35+
},
36+
},
37+
},
38+
};
39+
40+
export default async function () {
41+
const page = await browser.newPage();
42+
43+
// Abort all images requests
44+
await page.route(/(\.png$)|(\.jpg$)/, async function (route) {
45+
await route.abort();
46+
})
47+
48+
await page.goto('https://quickpizza.grafana.com/');
49+
```
50+
51+
{{< /code >}}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: 'continue(options)'
3+
description: 'Browser module: Route.continue method'
4+
---
5+
6+
# continue(options)
7+
8+
Sends the request to the network with optional overrides.
9+
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. |
17+
18+
### Returns
19+
20+
| Type | Description |
21+
| --------------- | ------------------------------------------------------------------------------- |
22+
| `Promise<void>` | A Promise that fulfills when the request is resumed. |
23+
24+
### Example
25+
26+
{{< code >}}
27+
28+
```javascript
29+
import { browser } from 'k6/browser';
30+
31+
export const options = {
32+
scenarios: {
33+
ui: {
34+
executor: 'shared-iterations',
35+
options: {
36+
browser: {
37+
type: 'chromium',
38+
},
39+
},
40+
},
41+
},
42+
};
43+
44+
export default async function () {
45+
const page = await browser.newPage();
46+
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+
});
54+
});
55+
56+
await page.goto('https://quickpizza.grafana.com/');
57+
} finally {
58+
await page.close();
59+
}
60+
}
61+
```
62+
63+
{{< /code >}}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: 'fulfill(options)'
3+
description: 'Browser module: Route.fulfill method'
4+
---
5+
6+
# fulfill(options)
7+
8+
Fulfills the request with the given response.
9+
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. |
17+
18+
### Returns
19+
20+
| Type | Description |
21+
| --------------- | ------------------------------------------------------------------------------- |
22+
| `Promise<void>` | A Promise that fulfills when the request is fulfilled. |
23+
24+
25+
### Example
26+
27+
{{< code >}}
28+
29+
```javascript
30+
import { browser } from 'k6/browser';
31+
32+
export const options = {
33+
scenarios: {
34+
ui: {
35+
executor: 'shared-iterations',
36+
options: {
37+
browser: {
38+
type: 'chromium',
39+
},
40+
},
41+
},
42+
},
43+
};
44+
45+
export default async function () {
46+
const page = await browser.newPage();
47+
48+
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+
);
67+
68+
await page.goto('https://quickpizza.grafana.com/browser.php');
69+
} finally {
70+
await page.close();
71+
}
72+
}
73+
```
74+
75+
{{< /code >}}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: 'request()'
3+
description: 'Browser module: Route.request method'
4+
---
5+
6+
# request()
7+
8+
Returns the matching [Request](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/request) object.
9+
10+
### Returns
11+
12+
| Type | Description |
13+
| --------- | --------------------- |
14+
| `Request` | The `Request` object. |
15+
16+
### Example
17+
18+
{{< code >}}
19+
20+
```javascript
21+
import { browser } from 'k6/browser';
22+
23+
export const options = {
24+
scenarios: {
25+
ui: {
26+
executor: 'shared-iterations',
27+
options: {
28+
browser: {
29+
type: 'chromium',
30+
},
31+
},
32+
},
33+
},
34+
};
35+
36+
export default async function () {
37+
const page = await browser.newPage();
38+
39+
try {
40+
await page.route(/.*\/api\/pizza/, async function (route) {
41+
await route.continue(
42+
{
43+
headers: {
44+
...route.request().headers(),
45+
'Content-Type': 'application/json',
46+
},
47+
postData: JSON.stringify({
48+
'customName': 'My Pizza'
49+
}),
50+
}
51+
);
52+
});
53+
54+
await page.goto('https://quickpizza.grafana.com/');
55+
} finally {
56+
await page.close();
57+
}
58+
}
59+
```
60+
61+
{{< /code >}}

0 commit comments

Comments
 (0)