Skip to content

Commit daf7772

Browse files
committed
Insert test fixture into product journey tests
1 parent 02c8d0a commit daf7772

14 files changed

Lines changed: 155 additions & 18 deletions

‎playwright.config.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { defineConfig, devices } from "@playwright/test";
88
// import path from 'path';
99
// dotenv.config({ path: path.resolve(__dirname, '.env') });
1010

11-
export const baseURL = process.env.BASE_URL || "https://endform-playwright-tutorial.vercel.app"
11+
export const baseURL =
12+
process.env.BASE_URL || "https://endform-playwright-tutorial.vercel.app";
1213

1314
/**
1415
* See https://playwright.dev/docs/test-configuration.

‎tests/activity-section.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect, test } from "./test";
22

33
test.describe("Activity Section", () => {
44
test("should navigate to dashboard activity section and verify user activities", async ({

‎tests/change-email.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect, test } from "./test";
22
import { testWithNewUser } from "./test-with-new-user";
33

44
test.describe("User Email Change Flow", () => {

‎tests/change-name.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect, test } from "./test";
22
import { testWithNewUser } from "./test-with-new-user";
33

44
test.describe("User Name Change Flow", () => {

‎tests/change-password.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect, test } from "./test";
22
import { testWithNewUser } from "./test-with-new-user";
33

44
test.describe("User Password Change Flow", () => {

‎tests/check-setup.spec.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ test("is already logged in", async ({ page }) => {
1212
await expect(
1313
page.getByRole("heading", { name: "Team Settings" }),
1414
).toBeVisible();
15+
await expect(page.getByTestId("team-member").first()).toBeVisible();
1516
});

‎tests/plan-upgrade.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect, test } from "./test";
22
import { testWithNewUser } from "./test-with-new-user";
33

44
test.describe("Plan Upgrade Flow", () => {

‎tests/support/faults.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import type { Page, TestInfo } from "@playwright/test";
2+
3+
export const implementedFaults = [
4+
"api-team-500",
5+
"script-404",
6+
"unexpected-dashboard-redirect",
7+
] as const;
8+
9+
export type FaultName = (typeof implementedFaults)[number];
10+
11+
const faultTargets: Record<FaultName, string[]> = {
12+
"api-team-500": [
13+
"should change user name in general settings and verify it appears in team members list",
14+
],
15+
"script-404": [
16+
"should successfully change password and sign in with new credentials",
17+
],
18+
"unexpected-dashboard-redirect": [
19+
"should navigate to dashboard activity section and verify user activities",
20+
],
21+
};
22+
23+
export async function installFaultsForTest(page: Page, testInfo: TestInfo) {
24+
const faults = process.env.FAULTS?.trim();
25+
26+
for (const faultName of implementedFaults) {
27+
if (faults !== "all" && faults !== faultName) {
28+
continue;
29+
}
30+
31+
if (!faultTargets[faultName].includes(testInfo.title)) {
32+
continue;
33+
}
34+
35+
switch (faultName) {
36+
case "api-team-500":
37+
await installApiTeam500(page);
38+
break;
39+
case "script-404":
40+
await installScript404(page);
41+
break;
42+
case "unexpected-dashboard-redirect":
43+
await installUnexpectedDashboardRedirect(page);
44+
break;
45+
}
46+
}
47+
}
48+
49+
async function installApiTeam500(page: Page) {
50+
const faultName = "api-team-500";
51+
52+
await page.route("**/api/team", async (route) => {
53+
await route.fulfill({
54+
status: 500,
55+
contentType: "application/json",
56+
headers: {
57+
"x-fault-injected": faultName,
58+
},
59+
body: JSON.stringify({ error: `Fault injected: ${faultName}` }),
60+
});
61+
});
62+
}
63+
64+
async function installScript404(page: Page) {
65+
const faultName = "script-404";
66+
67+
await page.route("**/*", async (route) => {
68+
if (route.request().resourceType() === "script") {
69+
await route.fulfill({
70+
status: 404,
71+
contentType: "application/javascript",
72+
headers: {
73+
"x-fault-injected": faultName,
74+
},
75+
body: `throw new Error("Fault injected: ${faultName}");`,
76+
});
77+
return;
78+
}
79+
80+
await route.continue();
81+
});
82+
}
83+
84+
async function installUnexpectedDashboardRedirect(page: Page) {
85+
const faultName = "unexpected-dashboard-redirect";
86+
87+
await page.route("**/dashboard", async (route) => {
88+
if (route.request().resourceType() === "document") {
89+
await route.fulfill({
90+
status: 302,
91+
headers: {
92+
location: "/sign-in",
93+
"x-fault-injected": faultName,
94+
},
95+
body: "",
96+
});
97+
return;
98+
}
99+
100+
await route.continue();
101+
});
102+
}

‎tests/team-invitation.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect, test } from "./test";
22
import { testWithNewUser } from "./test-with-new-user";
33

44
test.describe("Team Invitation Flow", () => {
@@ -55,7 +55,7 @@ test.describe("Team Invitation Flow", () => {
5555
});
5656

5757
await test.step("Complete signup process with invitation", async () => {
58-
await page.goto("/sign-up?inviteId=" + invitationId);
58+
await page.goto(`/sign-up?inviteId=${invitationId}`);
5959
await expect(
6060
page.getByRole("heading", { name: "Create your account" }),
6161
).toBeVisible();

‎tests/test-with-new-user.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { test } from "@playwright/test";
21
import {
32
type ApiUser,
43
createUser,
54
deleteUser,
65
getCookieForSession,
76
} from "../setup-utils";
7+
import { test } from "./test";
88

99
// This is an _automatic_ fixture, that when used will provide the test with a unique new user
1010
// https://playwright.dev/docs/test-fixtures#automatic-fixtures
@@ -14,12 +14,16 @@ export const testWithNewUser = test.extend<{
1414
}>({
1515
newUser: [
1616
async ({ baseURL, page }, use) => {
17-
const { email, password, session } = await createUser(baseURL!);
18-
await page.context().addCookies([getCookieForSession(session, baseURL!)]);
17+
if (!baseURL) {
18+
throw new Error("baseURL is required to create a test user");
19+
}
20+
21+
const { email, password, session } = await createUser(baseURL);
22+
await page.context().addCookies([getCookieForSession(session, baseURL)]);
1923

2024
await use({ email, password, session });
2125

22-
await deleteUser(baseURL!, email);
26+
await deleteUser(baseURL, email);
2327
},
2428
{ auto: true },
2529
],

0 commit comments

Comments
 (0)