@atomicsmash/test-utils 3.0.0 → 4.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,11 +1,15 @@
1
- import type { Page, BrowserContext, PlaywrightTestConfig } from "@playwright/test";
1
+ import type { Page, Browser, PlaywrightTestConfig, PlaywrightTestArgs, PlaywrightTestOptions } from "@playwright/test";
2
2
  import type { SerialFrameSelector } from "axe-core";
3
+ export type WordPressUserRole = "unauthenticated" | "subscriber" | "contributor" | "author" | "editor" | "admin" | "super-admin";
4
+ export type WooCommerceUserRole = "customer" | "shop-manager";
5
+ export type UserRole = WordPressUserRole | WooCommerceUserRole;
3
6
  export type PageList = {
4
7
  url: string;
5
8
  name: string;
6
9
  slug?: string;
7
- restrictedAccess?: boolean;
10
+ minimumUserPrivilege?: UserRole;
8
11
  }[];
12
+ export declare function doesRoleHaveHighEnoughPermissions(userRole: WordPressUserRole | WooCommerceUserRole, minimumRolePermission: WordPressUserRole | WooCommerceUserRole): boolean;
9
13
  export type AuthenticatePageFunction = ({ page }: {
10
14
  page: Page;
11
15
  }, use: (r: Page) => Promise<void>) => Promise<void>;
@@ -27,14 +31,16 @@ export declare const playwrightConfig: {
27
31
  snapshotDir: string;
28
32
  snapshotPathTemplate: string;
29
33
  };
