Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

docs: add route example with some logic (#6324)

Dmitry Gozmance0331038b44
diff --git a/docs/src/api/class-browsercontext.md b/docs/src/api/class-browsercontext.md
index d916622d09..f6c2131210 100644
--- a/docs/src/api/class-browsercontext.md
+++ b/docs/src/api/class-browsercontext.md
@@ -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.
diff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.md
index 28545137c8..16cbe5bc81 100644
--- a/docs/src/api/class-page.md
+++ b/docs/src/api/class-page.md
@@ -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.
diff --git a/docs/src/api/class-route.md b/docs/src/api/class-route.md
index 3a84edd4a7..0b33a5f930 100644
--- a/docs/src/api/class-route.md
+++ b/docs/src/api/class-route.md
@@ -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)
```
diff --git a/types/types.d.ts b/types/types.d.ts
index d2bf5876c7..ab740185ea 100644
--- a/types/types.d.ts
+++ b/types/types.d.ts
@@ -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.
*