Guides / Testing

Playwright

Reliable Playwright Tests with Hosted Mock Endpoints

Give browser tests stable checkout outcomes in local runs and CI using explicit success, declined, and error routes without shared mutable state.

Mockport Team · Updated September 28, 2026 · 14 min read

What you’ll build

  • Stable success, declined, and error checkout routes.
  • Separate app and API environment configuration.
  • Deterministic checkout assertions.
  • Parallel-test isolation using static scenario routes.

01

Model outcomes as separate routes

Create a checkout-tests project and add one POST route per outcome. Each route returns a static body and selected HTTP status, making the scenario readable from the test name and URL.

Use /v1/checkout/success for 200, /declined for 402, and /error for 500. Request bodies can remain realistic while returned fixtures stay fixed and easy to assert.

success-response.json
{
  "orderId": "ord_test_1001",
  "status": "confirmed",
  "total": 64
}
Practical note

A POST mock exercises frontend request code without creating persistent records.

02

Configure the app and Playwright runner

Keep the application URL and mock API URL separate. Playwright baseURL identifies the app under test; the app’s API environment variable identifies the hosted mock service.

Set both locally and in CI. A Playwright configuration does not automatically change your application’s fetch destination—you must explicitly configure the app.

playwright.config.ts
import { defineConfig } from "@playwright/test";

export default defineConfig({
  use: {
    baseURL:
      process.env.PLAYWRIGHT_BASE_URL ?? "http://127.0.0.1:3000",
  },
  fullyParallel: true,
  retries: process.env.CI ? 2 : 0,
});
Practical note

For a Next.js browser app, set NEXT_PUBLIC_API_URL before building the app under test.

03

Write a deterministic success test

Select the success scenario before opening checkout, then submit the same known cart on every run. Assert user-visible completion rather than implementation details.

The order id is fixed in the mock response, so the receipt can be checked exactly. This proves the interface mapped the response into the expected confirmation.

checkout-success.spec.ts
import { test, expect } from "@playwright/test";

test("confirms a completed checkout", async ({ page }) => {
  await page.addInitScript(() => {
    localStorage.setItem("checkoutScenario", "success");
  });

  await page.goto("/checkout");
  await page.getByRole("button", { name: "Place order" }).click();

  await expect(
    page.getByRole("heading", { name: "Order confirmed" })
  ).toBeVisible();
  await expect(page.getByText("ord_test_1001")).toBeVisible();
});
Practical note

Your demo app must read checkoutScenario and choose the matching route. Only enable this scenario switch in development/test builds.

04

Cover declined and service-error states

Run the declined route as its own test and expect the checkout form to remain available. The message should explain that payment was declined without claiming an order was created.

The 500 route should preserve the cart and expose a retry action. Assert recovery copy and enabled actions instead of merely checking for a red box.

checkout-errors.spec.ts
import { test, expect } from "@playwright/test";

for (const scenario of ["declined", "error"] as const) {
  test(`recovers from ${scenario}`, async ({ page }) => {
    await page.addInitScript((value) => {
      localStorage.setItem("checkoutScenario", value);
    }, scenario);

    await page.goto("/checkout");
    await page.getByRole("button", { name: "Place order" }).click();

    await expect(page.getByRole("alert")).toBeVisible();
    await expect(
      page.getByRole("button", {
        name: scenario === "declined" ? "Try another card" : "Retry",
      })
    ).toBeEnabled();
  });
}

05

Set CI variables explicitly

Define PLAYWRIGHT_BASE_URL for the deployed app and its API environment variable for checkout-tests.mockport.dev. Keep variable names identical to local configuration.

A small preflight request can provide a fast configuration failure. Check both the selected HTTP status and a known fixture key before running browser tests.

ci-environment.txt
# Test runner:
PLAYWRIGHT_BASE_URL=https://your-preview.example.com
CI=true

# Application build:
NEXT_PUBLIC_API_URL=https://checkout-tests.mockport.dev
Practical note

Mockport routes are public test surfaces. Use invented fixtures, never customer records or secrets.

06

Isolate parallel tests without shared state

Parallel tests become unreliable when one worker edits a response another is using. Keep published fixtures static during a run and assign descriptive routes to every outcome.

If two suites need different success bodies, use /success-standard and /success-discount rather than mutating /success. This makes failures easier to reproduce after CI completes.

  • Do not edit a shared response during a test run.
  • Use separate route names for separate outcomes.
  • Keep fixture ids fixed and non-sensitive.
  • Set scenario selection in each isolated browser context.
  • Assert against one known static response per test.

Keep the contract predictable, then keep building.

Static hosted scenario routes make local and CI checkout tests tell the same story. Separate URLs provide isolation, while explicit statuses make success, decline, and service-error behavior easy to verify.

Put your next endpoint online in minutes.

Start a 14-day trial. Team workspaces are $9/month with 5 project domains, 15 routes per project, and up to 5 seats.

Start free trial →

Related guides