30
- export declare const getLighthouseTest: (logInUser: AuthenticatePageFunction) => import("@playwright/test").TestType<import("@playwright/test").PlaywrightTestArgs & import("@playwright/test").PlaywrightTestOptions & {
31
- authenticatedPage: Page;
32
- context: BrowserContext;
33
- }, import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions & {
34
+ export declare const lighthouseTest: import("playwright/test").TestType<PlaywrightTestArgs & PlaywrightTestOptions, import("playwright/test").PlaywrightWorkerArgs & import("playwright/test").PlaywrightWorkerOptions & {
34
35
  port: number;
36
+ browser: Browser;
35
37
  }>;
36
- type LighthouseTestFunction = Parameters<ReturnType<typeof getLighthouseTest>>[2];
37
- export declare const doLighthouseTest: (pageToTest: PageList[number], type: "desktop" | "mobile", disableAuditLogs?: boolean) => LighthouseTestFunction;
38
+ type LighthouseTestFunction = Parameters<typeof lighthouseTest>[2];
39
+ export declare const doLighthouseTest: (pageToTest: {
40
+ url: string | (() => string | Promise<string>);
41
+ name: string;
42
+ slug?: string;
43
+ }, type: "desktop" | "mobile", disableAuditLogs?: boolean) => LighthouseTestFunction;
38
44
  export declare function checkAccessibility(page: Page, { includes, excludes, }?: {
39
45
  includes?: SerialFrameSelector[];
40
46
  excludes?: SerialFrameSelector[];
@@ -42,10 +48,10 @@ export declare function checkAccessibility(page: Page, { includes, excludes, }?:
42
48
  export declare const checkPrivilegedPages: (pagesToTest: PageList) => ({ page }: {
43
49
  page: Page;
44
50
  }) => Promise<void>;
45
- export declare const checkForLoremIpsum: (pagesToTest: PageList) => ({ authenticatedPage }: {
46
- authenticatedPage: Page;
51
+ export declare const checkForLoremIpsum: (pagesToTest: PageList) => ({ page }: {
52
+ page: Page;
47
53
  }) => Promise<void>;
48
54
  export declare function slugify(stringToSlugify: string): string;
49
- export declare function generateProjectsForAllBrowsers(baseProject: NonNullable<PlaywrightTestConfig["projects"]>[number]): import("@playwright/test").Project<import("@playwright/test").PlaywrightTestOptions & {}, import("@playwright/test").PlaywrightWorkerOptions & {}>[];
55
+ export declare function generateProjectsForAllBrowsers(baseProject: NonNullable<PlaywrightTestConfig["projects"]>[number]): import("playwright/test").Project<PlaywrightTestOptions & {}, import("playwright/test").PlaywrightWorkerOptions & {}>[];
50
56
  export {};
51
57
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,IAAI,EACJ,cAAc,EACd,oBAAoB,EACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAap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KAAK,sBAAsB,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;AAEnE,eAAO,MAAM,gBAAgB,EAAE,CAC9B,UAAU,EAAE;IACX,GAAG,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/C,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;CACd,EACD,IAAI,EAAE,SAAS,GAAG,QAAQ,EAC1B,gBAAgB,CAAC,EAAE,OAAO,KACtB,sBAkCH,CAAC;AAEH,wBAAsB,kBAAkB,CACvC,IAAI,EAAE,IAAI,EACV,EACC,QAAQ,EACR,QAAQ,GACR,GAAE;IACF,QAAQ,CAAC,EAAE,mBAAmB,EAAE,CAAC;IACjC,QAAQ,CAAC,EAAE,mBAAmB,EAAE,CAAC;CAC5B,iBAiCN;AAED,eAAO,MAAM,oBAAoB,gBAClB,QAAQ,gBACL;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,kBAO9B,CAAC;AACH,eAAO,MAAM,kBAAkB,gBAChB,QAAQ,gBACL;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,kBAO9B,CAAC;AAEH,wBAAgB,OAAO,CAAC,eAAe,EAAE,MAAM,UAU9C;AAED,wBAAgB,8BAA8B,CAC7C,WAAW,EAAE,WAAW,CAAC,oBAAoB,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,2HAqDlE"}
package/dist/index.js CHANGED
@@ -1,5 +1,3 @@
1
- import os from "node:os";
2
- import path from "node:path";
3
1
  import { AxeBuilder } from "@axe-core/playwright";
4
2
  import { test as base, chromium, devices } from "@playwright/test";
5
3
  import getPort from "get-port";
@@ -7,6 +5,52 @@ import lighthouseDesktopConfig from "lighthouse/core/config/lr-desktop-config.js
7
5
  import lighthouseMobileConfig from "lighthouse/core/config/lr-mobile-config.js";
8
6
  import { playAudit } from "playwright-lighthouse";
9
7
  const { expect } = base;
8
+ export function doesRoleHaveHighEnoughPermissions(userRole, minimumRolePermission) {
9
+ if (userRole === minimumRolePermission) {
10
+ return true;
11
+ }
12
+ switch (userRole) {
13
+ case "super-admin":
14
+ return true;
15
+ case "admin":
16
+ return minimumRolePermission !== "super-admin";
17
+ case "shop-manager":
18
+ return !["super-admin", "admin"].includes(minimumRolePermission);
19
+ case "editor":
20
+ return !["super-admin", "admin", "shop-manager"].includes(minimumRolePermission);
21
+ case "author":
22
+ return !["super-admin", "admin", "shop-manager", "editor"].includes(minimumRolePermission);
23
+ case "contributor":
24
+ return ![
25
+ "super-admin",
26
+ "admin",
27
+ "shop-manager",
28
+ "editor",
29
+ "author",
30
+ ].includes(minimumRolePermission);
31
+ case "customer":
32
+ return ![
33
+ "super-admin",
34
+ "admin",
35
+ "shop-manager",
36
+ "editor",
37
+ "author",
38
+ "contributor",
39
+ ].includes(minimumRolePermission);
40
+ case "subscriber":
41
+ return ![
42
+ "super-admin",
43
+ "admin",
44
+ "shop-manager",
45
+ "editor",
46
+ "author",
47
+ "contributor",
48
+ "customer",
49
+ ].includes(minimumRolePermission);
50
+ case "unauthenticated":
51
+ return minimumRolePermission === "unauthenticated";
52
+ }
53
+ }
10
54
  export const playwrightConfig = {
11
55
  testDir: "./tests/e2e",
12
56
  timeout: 30 * 1000,
@@ -25,44 +69,39 @@ export const playwrightConfig = {
25
69
  snapshotDir: "./screenshots",
26
70
  snapshotPathTemplate: "{snapshotDir}/{platform}/{testFileName}-snapshots/{arg}{ext}",
27
71
  };
28
- export const getLighthouseTest = (logInUser) => {
29
- const lighthouseTest = base.extend({
30
- port: [
31
- async (_unused, use) => {
32
- // Assign a unique port for each playwright worker to support parallel tests
33
- const port = await getPort();
34
- await use(port);
35
- },
36
- { scope: "worker" },
37
- ],
38
- context: [
39
- async ({ port }, use) => {
40
- const userDataDir = path.join(os.tmpdir(), "pw", String(Math.random()));
41
- const context = await chromium.launchPersistentContext(userDataDir, {
42
- args: [`--remote-debugging-port=${port}`],
43
- });
44
- await use(context);
45
- await context.close();
46
- },
47
- { scope: "test" },
48
- ],
49
- authenticatedPage: [logInUser, { scope: "test" }],
50
- });
51
- lighthouseTest.describe.configure({ mode: "parallel", retries: 3 });
52
- return lighthouseTest;
53
- };
54
- export const doLighthouseTest = (pageToTest, type, disableAuditLogs = false) => async function ({ port, authenticatedPage }) {
55
- await authenticatedPage.goto(pageToTest.url);
72
+ export const lighthouseTest = base.extend({
73
+ port: [
74
+ // eslint-disable-next-line no-empty-pattern -- destructure pattern is required by playwright
75
+ async ({}, use) => {
76
+ // Assign a unique port for each playwright worker to support parallel tests
77
+ const port = await getPort();
78
+ await use(port);
79
+ },
80
+ { scope: "worker" },
81
+ ],
82
+ browser: [
83
+ async ({ port }, use) => {
84
+ const browser = await chromium.launch({
85
+ args: [`--remote-debugging-port=${port}`],
86
+ });
87
+ await use(browser);
88
+ },
89
+ { scope: "worker" },
90
+ ],
91
+ });
92
+ export const doLighthouseTest = (pageToTest, type, disableAuditLogs = false) => async function ({ page, port }) {
93
+ await page.goto(typeof pageToTest.url === "string"
94
+ ? pageToTest.url
95
+ : await pageToTest.url());
56
96
  await playAudit({
57
- // TODO: Wait for playwright-lighthouse to update dependency constraint, then remove this "as" declaration.
58
- page: authenticatedPage,
97
+ page,
98
+ port,
59
99
  thresholds: {
60
100
  performance: 85,
61
101
  accessibility: 85,
62
102
  "best-practices": 85,
63
103
  seo: 85,
64
104
  },
65
- port: port,
66
105
  reports: {
67
106
  formats: {
68
107
  html: true,
@@ -76,7 +115,7 @@ export const doLighthouseTest = (pageToTest, type, disableAuditLogs = false) =>
76
115
  ignoreError: true,
77
116
  disableLogs: disableAuditLogs,
78
117
  });
79
- await authenticatedPage.goto(`file://${process.cwd()}/lighthouse/latest-lighthouse-report/${pageToTest.slug ?? slugify(pageToTest.name)}-${type}.html`);
118
+ await page.goto(`file://${process.cwd()}/lighthouse/latest-lighthouse-report/${pageToTest.slug ?? slugify(pageToTest.name)}-${type}.html`);
80
119
  };
81
120
  export async function checkAccessibility(page, { includes, excludes, } = {}) {
82
121
  let builder;
@@ -105,17 +144,17 @@ export async function checkAccessibility(page, { includes, excludes, } = {}) {
105
144
  })).toEqual([]);
106
145
  }
107
146
  export const checkPrivilegedPages = (pagesToTest) => async ({ page }) => {
108
- for (const pageToTest of pagesToTest.filter((page) => page.restrictedAccess)) {
147
+ for (const pageToTest of pagesToTest) {
109
148
  const response = await page.goto(pageToTest.url);
110
149
  expect(response).not.toBeNull();
111
150
  const nonNullResponse = response;
112
151
  expect(nonNullResponse.status()).toBe(401);
113
152
  }
114
153
  };
115
- export const checkForLoremIpsum = (pagesToTest) => async ({ authenticatedPage }) => {
154
+ export const checkForLoremIpsum = (pagesToTest) => async ({ page }) => {
116
155
  for (const pageToTest of pagesToTest) {
117
- await authenticatedPage.goto(pageToTest.url);
118
- await expect(authenticatedPage.getByText("Lorem ipsum")).not.toBeAttached({
156
+ await page.goto(pageToTest.url);
157
+ await expect(page.getByText("Lorem ipsum")).not.toBeAttached({
119
158
  timeout: 100,
120
159
  });
121
160
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomicsmash/test-utils",
3
- "version": "3.0.0",
3
+ "version": "4.0.0-beta.1",
4
4
  "type": "module",
5
5
  "description": "A collection of helper functions for automated testing with Playwright.",
6
6
  "keywords": [