-
Notifications
You must be signed in to change notification settings - Fork 238
Browser: add route-related doc #2018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
98560a6
Browser: add route-related doc
AgnesToulet 8b7f156
Add code review changes
ankur22 0b29d77
Add skip on examples for now
ankur22 4ebc2c8
Update subheading to h2 from h3
ankur22 3d05d5b
Update wording around matching handlers
ankur22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
docs/sources/k6/next/javascript-api/k6-browser/page/route.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
--- | ||
title: 'route(url, handler)' | ||
description: 'Browser module: page.route(url, handler) method' | ||
--- | ||
|
||
# route(url, handler) | ||
|
||
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. | ||
|
||
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. | ||
|
||
| Parameter | Type | Default | Description | | ||
| --------- | -------------------------------------------------------------------------------------------- | ------- | --------------------------------------------------- | | ||
| url | string or Regexp | `''` | URL to match during routing. | | ||
| handler | function([Route](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/)) | null | Handler function executed when routing the request. | | ||
|
||
### Returns | ||
|
||
| Type | Description | | ||
| --------------- | ------------------------------------------------------------ | | ||
| `Promise<void>` | A Promise that fulfills when the route is added to the page. | | ||
|
||
### Example | ||
|
||
{{< code >}} | ||
|
||
```javascript | ||
import { browser } from 'k6/browser'; | ||
|
||
export const options = { | ||
scenarios: { | ||
browser: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default async function () { | ||
const page = await browser.newPage(); | ||
|
||
// Abort all images requests | ||
await page.route(/(\.png$)|(\.jpg$)/, async function (route) { | ||
await route.abort(); | ||
}); | ||
|
||
// Fulfill request with the following response which | ||
// changes the quotes displayed on the page | ||
await page.route(/.*\/quotes$/, async function (route) { | ||
await route.fulfill({ | ||
body: JSON.stringify({ | ||
quotes: ['"We ❤️ pizza" - k6 team'], | ||
}), | ||
}); | ||
}); | ||
|
||
// Change the pizza request when the button is clicked | ||
await page.route(/.*\/pizza$/, async function (route) { | ||
await route.continue({ | ||
headers: { | ||
...route.request().headers(), | ||
foo: 'bar', | ||
}, | ||
method: 'POST', | ||
postData: JSON.stringify({ | ||
maxCaloriesPerSlice: 500, | ||
mustBeVegetarian: true, | ||
excludedIngredients: ['Pineapple'], | ||
excludedTools: ['Knife', 'Scissors'], | ||
maxNumberOfToppings: 1, | ||
minNumberOfToppings: 1, | ||
customName: 'Classic Pizza', | ||
}), | ||
}); | ||
}); | ||
|
||
await page.goto('https://quickpizza.grafana.com/'); | ||
|
||
await page.getByRole('button', { name: 'Pizza, Please!' }).click(); | ||
|
||
await page.close(); | ||
} | ||
``` | ||
|
||
{{< /code >}} |
88 changes: 88 additions & 0 deletions
88
docs/sources/k6/next/javascript-api/k6-browser/route/_index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
--- | ||
title: 'Route' | ||
description: 'Browser module: Route Class' | ||
weight: 12 | ||
--- | ||
|
||
# Route | ||
|
||
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. | ||
|
||
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. | ||
mstoykov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Supported APIs | ||
|
||
| Method | Description | | ||
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | | ||
| [abort([errorCode])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/abort) | Aborts the route's request. | | ||
| [continue([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/continue) | Continues the request with optional overrides. | | ||
| [fulfill([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/route/fulfill) | Fulfills the request with the given response | | ||
| [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. | | ||
|
||
### Example | ||
|
||
{{< code >}} | ||
|
||
```javascript | ||
import { browser } from 'k6/browser'; | ||
|
||
export const options = { | ||
scenarios: { | ||
browser: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default async function () { | ||
const page = await browser.newPage(); | ||
|
||
// Abort all images requests | ||
await page.route(/(\.png$)|(\.jpg$)/, async function (route) { | ||
await route.abort(); | ||
}); | ||
|
||
// Fulfill request with the following response which | ||
// changes the quotes displayed on the page | ||
await page.route(/.*\/quotes$/, async function (route) { | ||
await route.fulfill({ | ||
body: JSON.stringify({ | ||
quotes: ['"We ❤️ pizza" - k6 team'], | ||
}), | ||
}); | ||
}); | ||
|
||
// Change the pizza request when the button is clicked | ||
await page.route(/.*\/pizza$/, async function (route) { | ||
await route.continue({ | ||
headers: { | ||
...route.request().headers(), | ||
foo: 'bar', | ||
}, | ||
method: 'POST', | ||
postData: JSON.stringify({ | ||
maxCaloriesPerSlice: 500, | ||
mustBeVegetarian: true, | ||
excludedIngredients: ['Pineapple'], | ||
excludedTools: ['Knife', 'Scissors'], | ||
maxNumberOfToppings: 1, | ||
minNumberOfToppings: 1, | ||
customName: 'Classic Pizza', | ||
}), | ||
}); | ||
}); | ||
|
||
await page.goto('https://quickpizza.grafana.com/'); | ||
|
||
await page.getByRole('button', { name: 'Pizza, Please!' }).click(); | ||
|
||
await page.close(); | ||
} | ||
``` | ||
|
||
{{< /code >}} |
54 changes: 54 additions & 0 deletions
54
docs/sources/k6/next/javascript-api/k6-browser/route/abort.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
title: 'abort([errorCode])' | ||
description: 'Browser module: Route.abort method' | ||
--- | ||
|
||
# abort([errorCode]) | ||
|
||
Aborts the request with the given error code. | ||
|
||
| Parameter | Type | Default | Description | | ||
| --------- | ------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ||
| 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'`. | | ||
|
||
### Returns | ||
|
||
| Type | Description | | ||
| --------------- | ---------------------------------------------------- | | ||
| `Promise<void>` | A Promise that fulfills when the request is aborted. | | ||
|
||
### Example | ||
|
||
{{< code >}} | ||
|
||
```javascript | ||
import { browser } from 'k6/browser'; | ||
|
||
export const options = { | ||
scenarios: { | ||
browser: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default async function () { | ||
const page = await browser.newPage(); | ||
|
||
// Abort all images requests | ||
await page.route(/(\.png$)|(\.jpg$)/, async function (route) { | ||
await route.abort('blockedbyclient'); | ||
}); | ||
|
||
await page.goto('https://quickpizza.grafana.com/'); | ||
|
||
await page.close(); | ||
} | ||
``` | ||
|
||
{{< /code >}} |
81 changes: 81 additions & 0 deletions
81
docs/sources/k6/next/javascript-api/k6-browser/route/continue.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--- | ||
title: 'continue([options])' | ||
description: 'Browser module: Route.continue method' | ||
--- | ||
|
||
# continue([options]) | ||
|
||
{{< admonition type="caution" >}} | ||
|
||
This method has **known issues**, but it does work as intended. For details, refer to [#5012](https://github.com/grafana/k6/issues/5012). | ||
|
||
{{< /admonition >}} | ||
|
||
Sends the request to the network with optional overrides. | ||
|
||
| Parameter | Type | Default | Description | | ||
| ---------------- | ---------------- | ------- | ---------------------------------- | | ||
| options | object | null | | | ||
| options.headers | object | null | Request headers. | | ||
| options.method | string | `''` | Request method (e.g. GET or POST). | | ||
| options.postData | string or Buffer | `''` | Post data of the request. | | ||
| options.url | string | `''` | Request URL. | | ||
|
||
### Returns | ||
|
||
| Type | Description | | ||
| --------------- | ---------------------------------------------------- | | ||
| `Promise<void>` | A Promise that fulfills when the request is resumed. | | ||
|
||
### Example | ||
|
||
{{< code >}} | ||
|
||
```javascript | ||
import { browser } from 'k6/browser'; | ||
|
||
export const options = { | ||
scenarios: { | ||
browser: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default async function () { | ||
const page = await browser.newPage(); | ||
|
||
// Change the pizza request when the button is clicked | ||
await page.route(/.*\/pizza$/, async function (route) { | ||
await route.continue({ | ||
headers: { | ||
...route.request().headers(), | ||
foo: 'bar', | ||
}, | ||
method: 'POST', | ||
postData: JSON.stringify({ | ||
maxCaloriesPerSlice: 500, | ||
mustBeVegetarian: true, | ||
excludedIngredients: ['Pineapple'], | ||
excludedTools: ['Knife', 'Scissors'], | ||
maxNumberOfToppings: 1, | ||
minNumberOfToppings: 1, | ||
customName: 'Classic Pizza', | ||
}), | ||
}); | ||
}); | ||
|
||
await page.goto('https://quickpizza.grafana.com/'); | ||
|
||
await page.getByRole('button', { name: 'Pizza, Please!' }).click(); | ||
|
||
await page.close(); | ||
} | ||
``` | ||
|
||
{{< /code >}} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.