Testing Resilience
Learning Objectives
- You can distinguish native form submission from local preservation during network loss.
- You can test local restoration and detect a write sent before a reviewed decision.
- You can use controlled fixtures to state exactly what a browser test establishes.
Write two browser tests for the document editor: local persistence and waiting for a reviewed conflict decision. The exercise supplies a working application, controlled fixtures and a completed native-form test. That baseline checks a reachable server without client JavaScript; the optional reference below explains it. If you need a reminder of labelled controls, assertions and reloads, revisit the worked Playwright test in Chapter 4.8.
Save while disconnected, then restore
The draft fixture supplies an already-open editor, disconnect() and
reconnect(). Choose a short nonempty proposal that you can recognize later.
Follow it through one browser context:
- Fill Proposal, disconnect and use Save on this device.
- Wait for “Saved on this device.” before continuing. An auto-retrying
await expect(...).toHaveText(...)can perform this wait. The status means the supplied store completed the local save during network loss. - Reconnect, reload the reachable route and use Load local draft.
- Assert that the saved proposal returned.
Visible text before reload could simply be an input value. Restoration after reload distinguishes that from stored work. The same browser context retains the local store across the reload; independent tests start in fresh contexts.
Use context offline mode for network loss, controlled HTTP responses for a reachable service error and controlled storage promises for a storage failure. Each control isolates a different condition.
Evidence of local preservation
0 / 5 points
An already-open editor can save a draft on this device and restore it when its route can load again. Which test checks both the local save and the later restoration?
Observe writes around an explicit decision
The review fixture starts a title editor at version 3. Its bump() changes
the server to “Other writer”, version 4; patches() returns recorded write
bodies. Choose a proposal different from “Original title” and “Other writer”.
Fill Proposed title, bump the server and use Save title. Wait for the
Current server title comparison. Before any reviewed decision, assert that
await review.patches() contains exactly one request.
Then use Reapply reviewed title and wait for “Server accepted the title.” to complete the explicit-decision path. The working fixture accepts a second request with the proposal and reviewed version 4. You can also assert the retained proposal, current title and exact request bodies to explain that interaction. The required detection assertion in this test is the request count before the decision; these extra observations are useful extensions.
Make an assertion detect its relevant fault
Complete the two sequences above in local-persistence and reviewed-conflict,
keeping both test names. Each test must pass against the working application
and detect its corresponding faulty version through a meaningful assertion.
An assertion that times out waiting for its expected result can detect a fault. Syntax/import errors, arbitrary throws, skips and a timeout of the whole test or browser action do not establish detection. Use the fixture controls rather than sleeps. A request-count assertion or a comparison of the whole request list can establish that no premature write occurred; choose the form you can explain.
Browser tests for interrupted work
0 / 45 points
Complete the two named tests in tests/resilience.spec.ts. Keep { test, expect } from "./fixture.ts". The browser fixture, HTTP application and local store are supplied. The optional native-form reference in this chapter shows the completed native-validation baseline; it remains an instructor regression. You author only the following two scenarios.
The supplied fixture interface is:
import type { Page } from "@playwright/test";type Fixtures = { draft: { page: Page; // local editor already open in its own browser context disconnect(): Promise<void>; reconnect(): Promise<void>; }; review: { page: Page; // title editor already open at version 3 in a fresh context bump(): Promise<void>; // server title becomes "Other writer", version 4 patches(): Promise<{ title: string; version: number }[]>; };};Choose a short, nonempty proposal for each scenario and use it in the corresponding assertions. For conflict, choose text different from Original title and Other writer.
local-persistence
Fill Proposal, await draft.disconnect() and activate Save on this device. Wait until the status region says Saved on this device. before continuing; an auto-retrying await expect(...).toHaveText(...) is one way to wait.
Await draft.reconnect(), reload the same page in the same browser context and activate Load local draft. Assert that the saved proposal returns in Proposal. Restoring the proposal after reload demonstrates that the local save persisted.
reviewed-conflict
Fill Proposed title, await review.bump() and activate Save title. Wait for the Current server title comparison before inspecting the requests.
Before any explicit decision, assert that await review.patches() contains exactly one request. A length assertion or a comparison with the complete expected request list are both suitable.
Activate Reapply reviewed title and wait for Server accepted the title. to complete the scenario. The working fixture then accepts a second request with the same proposal and reviewed version 4.
Extra assertions about Other writer, retained input or exact request bodies are useful extensions. The required detection assertion is the request count before the explicit decision.
Submit the tests
Keep both test names and complete the stated fixture steps. The required assertions check restoration after reload and no extra write before reapplication. Predict which faulty behavior each assertion detects; organize the assertions in the way you can explain.
Both tests must pass the working application and detect their targeted faulty application through a relevant assertion. A failed expectation can count; syntax/import errors, arbitrary throws, skips, whole-test/action timeouts and sleeps earn no detection credit.
Choose Submit solution. Actual local persistence: 20 points; reviewed recovery without replay: 25. The supplied native baseline earns no student points.
These tests cover local restoration across a reachable reload and waiting for a reviewed decision. The editor exercise separately checks denied and failed comparison reads, teardown and further conflicts.
Optional reference: the supplied native-form baseline
The fixture opens /feedback with JavaScript disabled. Its completed test has
this shape:
import { test, expect } from "./fixture.ts";
test("native-baseline", async ({ nativePage }) => { const raw = " "; // Exactly ten spaces. await nativePage.getByLabel("Message").fill(raw); await nativePage.getByRole("button", { name: "Check feedback" }).click(); await expect(nativePage.getByRole("status")) .toHaveText("Write between 10 and 200 characters."); await expect(nativePage.getByLabel("Message")).toHaveValue(raw);});Ten raw spaces satisfy the browser’s required/minimum-length constraint but fail server validation after trimming. A shorter value could stop at the browser instead of reaching the action. The assertions check server feedback and exact retained text with JavaScript disabled.
Check Your Understanding
- Why does native form submission still need a reachable server?
- Which observation distinguishes stored work from retained input?
- Why must the storage test keep one browser context across reload?
- Which request observation detects replay before a reviewed decision?