rrweb-browser-test 2.1.4 → 2.3.0

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/README.md CHANGED
@@ -1,33 +1,134 @@
1
1
  # rrweb-browser-test
2
2
 
3
- Auto-generating browser tests from rrweb recording events. Following test frameworks supported:
4
-
5
- - Playwright
6
- - Puppeteer
7
- - Cypress
8
- - Selenium
9
-
10
- ### Example
11
-
12
- Specify your recorded rrweb as `events.json`. The following code will generate auto-generate test script based on recorded events.
13
-
14
- ```javascript
15
- import { generateBrowserTests, TestFramework } from '../dist/index.cjs';
16
- import events from 'events.json' with { type: 'json' };
17
-
18
- console.log(generateBrowserTests(events).toCode());
19
- console.log(
20
- generateBrowserTests(events, {
21
- framework: TestFramework.PLAYWRIGHT,
22
- }).toCode(),
23
- );
24
- console.log(
25
- generateBrowserTests(events, { framework: TestFramework.CYPRESS }).toCode(),
26
- );
27
- console.log(
28
- generateBrowserTests(events, { framework: TestFramework.SELENIUM }).toCode(),
29
- );
30
- console.log(
31
- generateBrowserTests(events, { framework: TestFramework.PUPPETEER }).toCode(),
32
- );
3
+ [![CI](https://github.com/Archish27/rrweb-browser-test/actions/workflows/ci.yml/badge.svg)](https://github.com/Archish27/rrweb-browser-test/actions/workflows/ci.yml)
4
+ [![npm version](https://img.shields.io/npm/v/rrweb-browser-test.svg)](https://www.npmjs.com/package/rrweb-browser-test)
5
+ [![npm downloads](https://img.shields.io/npm/dm/rrweb-browser-test.svg)](https://www.npmjs.com/package/rrweb-browser-test)
6
+ [![types](https://img.shields.io/npm/types/rrweb-browser-test.svg)](https://www.npmjs.com/package/rrweb-browser-test)
7
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/rrweb-browser-test)](https://bundlephobia.com/package/rrweb-browser-test)
8
+ [![node](https://img.shields.io/node/v/rrweb-browser-test.svg)](https://www.npmjs.com/package/rrweb-browser-test)
9
+ [![license](https://img.shields.io/github/license/Archish27/rrweb-browser-test.svg)](LICENSE)
10
+ [![PRs welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
11
+
12
+ Turn an [rrweb](https://github.com/rrweb-io/rrweb) session recording into an
13
+ end-to-end test. Record a real user journey once, get a Playwright, Cypress,
14
+ Puppeteer or Selenium script back (or a mobile test for Appium, Espresso,
15
+ XCUITest or Detox).
16
+
17
+ ```ts
18
+ await page.locator('button[data-testid="header-datepicker"]').click();
19
+ await page.locator('div.list-item', { hasText: 'Past week' }).click();
20
+ await page.locator('input[name="email"]').fill('jane@example.com');
21
+ await page.locator('input#remember').setChecked(true, { force: true });
22
+ ```
23
+
24
+ ## Install
25
+
26
+ ```sh
27
+ npm install --save-dev rrweb-browser-test
28
+ ```
29
+
30
+ Works in Node 18+ and ships ESM, CommonJS and TypeScript types.
31
+
32
+ ## Usage
33
+
34
+ ```ts
35
+ import { generateBrowserTests, TestFramework } from 'rrweb-browser-test';
36
+
37
+ const events = await loadRecording(); // the rrweb events array
38
+
39
+ const code = generateBrowserTests(events, {
40
+ framework: TestFramework.PLAYWRIGHT,
41
+ });
42
+ // write `code` to tests/recorded.spec.ts
33
43
  ```
44
+
45
+ `generateBrowserTests` returns the test file as a string. Without options it
46
+ generates Playwright.
47
+
48
+ Try it on the bundled sample recording:
49
+
50
+ ```sh
51
+ npm run build
52
+ node example/generate.mjs # Playwright
53
+ node example/generate.mjs example/events.json cypress
54
+ ```
55
+
56
+ ### Frameworks
57
+
58
+ | `framework` | Target | Output |
59
+ | ------------ | ---------------------- | --------------------------- |
60
+ | `playwright` | Browser | `@playwright/test` spec |
61
+ | `cypress` | Browser | Cypress spec |
62
+ | `puppeteer` | Browser | Jest + Puppeteer |
63
+ | `selenium` | Browser | Jest + `selenium-webdriver` |
64
+ | `appium` | Android / iOS (native) | WebdriverIO + Appium |
65
+ | `espresso` | Android (native) | Espresso (Kotlin) |
66
+ | `xcuitest` | iOS (native) | XCUITest (Swift) |
67
+ | `detox` | React Native | Detox |
68
+
69
+ `MOBILE_FRAMEWORKS` lists the native ones, if you need to tell them apart.
70
+
71
+ ### Input formats
72
+
73
+ `events` can be any of:
74
+
75
+ - rrweb `eventWithTime` objects, as emitted by `rrweb.record()`
76
+ - JSON strings of those events
77
+ - stored rows with stringified fields, such as
78
+ `{ "type": "3", "timestamp": "1700000000000", "data": "{\"source\":2,…}" }`
79
+
80
+ Events are sorted by timestamp, so the order of the input doesn't matter.
81
+
82
+ ## How it works
83
+
84
+ 1. **Replays the DOM.** rrweb identifies elements by numeric ids that restart
85
+ on every page load and change with every DOM mutation. The generator
86
+ replays each full snapshot and mutation in order, so every click, input or
87
+ scroll is resolved against the page as it was at that moment.
88
+ 2. **Finds the element the user meant.** A click on an icon or a `<span>`
89
+ inside a button targets the button (or the nearest link, input, label,
90
+ `role="button"` element, or element with a test id).
91
+ 3. **Builds a selector that is unique at that moment,** in this order:
92
+ 1. `data-testid` / `data-test` / `data-cy` / `data-qa`
93
+ 2. `id`, unless it looks generated (`:r1:`, `rc_…`, uuids)
94
+ 3. `name`, `placeholder`, `aria-label`, `title`, `alt`, or `href` for links
95
+ 4. tag plus stable classes, ignoring hashed and state classes such as
96
+ `css-1x2y3z`, `active` or `is-open`
97
+ 5. the element's text (`hasText` in Playwright, `cy.contains` in Cypress)
98
+ 6. a unique ancestor plus the element, then an `:nth-of-type` path
99
+ 4. **Keeps only what the user did.** rrweb also records value changes made by
100
+ page scripts. Input events count only on elements the user focused or
101
+ clicked (or that rrweb marks `userTriggered`). Keystrokes collapse into a
102
+ single fill, and scroll ticks into the final position.
103
+
104
+ ### Masked inputs
105
+
106
+ When rrweb masks an input (`maskAllInputs`, password fields), the recording
107
+ holds `********` instead of the value. The generator writes a placeholder and
108
+ a TODO instead of typing asterisks:
109
+
110
+ ```ts
111
+ // TODO: this value was masked in the recording — replace with test data
112
+ await page.locator('input[name="password"]').fill('TODO_MASKED_VALUE');
113
+ ```
114
+
115
+ ### Tips for stable tests
116
+
117
+ - Add `data-testid` attributes to the elements users interact with. They
118
+ always win.
119
+ - Generated tests are a starting point. Rename the test, replace masked
120
+ values, and add assertions for what the journey should prove.
121
+ - Mouse moves are kept so hover menus work. Delete them if your flow doesn't
122
+ need them.
123
+
124
+ ## Contributing
125
+
126
+ Contributions are welcome! Read [CONTRIBUTING.md](CONTRIBUTING.md) for setup,
127
+ the project layout and how to add a framework. Please follow the
128
+ [Code of Conduct](CODE_OF_CONDUCT.md).
129
+
130
+ Found a security issue? See [SECURITY.md](SECURITY.md).
131
+
132
+ ## License
133
+
134
+ [MIT](LICENSE) © Archish Thakkar