docs: make sure to await route.* methods (#29489)
Fixes https://github.com/microsoft/playwright/issues/29471 Follow-up on https://github.com/microsoft/playwright/pull/28745.
This commit is contained in:
parent
918c26fa02
commit
ce5d970929
|
|
@ -331,7 +331,7 @@ test('context request will share cookie storage with its browser context', async
|
||||||
[name, value])
|
[name, value])
|
||||||
)).toEqual(responseCookies);
|
)).toEqual(responseCookies);
|
||||||
|
|
||||||
route.fulfill({
|
await route.fulfill({
|
||||||
response,
|
response,
|
||||||
headers: { ...responseHeaders, foo: 'bar' },
|
headers: { ...responseHeaders, foo: 'bar' },
|
||||||
});
|
});
|
||||||
|
|
@ -375,7 +375,7 @@ test('global context request has isolated cookie storage', async ({
|
||||||
new Map(contextCookies2.map(({ name, value }) => [name, value]))
|
new Map(contextCookies2.map(({ name, value }) => [name, value]))
|
||||||
).toEqual(responseCookies);
|
).toEqual(responseCookies);
|
||||||
|
|
||||||
route.fulfill({
|
await route.fulfill({
|
||||||
response,
|
response,
|
||||||
headers: { ...responseHeaders, foo: 'bar' },
|
headers: { ...responseHeaders, foo: 'bar' },
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1120,11 +1120,11 @@ await browser.CloseAsync();
|
||||||
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:
|
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
|
```js
|
||||||
await context.route('/api/**', route => {
|
await context.route('/api/**', async route => {
|
||||||
if (route.request().postData().includes('my-string'))
|
if (route.request().postData().includes('my-string'))
|
||||||
route.fulfill({ body: 'mocked-data' });
|
await route.fulfill({ body: 'mocked-data' });
|
||||||
else
|
else
|
||||||
route.continue();
|
await route.continue();
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -1138,16 +1138,16 @@ context.route("/api/**", route -> {
|
||||||
```
|
```
|
||||||
|
|
||||||
```python async
|
```python async
|
||||||
def handle_route(route):
|
async def handle_route(route: Route):
|
||||||
if ("my-string" in route.request.post_data):
|
if ("my-string" in route.request.post_data):
|
||||||
route.fulfill(body="mocked-data")
|
await route.fulfill(body="mocked-data")
|
||||||
else:
|
else:
|
||||||
route.continue_()
|
await route.continue_()
|
||||||
await context.route("/api/**", handle_route)
|
await context.route("/api/**", handle_route)
|
||||||
```
|
```
|
||||||
|
|
||||||
```python sync
|
```python sync
|
||||||
def handle_route(route):
|
def handle_route(route: Route):
|
||||||
if ("my-string" in route.request.post_data):
|
if ("my-string" in route.request.post_data):
|
||||||
route.fulfill(body="mocked-data")
|
route.fulfill(body="mocked-data")
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -3456,11 +3456,11 @@ await page.GotoAsync("https://www.microsoft.com");
|
||||||
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:
|
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
|
```js
|
||||||
await page.route('/api/**', route => {
|
await page.route('/api/**', async route => {
|
||||||
if (route.request().postData().includes('my-string'))
|
if (route.request().postData().includes('my-string'))
|
||||||
route.fulfill({ body: 'mocked-data' });
|
await route.fulfill({ body: 'mocked-data' });
|
||||||
else
|
else
|
||||||
route.continue();
|
await route.continue();
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -3474,16 +3474,16 @@ page.route("/api/**", route -> {
|
||||||
```
|
```
|
||||||
|
|
||||||
```python async
|
```python async
|
||||||
def handle_route(route):
|
async def handle_route(route: Route):
|
||||||
if ("my-string" in route.request.post_data):
|
if ("my-string" in route.request.post_data):
|
||||||
route.fulfill(body="mocked-data")
|
await route.fulfill(body="mocked-data")
|
||||||
else:
|
else:
|
||||||
route.continue_()
|
await route.continue_()
|
||||||
await page.route("/api/**", handle_route)
|
await page.route("/api/**", handle_route)
|
||||||
```
|
```
|
||||||
|
|
||||||
```python sync
|
```python sync
|
||||||
def handle_route(route):
|
def handle_route(route: Route):
|
||||||
if ("my-string" in route.request.post_data):
|
if ("my-string" in route.request.post_data):
|
||||||
route.fulfill(body="mocked-data")
|
route.fulfill(body="mocked-data")
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -92,11 +92,11 @@ page.route("**/*", handle)
|
||||||
```
|
```
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
await page.RouteAsync("**/*", route =>
|
await page.RouteAsync("**/*", async route =>
|
||||||
{
|
{
|
||||||
var headers = new Dictionary<string, string>(route.Request.Headers) { { "foo", "bar" } };
|
var headers = new Dictionary<string, string>(route.Request.Headers) { { "foo", "bar" } };
|
||||||
headers.Remove("origin");
|
headers.Remove("origin");
|
||||||
route.ContinueAsync(headers);
|
await route.ContinueAsync(new() { Headers = headers });
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -264,17 +264,17 @@ page.route("**/*", route -> {
|
||||||
|
|
||||||
```python async
|
```python async
|
||||||
# Handle GET requests.
|
# Handle GET requests.
|
||||||
def handle_get(route):
|
async def handle_get(route):
|
||||||
if route.request.method != "GET":
|
if route.request.method != "GET":
|
||||||
route.fallback()
|
await route.fallback()
|
||||||
return
|
return
|
||||||
# Handling GET only.
|
# Handling GET only.
|
||||||
# ...
|
# ...
|
||||||
|
|
||||||
# Handle POST requests.
|
# Handle POST requests.
|
||||||
def handle_post(route):
|
async def handle_post(route):
|
||||||
if route.request.method != "POST":
|
if route.request.method != "POST":
|
||||||
route.fallback()
|
await route.fallback()
|
||||||
return
|
return
|
||||||
# Handling POST only.
|
# Handling POST only.
|
||||||
# ...
|
# ...
|
||||||
|
|
@ -378,11 +378,11 @@ page.route("**/*", handle)
|
||||||
```
|
```
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
await page.RouteAsync("**/*", route =>
|
await page.RouteAsync("**/*", async route =>
|
||||||
{
|
{
|
||||||
var headers = new Dictionary<string, string>(route.Request.Headers) { { "foo", "foo-value" } };
|
var headers = new Dictionary<string, string>(route.Request.Headers) { { "foo", "foo-value" } };
|
||||||
headers.Remove("bar");
|
headers.Remove("bar");
|
||||||
route.FallbackAsync(headers);
|
await route.FallbackAsync(new() { Headers = headers });
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -495,10 +495,10 @@ await page.GotoAsync("https://example.com");
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Delete header
|
// Delete header
|
||||||
await page.route('**/*', route => {
|
await page.route('**/*', async route => {
|
||||||
const headers = route.request().headers();
|
const headers = route.request().headers();
|
||||||
delete headers['X-Secret'];
|
delete headers['X-Secret'];
|
||||||
route.continue({ headers });
|
await route.continue({ headers });
|
||||||
});
|
});
|
||||||
|
|
||||||
// Continue requests as POST.
|
// Continue requests as POST.
|
||||||
|
|
@ -522,7 +522,7 @@ page.route("**/*", route -> route.resume(new Route.ResumeOptions().setMethod("PO
|
||||||
async def handle_route(route):
|
async def handle_route(route):
|
||||||
headers = route.request.headers
|
headers = route.request.headers
|
||||||
del headers["x-secret"]
|
del headers["x-secret"]
|
||||||
route.continue_(headers=headers)
|
await route.continue_(headers=headers)
|
||||||
await page.route("**/*", handle_route)
|
await page.route("**/*", handle_route)
|
||||||
|
|
||||||
# Continue requests as POST.
|
# Continue requests as POST.
|
||||||
|
|
@ -617,7 +617,7 @@ await page.route('**/title.html', async route => {
|
||||||
// Add a prefix to the title.
|
// Add a prefix to the title.
|
||||||
let body = await response.text();
|
let body = await response.text();
|
||||||
body = body.replace('<title>', '<title>My prefix:');
|
body = body.replace('<title>', '<title>My prefix:');
|
||||||
route.fulfill({
|
await route.fulfill({
|
||||||
// Pass all fields from the response.
|
// Pass all fields from the response.
|
||||||
response,
|
response,
|
||||||
// Override response body.
|
// Override response body.
|
||||||
|
|
|
||||||
|
|
@ -1233,16 +1233,16 @@ test.beforeEach(async ({ page }) => {
|
||||||
await page.route('**/*', async route => {
|
await page.route('**/*', async route => {
|
||||||
const headers = await route.request().allHeaders();
|
const headers = await route.request().allHeaders();
|
||||||
delete headers['if-none-match'];
|
delete headers['if-none-match'];
|
||||||
route.fallback({ headers });
|
await route.fallback({ headers });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work', async ({ page }) => {
|
test('should work', async ({ page }) => {
|
||||||
await page.route('**/*', route => {
|
await page.route('**/*', async route => {
|
||||||
if (route.request().resourceType() === 'image')
|
if (route.request().resourceType() === 'image')
|
||||||
route.abort();
|
await route.abort();
|
||||||
else
|
else
|
||||||
route.fallback();
|
await route.fallback();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
@ -1813,7 +1813,7 @@ test('response interception', async ({ page }) => {
|
||||||
const response = await page._request.fetch(route.request());
|
const response = await page._request.fetch(route.request());
|
||||||
const image = await jimp.read(await response.body());
|
const image = await jimp.read(await response.body());
|
||||||
await image.blur(5);
|
await image.blur(5);
|
||||||
route.fulfill({
|
await route.fulfill({
|
||||||
response,
|
response,
|
||||||
body: await image.getBufferAsync('image/jpeg'),
|
body: await image.getBufferAsync('image/jpeg'),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
12
packages/playwright-core/types/types.d.ts
vendored
12
packages/playwright-core/types/types.d.ts
vendored
|
|
@ -3665,11 +3665,11 @@ export interface Page {
|
||||||
* some post data, and leaving all other requests as is:
|
* some post data, and leaving all other requests as is:
|
||||||
*
|
*
|
||||||
* ```js
|
* ```js
|
||||||
* await page.route('/api/**', route => {
|
* await page.route('/api/**', async route => {
|
||||||
* if (route.request().postData().includes('my-string'))
|
* if (route.request().postData().includes('my-string'))
|
||||||
* route.fulfill({ body: 'mocked-data' });
|
* await route.fulfill({ body: 'mocked-data' });
|
||||||
* else
|
* else
|
||||||
* route.continue();
|
* await route.continue();
|
||||||
* });
|
* });
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
|
|
@ -8464,11 +8464,11 @@ export interface BrowserContext {
|
||||||
* some post data, and leaving all other requests as is:
|
* some post data, and leaving all other requests as is:
|
||||||
*
|
*
|
||||||
* ```js
|
* ```js
|
||||||
* await context.route('/api/**', route => {
|
* await context.route('/api/**', async route => {
|
||||||
* if (route.request().postData().includes('my-string'))
|
* if (route.request().postData().includes('my-string'))
|
||||||
* route.fulfill({ body: 'mocked-data' });
|
* await route.fulfill({ body: 'mocked-data' });
|
||||||
* else
|
* else
|
||||||
* route.continue();
|
* await route.continue();
|
||||||
* });
|
* });
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue