docs: add a new fruit when mocking response (#29137)

Strawberry is already in the original list.

Fixes https://github.com/microsoft/playwright/issues/29114
This commit is contained in:
Yury Semikhatsky 2024-01-23 16:10:33 -08:00 committed by GitHub
parent cec2ff1772
commit 9cd2916623
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -118,7 +118,7 @@ test('gets the json from api and adds a new fruit', async ({ page }) => {
await page.route('*/**/api/v1/fruits', async route => { await page.route('*/**/api/v1/fruits', async route => {
const response = await route.fetch(); const response = await route.fetch();
const json = await response.json(); const json = await response.json();
json.push({ name: 'Playwright', id: 100 }); json.push({ name: 'Loquat', id: 100 });
// Fulfill using the original response, while patching the response body // Fulfill using the original response, while patching the response body
// with the given JSON object. // with the given JSON object.
await route.fulfill({ response, json }); await route.fulfill({ response, json });
@ -128,7 +128,7 @@ test('gets the json from api and adds a new fruit', async ({ page }) => {
await page.goto('https://demo.playwright.dev/api-mocking'); await page.goto('https://demo.playwright.dev/api-mocking');
// Assert that the new fruit is visible // Assert that the new fruit is visible
await expect(page.getByText('Playwright', { exact: true })).toBeVisible(); await expect(page.getByText('Loquat', { exact: true })).toBeVisible();
}); });
``` ```
@ -137,7 +137,7 @@ async def test_gets_the_json_from_api_and_adds_a_new_fruit(page: Page):
async def handle(route: Route): async def handle(route: Route):
response = await route.fetch() response = await route.fetch()
json = await response.json() json = await response.json()
json.append({ "name": "Playwright", "id": 100}) json.append({ "name": "Loquat", "id": 100})
# Fulfill using the original response, while patching the response body # Fulfill using the original response, while patching the response body
# with the given JSON object. # with the given JSON object.
await route.fulfill(response=response, json=json) await route.fulfill(response=response, json=json)
@ -148,7 +148,7 @@ async def test_gets_the_json_from_api_and_adds_a_new_fruit(page: Page):
await page.goto("https://demo.playwright.dev/api-mocking") await page.goto("https://demo.playwright.dev/api-mocking")
# Assert that the new fruit is visible # Assert that the new fruit is visible
await page.get_by_text("Playwright", exact=True).to_be_visible() await page.get_by_text("Loquat", exact=True).to_be_visible()
``` ```
```python sync ```python sync
@ -156,7 +156,7 @@ def test_gets_the_json_from_api_and_adds_a_new_fruit(page: Page):
def handle(route: Route): def handle(route: Route):
response = route.fetch() response = route.fetch()
json = response.json() json = response.json()
json.append({ "name": "Playwright", "id": 100}) json.append({ "name": "Loquat", "id": 100})
# Fulfill using the original response, while patching the response body # Fulfill using the original response, while patching the response body
# with the given JSON object. # with the given JSON object.
route.fulfill(response=response, json=json) route.fulfill(response=response, json=json)
@ -167,28 +167,28 @@ def test_gets_the_json_from_api_and_adds_a_new_fruit(page: Page):
page.goto("https://demo.playwright.dev/api-mocking") page.goto("https://demo.playwright.dev/api-mocking")
# Assert that the new fruit is visible # Assert that the new fruit is visible
page.get_by_text("Playwright", exact=True).to_be_visible() page.get_by_text("Loquat", exact=True).to_be_visible()
``` ```
```csharp ```csharp
await page.RouteAsync("*/**/api/v1/fruits", async (route) => { await page.RouteAsync("*/**/api/v1/fruits", async (route) => {
var response = await route.FetchAsync(); var response = await route.FetchAsync();
var fruits = await response.JsonAsync<Fruit[]>(); var fruits = await response.JsonAsync<Fruit[]>();
fruits.Add(new Fruit() { Name = "Playwright", Id = 100 }); fruits.Add(new Fruit() { Name = "Loquat", Id = 100 });
// Fulfill using the original response, while patching the response body // Fulfill using the original response, while patching the response body
// with the given JSON object. // with the given JSON object.
await route.FulfillAsync(new () await route.FulfillAsync(new ()
{ {
Response = response, Response = response,
Json = json Json = fruits
}); });
} }
); );
// Go to the page // Go to the page
await page.GotoAsync("https://demo.playwright.dev/api-mocking"); await page.GotoAsync("https://demo.playwright.dev/api-mocking");
// Assert that the Strawberry fruit is visible // Assert that the Loquat fruit is visible
await Expect(page.GetByTextAsync("Playwright", new () { Exact = true })).ToBeVisibleAsync(); await Expect(page.GetByTextAsync("Loquat", new () { Exact = true })).ToBeVisibleAsync();
``` ```
```java ```java
@ -196,7 +196,7 @@ page.route("*/**/api/v1/fruits", route -> {
Response response = route.fetch(); Response response = route.fetch();
byte[] json = response.body(); byte[] json = response.body();
parsed = new Gson().fromJson(json, JsonObject.class) parsed = new Gson().fromJson(json, JsonObject.class)
parsed.add(new JsonObject().add("name", "Playwright").add("id", 100)); parsed.add(new JsonObject().add("name", "Loquat").add("id", 100));
// Fulfill using the original response, while patching the response body // Fulfill using the original response, while patching the response body
// with the given JSON object. // with the given JSON object.
route.fulfill(new Route.FulfillOptions().setResponse(response).setBody(json.toString())); route.fulfill(new Route.FulfillOptions().setResponse(response).setBody(json.toString()));
@ -205,8 +205,8 @@ page.route("*/**/api/v1/fruits", route -> {
// Go to the page // Go to the page
page.goto("https://demo.playwright.dev/api-mocking"); page.goto("https://demo.playwright.dev/api-mocking");
// Assert that the Strawberry fruit is visible // Assert that the Loquat fruit is visible
assertThat(page.getByText("Playwright", new Page.GetByTextOptions().setExact(true))).isVisible(); assertThat(page.getByText("Loquat", new Page.GetByTextOptions().setExact(true))).isVisible();
``` ```
In the trace of our test we can see that the API was called and the response was modified. In the trace of our test we can see that the API was called and the response was modified.