Setup
Configure the auth fixture and open a protected page with a test session
Configure the Blueprint Auth fixture, start a test as a signed-in user, and assert a protected dashboard. This tutorial seeds a session without testing the application’s sign-in flow.
Before you start
Complete Getting Started first. This guide builds on its test proxy, Playwright configuration, and shared test helper.
Your app also needs:
@krakentech/blueprint-authversion45.1.0or later within major version45(^45.1.0), configured for your router- A protected
/dashboardpage that queriesviewer.preferredNameand displays it as a heading
Use test-only keys and credentials, and restrict access to traces containing tokens or cookies.
Set up the Blueprint Auth fixture
Configure the app and fixture with matching test settings, then open the dashboard with a seeded session.
Define test settings
Create a shared test configuration. These endpoints replace live authentication services during the test:
import type { BlueprintAuthConfig } from "@octopus-energy/playwright/blueprint-auth";
export const testAuthConfig = {
krakenConfig: {
authEndpoint: "https://auth.kraken.test/",
graphqlAuthEndpoint: "https://api.kraken.test/v1/graphql/",
oauthClientId: "sandbox-playwright",
},
} satisfies BlueprintAuthConfig;Generate signing keys
Create a global setup file to generate the signing keys used by your test workers:
import { createSigningKeysSetup } from "@octopus-energy/playwright/blueprint-auth";
export default createSigningKeysSetup();Workers share fresh signing keys for each run. See the signing-key setup reference.
Configure the app’s test authentication
Merge these auth-specific additions into your existing Playwright configuration, keeping its server settings and environment variables. The app must trust the fixture’s token issuer:
import { defineConfig } from "@playwright/test";
import { testAuthConfig } from "./lib/playwright/auth-config";
const { krakenConfig } = testAuthConfig;
export default defineConfig({
globalSetup: "./lib/playwright/auth-setup.ts",
webServer: {
// ...existing server settings
env: {
// ...existing environment variables
KRAKEN_AUTH_ENDPOINT: krakenConfig.authEndpoint,
KRAKEN_GRAPHQL_ENDPOINT: krakenConfig.graphqlAuthEndpoint,
KRAKEN_GRAPHQL_AUTH_ENDPOINT: krakenConfig.graphqlAuthEndpoint,
KRAKEN_ACCESS_TOKEN_ISSUERS: new URL("token/", krakenConfig.authEndpoint).href,
KRAKEN_OAUTH_CLIENT_ID: krakenConfig.oauthClientId,
KRAKEN_X_CLIENT_IP_SECRET_KEY: "playwright_client_ip_secret",
},
},
});Extend your shared test helper
Keep your existing createPlaywrightHelpers import and options. Rename its test binding to base, then extend it with withBlueprintAuth:
import { withBlueprintAuth } from "@octopus-energy/playwright/blueprint-auth";
import { testAuthConfig } from "./auth-config";
const { test: base, expect } = createPlaywrightHelpers();
const test = base.extend(withBlueprintAuth(testAuthConfig));
export { expect, test };Open the dashboard as a signed-in user
Use login to set the session cookies, then mock the dashboard’s viewer query separately:
import { HttpResponse } from "msw";
import { expect, test } from "../../lib/playwright/test";
test("shows Alice’s dashboard", async ({ page, msw, blueprintAuth }) => {
await blueprintAuth.login({ authMethod: "email", subject: "alice" });
msw.use(
msw.graphql.link("https://api.kraken.test/v1/graphql/").query("viewer", () =>
HttpResponse.json({
data: { viewer: { preferredName: "Alice" } },
}),
),
);
await page.goto("/dashboard");
const heading = page.getByRole("heading", { name: "Alice", exact: true });
await expect(heading).toBeVisible();
});For a mobile web-view session, replace the login call above with:
await blueprintAuth.login({ authMethod: "mobile-web-view", subject: "alice" });This uses the same email-style tokens but sets MWAuthToken and MWRefreshToken cookies. The rest of the test stays the same. See the login reference for options and cookie behavior.
Run the test
Run the test from your application root:
npm exec -- playwright test tests/playwright/auth.spec.tsThe test passes when the dashboard displays Alice. Authentication uses your test session, and the query handler supplies the viewer data.
Next steps
Choose the behavior you want to test:
- User sessions: email sign-in, rejected credentials, and session refresh.
- OAuth redirects: exercise the application’s start and callback routes.
- Scoped and masquerade access: enter through a credential-bearing link or support route.
- Organization authentication: make server-side requests using a cached organization token.
See the API reference for method options and defaults.