Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/src/api/class-browsercontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
38 changes: 38 additions & 0 deletions docs/src/api/class-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions docs/src/api/class-route.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand All @@ -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)
```
Expand Down
24 changes: 24 additions & 0 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down