docs: add route example with some logic (#6324)
This commit is contained in:
parent
be27f47309
commit
ce0331038b
|
|
@ -774,6 +774,44 @@ page.goto("https://example.com")
|
|||
browser.close()
|
||||
```
|
||||
|
||||
It is possible to examine the request to decide the route action. For example, mocking all requests that contain some post data, and leaving all other requests as is:
|
||||
|
||||
```js
|
||||
await context.route('/api/**', route => {
|
||||
if (route.request().postData().includes('my-string'))
|
||||
route.fulfill({ body: 'mocked-data' });
|
||||
else
|
||||
route.continue();
|
||||
});
|
||||
```
|
||||
|
||||
```java
|
||||
context.route("/api/**", route -> {
|
||||
if (route.request().postData().contains("my-string"))
|
||||
route.fulfill(new Route.FulfillOptions().setBody("mocked-data"));
|
||||
else
|
||||
route.resume();
|
||||
});
|
||||
```
|
||||
|
||||
```python async
|
||||
def handle_route(route):
|
||||
if ("my-string" in route.request.post_data)
|
||||
route.fulfill(body="mocked-data")
|
||||
else
|
||||
route.continue_()
|
||||
await context.route("/api/**", handle_route)
|
||||
```
|
||||
|
||||
```python sync
|
||||
def handle_route(route):
|
||||
if ("my-string" in route.request.post_data)
|
||||
route.fulfill(body="mocked-data")
|
||||
else
|
||||
route.continue_()
|
||||
context.route("/api/**", handle_route)
|
||||
```
|
||||
|
||||
Page routes (set up with [`method: Page.route`]) take precedence over browser context routes when request matches both
|
||||
handlers.
|
||||
|
||||
|
|
|
|||
|
|
@ -2144,6 +2144,44 @@ page.goto("https://example.com")
|
|||
browser.close()
|
||||
```
|
||||
|
||||
It is possible to examine the request to decide the route action. For example, mocking all requests that contain some post data, and leaving all other requests as is:
|
||||
|
||||
```js
|
||||
await page.route('/api/**', route => {
|
||||
if (route.request().postData().includes('my-string'))
|
||||
route.fulfill({ body: 'mocked-data' });
|
||||
else
|
||||
route.continue();
|
||||
});
|
||||
```
|
||||
|
||||
```java
|
||||
page.route("/api/**", route -> {
|
||||
if (route.request().postData().contains("my-string"))
|
||||
route.fulfill(new Route.FulfillOptions().setBody("mocked-data"));
|
||||
else
|
||||
route.resume();
|
||||
});
|
||||
```
|
||||
|
||||
```python async
|
||||
def handle_route(route):
|
||||
if ("my-string" in route.request.post_data)
|
||||
route.fulfill(body="mocked-data")
|
||||
else
|
||||
route.continue_()
|
||||
await page.route("/api/**", handle_route)
|
||||
```
|
||||
|
||||
```python sync
|
||||
def handle_route(route):
|
||||
if ("my-string" in route.request.post_data)
|
||||
route.fulfill(body="mocked-data")
|
||||
else
|
||||
route.continue_()
|
||||
page.route("/api/**", handle_route)
|
||||
```
|
||||
|
||||
Page routes take precedence over browser context routes (set up with [`method: BrowserContext.route`]) when request
|
||||
matches both handlers.
|
||||
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ async def handle(route, request):
|
|||
"foo": "bar" # set "foo" header
|
||||
"origin": None # remove "origin" header
|
||||
}
|
||||
await route.continue(headers=headers)
|
||||
await route.continue_(headers=headers)
|
||||
}
|
||||
await page.route("**/*", handle)
|
||||
```
|
||||
|
|
@ -79,7 +79,7 @@ def handle(route, request):
|
|||
"foo": "bar" # set "foo" header
|
||||
"origin": None # remove "origin" header
|
||||
}
|
||||
route.continue(headers=headers)
|
||||
route.continue_(headers=headers)
|
||||
}
|
||||
page.route("**/*", handle)
|
||||
```
|
||||
|
|
|
|||
24
types/types.d.ts
vendored
24
types/types.d.ts
vendored
|
|
@ -2382,6 +2382,18 @@ export interface Page {
|
|||
* await browser.close();
|
||||
* ```
|
||||
*
|
||||
* It is possible to examine the request to decide the route action. For example, mocking all requests that contain some
|
||||
* post data, and leaving all other requests as is:
|
||||
*
|
||||
* ```js
|
||||
* await page.route('/api/**', route => {
|
||||
* if (route.request().postData().includes('my-string'))
|
||||
* route.fulfill({ body: 'mocked-data' });
|
||||
* else
|
||||
* route.continue();
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* Page routes take precedence over browser context routes (set up with
|
||||
* [browserContext.route(url, handler)](https://playwright.dev/docs/api/class-browsercontext#browsercontextrouteurl-handler))
|
||||
* when request matches both handlers.
|
||||
|
|
@ -5262,6 +5274,18 @@ export interface BrowserContext {
|
|||
* await browser.close();
|
||||
* ```
|
||||
*
|
||||
* It is possible to examine the request to decide the route action. For example, mocking all requests that contain some
|
||||
* post data, and leaving all other requests as is:
|
||||
*
|
||||
* ```js
|
||||
* await context.route('/api/**', route => {
|
||||
* if (route.request().postData().includes('my-string'))
|
||||
* route.fulfill({ body: 'mocked-data' });
|
||||
* else
|
||||
* route.continue();
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* Page routes (set up with [page.route(url, handler)](https://playwright.dev/docs/api/class-page#pagerouteurl-handler))
|
||||
* take precedence over browser context routes when request matches both handlers.
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in a new issue