@empiricalrun/playwright-utils 0.70.0 → 0.70.2

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/docs/auth.md DELETED
@@ -1,116 +0,0 @@
1
- # Authentication utilities
2
-
3
- This builds around playwright's `storageState` primitive which is used to cache
4
- auth related state (in a json file on disk).
5
-
6
- This requires an object of `UserContext` type, which has a `key: string`. The
7
- rest of the object can be custom. See [section](#set-up-usercontext) on how to do that.
8
-
9
- ## Usage
10
-
11
- ### Login if required
12
-
13
- ```ts
14
- import { login } from "../pages/login";
15
- import { test as setup } from "./fixtures";
16
- import { AuthStore } from "@empiricalrun/playwright-utils";
17
-
18
- setup("login to application", async ({ page, userContext }) => {
19
- const auth = new AuthStore(
20
- page,
21
- userContext,
22
- // Pass login method
23
- login,
24
- // Pass login checker method
25
- (page, userContext) => {
26
- const postLoginElement = page.getByRole("button", { name: "Search" });
27
- await postLoginElement.waitFor();
28
- return postLoginElement.isVisible();
29
- }
30
- );
31
- await auth.loginIfRequired();
32
- });
33
- ```
34
-
35
- ### Get auth state location
36
-
37
- ```ts
38
- // in your playwright.config.ts
39
- import { AuthStore } from "@empiricalrun/playwright-utils";
40
-
41
- projects: [
42
- name: "chromium",
43
- use: {
44
- storageState: AuthStore.location(userContext)
45
- // ...
46
- }
47
- ]
48
- ```
49
-
50
- ## Set up UserContext
51
-
52
- - Create the `UserContext` type and implementations of that type in `test-data/index.ts`
53
- - Create `TestOptions` in `fixtures.ts` that has all options that can be configured
54
- - Configure fixtures and `playwright.config.ts` to use `TestOptions` type
55
- - Set the `userContext` in the project.use field
56
- - For setup projects, add `userContext` only
57
- - For test/teardown projects, add `userContext` and `storageState` both
58
-
59
- ```ts
60
- // fixtures.ts
61
- import { baseTestFixture } from "@empiricalrun/playwright-utils/test";
62
- import { UserContext, primaryUser } from "./test-data";
63
-
64
- export type TestOptions = {
65
- userContext: UserContext;
66
- }
67
-
68
- export const test = baseTestFixture(base).extend<TestOptions>({
69
- userContext: [primaryUser, { option: true }],
70
- });
71
- ```
72
-
73
- ```ts
74
- // playwright.config.ts
75
- import { defineConfig } from "@playwright/test";
76
- import { AuthStore } from "@empiricalrun/playwright-utils";
77
- import { TestOptions } from "./tests/fixtures";
78
- import { someTypeOfUser } from "./tests/test-data";
79
-
80
- export default defineConfig<TestOptions>({
81
- ...baseConfig,
82
- projects: [
83
- {
84
- name: "setup",
85
- use: {
86
- // NOTE: Don't send storageState to setup
87
- // Playwright will fail attempting to load non-existent file
88
- // AuthStore finds it own it's own
89
- userContext: someTypeOfUser
90
- },
91
- },
92
- {
93
- name: "chromium",
94
- use: {
95
- userContext: someTypeOfUser
96
- storageState: AuthStore.location(someTypeOfUser)
97
- },
98
- },
99
- ],
100
- });
101
- ```
102
-
103
- ## Structuring tests across projects
104
-
105
- Structure tests based on which user context they depend on
106
- - If 2 tests depend on 2 different user contexts, they will be in 2 different projects
107
-
108
- ### Multiplayer
109
-
110
- If a test requires 2 user context (multi-user scenarios), then
111
- - define user contexts for both the users
112
- - for sequential execution (admin does foo, user does bar)
113
- - use `page` for the admin, and then login on the same page with the other user context
114
- - for parallel execution (host and guest join room together)
115
- - create 2 pages in the fixtures: `hostPage`, `guestPage` belonging to 2 different browser contexts
116
- - in the fixture, use AuthStore to login if required
package/docs/email.md DELETED
@@ -1,97 +0,0 @@
1
- # Email automation
2
-
3
- ## Providers
4
-
5
- The `EmailClient` supports two email providers:
6
-
7
- - **`mailosaur`** (default) — Uses Mailosaur via the dashboard proxy
8
- - **`inbox`** — Uses our self-hosted Cloudflare Worker on `empiricalrun.email`
9
-
10
- Both providers are accessed through the dashboard proxy, so only `EMPIRICALRUN_API_KEY` is required.
11
-
12
- ## Example usage
13
-
14
- ### Dynamic email
15
-
16
- This dynamically generates a random email address that can
17
- be used for the test (e.g. invite a new user).
18
-
19
- ```ts
20
- import { EmailClient } from "@empiricalrun/playwright-utils";
21
- import { expect } from "@playwright/test";
22
-
23
- const client = new EmailClient();
24
- const address = client.getAddress(); // e.g. abc1234@empiricalrun.email
25
-
26
- // Input the `address` in the application
27
- // that sends the email.
28
-
29
- // Get email received on the `address`
30
- const email = await client.waitForEmail();
31
- expect(
32
- email.links.find((l) => l.text === "Join your team")
33
- ).toBeTruthy();
34
- ```
35
-
36
- ### Static email
37
-
38
- This uses a known (static) email that can be used to login
39
- into an application.
40
-
41
- This needs an email id (e.g. `test-login-user`). The email id
42
- is appended with the domain (`empiricalrun.email`) to get the full
43
- email address.
44
-
45
- ```ts
46
- import { EmailClient } from "@empiricalrun/playwright-utils";
47
-
48
- const emailId = `test-login-user`;
49
-
50
- const client = new EmailClient({ emailId });
51
- const address = client.getAddress(); // Returns full address with domain
52
-
53
- // Get email received on the `address`
54
- const email = await client.waitForEmail();
55
-
56
- // Get login OTP
57
- const loginCode = email.codes[0];
58
- ```
59
-
60
- ### Using the inbox provider
61
-
62
- Pass `provider: "inbox"` to use the self-hosted inbox provider.
63
-
64
- ```ts
65
- const client = new EmailClient({ provider: "inbox" });
66
- ```
67
-
68
- ### Filtering emails
69
-
70
- You can filter emails by sender, subject, or body content:
71
-
72
- ```ts
73
- const email = await client.waitForEmail({
74
- from: "noreply@example.com",
75
- subject: "Verify your email",
76
- body: "verification code",
77
- });
78
- ```
79
-
80
- ### Searching from a specific time
81
-
82
- By default, `waitForEmail` includes emails received up to five seconds before
83
- it is called. Pass `receivedAfter` to control the start of the search window.
84
- Capturing the time immediately before the action that sends the email avoids
85
- matching older messages without missing an email that arrives before polling
86
- begins.
87
-
88
- ```ts
89
- const receivedAfter = new Date();
90
-
91
- await page.getByRole("button", { name: "Send code" }).click();
92
-
93
- const email = await client.waitForEmail({
94
- receivedAfter,
95
- subject: "Your login code",
96
- });
97
- ```
package/docs/fixtures.md DELETED
@@ -1,106 +0,0 @@
1
- # Fixtures
2
-
3
- The playwright-utils package provides fixtures that wrap around Playwright's built-in
4
- `page`, `context` fixtures to record videos with Playwright's action annotations
5
- (cursor, target box and action title), which make it easier to see actions taken in a video.
6
-
7
- To use this, you can use the `baseTestFixture` and `extendExpect` imports
8
- in your fixtures file.
9
-
10
- ```ts
11
- import { test as base, expect as baseExpect } from "@playwright/test";
12
- import { baseTestFixture, extendExpect } from "@empiricalrun/playwright-utils/test";
13
-
14
- export const test = baseTestFixture(base);
15
- export const expect = extendExpect(baseExpect);
16
- ```
17
-
18
- ## Agentic behaviors
19
-
20
- `baseTestFixture` patches the playwright page object to add the `locator.query()` vision
21
- method and agentic behaviors like dismissing overlays that block a click or hover.
22
- Opt out with `baseTestFixture(base, { visionMethods: false })` or `{ agentCapabilities: false }`.
23
-
24
- ## Custom Matchers
25
-
26
- The `extendExpect` method adds custom matchers that can be used with Playwright's expect:
27
-
28
- ### Visual Testing
29
-
30
- The `toLookRight` matcher uses LLM to analyze screenshots for visual inconsistencies:
31
-
32
- ```ts
33
- test('page looks visually correct', async ({ page }) => {
34
- await page.goto('https://your-site.com');
35
- const description = "A login page with email and password fields, and a submit button"
36
- await expect(page).toLookRight(description);
37
- });
38
- ```
39
-
40
- ## Advanced Use Cases
41
-
42
- The packages also expose methods to enable advanced use-cases.
43
-
44
- ### Get a new browser context
45
-
46
- This package provides a fixture `customContextPageProvider` which is a good way to create
47
- a fresh, new browser context, and a page inside it.
48
-
49
- There are two benefits of using this to create contexts or pages:
50
- 1. Videos get recorded and attached to the test report
51
- 2. Action annotations are shown in the video
52
-
53
- ```ts
54
- import { test, expect } from "./fixtures";
55
-
56
- test("Example test", async ({ page: builtInPage, customContextPageProvider }) => {
57
- // builtInPage is from default browser context
58
- const { page: newPage, context } = await customContextPageProvider();
59
- // newPage is from this other browser context
60
- });
61
- ```
62
-
63
- ### Send private headers to some hosts only
64
-
65
- `extraHTTPHeaders` sends its headers to every origin a page touches, including third
66
- parties and redirect targets. Pass `scopedHTTPHeaders` to `customContextPageProvider` to
67
- send headers (for example a WAF bypass token) only to the hosts you list:
68
-
69
- ```ts
70
- const { page } = await customContextPageProvider({
71
- scopedHTTPHeaders: {
72
- hosts: ["example.com", "*.example.com", "*.example-cdn.com"],
73
- headers: { "x-waf-bypass": process.env.WAF_BYPASS_TOKEN! },
74
- },
75
- });
76
- ```
77
-
78
- - A host is an exact hostname, or `*.` followed by a domain to match its subdomains
79
- (`*.example-cdn.com` does not match `example-cdn.com` itself).
80
- - Every request is checked on its own, including each redirect hop, cross-origin iframes,
81
- workers, popups and pages opened later with `context.newPage()`.
82
- - The same header names are removed from the context-wide `extraHTTPHeaders` the context
83
- would otherwise inherit, and are never sent to other hosts.
84
- - Headers added this way do not trigger CORS preflights, unlike `extraHTTPHeaders`.
85
- - Chromium only. Requests made with `context.request` / `page.request` do not get the
86
- headers; pass them to those calls yourself.
87
-
88
- ### Inject locator scripts manually
89
-
90
- In some cases, you want to patch a page you created yourself. This is useful if the test
91
- requires `launchPersistentContext`, which means the browser context is created manually.
92
-
93
- With `injectLocatorHighlightScripts`, you still get the locator helpers in any page. For
94
- action annotations in the video, pass `recordVideo: { dir, showActions: { duration: 100 } }`
95
- when you create the context.
96
-
97
- ```ts
98
- import { injectLocatorHighlightScripts, baseTestFixture } from "@empiricalrun/playwright-utils/test";
99
-
100
- export const test = baseTestFixture(base).extend<TestOptions>({
101
- customPage: async ({ page }, use) => {
102
- injectLocatorHighlightScripts(page);
103
- await use(page);
104
- },
105
- });
106
- ```
package/docs/kv.md DELETED
@@ -1,90 +0,0 @@
1
- # KV Store
2
-
3
- A project-scoped key-value store for sharing state across tests, shards, test
4
- runs and agent sessions. Every entry expires, and conditional writes make it
5
- safe to coordinate between parallel shards.
6
-
7
- ## Fixture usage
8
-
9
- ```ts
10
- test("example", async ({ page, kv }) => {
11
- await kv.set("my-key", { some: "data" }, 3600);
12
- const value = await kv.get<{ some: string }>("my-key");
13
- });
14
- ```
15
-
16
- ## Standalone usage
17
-
18
- ```ts
19
- import { KVClient } from "@empiricalrun/playwright-utils";
20
-
21
- const kv = new KVClient();
22
- await kv.set("my-key", { some: "data" }, 3600);
23
- const value = await kv.get<{ some: string }>("my-key");
24
- ```
25
-
26
- ## Keys, values and expiry
27
-
28
- - Keys are 1-512 characters from `A-Z a-z 0-9 . _ : -`. Use `:` to namespace
29
- them, e.g. `async-checks:analytics:1234`.
30
- - Values are any JSON value, up to 1 MB serialized.
31
- - `ttl` is required, in seconds, up to 30 days.
32
-
33
- ## Entries and versions
34
-
35
- `getEntry` returns the value with its metadata. `version` increases on every
36
- write.
37
-
38
- ```ts
39
- const entry = await kv.getEntry("my-key");
40
- // { key, value, version, expires_at, created_at, updated_at, written_by }
41
- ```
42
-
43
- ## Conditional writes
44
-
45
- `set` with `ifAbsent` writes only if the key does not exist; `ifVersion` writes
46
- only if the entry is still at that version. A failed condition throws
47
- `KvPreconditionFailedError`, whose `current` is the entry as it is now.
48
-
49
- ```ts
50
- import { KvPreconditionFailedError } from "@empiricalrun/playwright-utils";
51
-
52
- try {
53
- const lease = await kv.set("lock:checkout", testInfo.testId, 60, {
54
- ifAbsent: true,
55
- });
56
- await kv.refresh("lock:checkout", 60, { ifVersion: lease.version });
57
- } catch (error) {
58
- if (error instanceof KvPreconditionFailedError) {
59
- console.log("held by", error.current?.value);
60
- }
61
- }
62
- ```
63
-
64
- ## Refresh, delete and list
65
-
66
- ```ts
67
- await kv.refresh("my-key", 600);
68
- await kv.delete("my-key", { ifVersion: 3 });
69
-
70
- const page = await kv.list({ prefix: "async-checks:" });
71
- for await (const entry of kv.entries("async-checks:")) {
72
- console.log(entry.key, entry.value);
73
- }
74
- ```
75
-
76
- `refresh` returns `null` and `delete` returns `false` when the key does not
77
- exist. `list` returns metadata only; `entries` pages through every entry with
78
- its value.
79
-
80
- ## Locks across shards
81
-
82
- Tests declared with Playwright's `lock` option (`test("...", { lock: "name" }, ...)`)
83
- also hold that lock across every shard of an Empirical test run. The
84
- `baseTestFixture` fixtures take a KV lease per lock name while the test runs and
85
- add a `lock` annotation with the time spent waiting. Locks are scoped to the
86
- test run.
87
-
88
- ## Requirements
89
-
90
- - `EMPIRICALRUN_API_KEY` environment variable must be set
@@ -1,36 +0,0 @@
1
- # Video Labels
2
-
3
- Pages generate video recordings after test execution, with 1 page generating 1 video file (webm).
4
-
5
- If your test case relies on multiple pages (e.g. for multi-user or multi-app flows), it can get difficult to
6
- know which page does "video-1.webm" belong to.
7
-
8
- To solve this, you should set video labels for pages. This will enable you to identify videos faster.
9
-
10
- ## Usage
11
-
12
- ```typescript
13
- import { setVideoLabel } from '@empiricalrun/playwright-utils/test';
14
-
15
- test('my test', async ({ page }) => {
16
- setVideoLabel(page, 'checkout-flow');
17
- // Video will be saved as 'checkout-flow.webm'
18
- });
19
- ```
20
-
21
- ## Multiple Contexts
22
-
23
- ```typescript
24
- test('multi-user scenario', async ({ page, customContextPageProvider }) => {
25
- setVideoLabel(page, 'host-page');
26
-
27
- const { page: guestPage } = await customContextPageProvider({ storageState: undefined });
28
- setVideoLabel(guestPage, 'guest-page');
29
- // Videos saved as 'guest-page.webm' and 'host-page.webm'
30
- });
31
- ```
32
-
33
- ## Notes
34
-
35
- - The default behavior is to label videos for multiple pages as: `video-0.webm`, `video-1.webm`, etc.
36
- - If setVideoLabel is called twice for the same page, the last label will be set