rrweb-browser-test 1.0.1 → 2.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rrweb-browser-test",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "description": "RRWeb events to generating browser test scripts",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -47,5 +47,8 @@
47
47
  "bugs": {
48
48
  "url": "https://github.com/Archish27/rrweb-browser-test/issues"
49
49
  },
50
- "homepage": "https://github.com/Archish27/rrweb-browser-test#readme"
50
+ "homepage": "https://github.com/Archish27/rrweb-browser-test#readme",
51
+ "dependencies": {
52
+ "@rrweb/types": "^2.0.0-alpha.17"
53
+ }
51
54
  }
package/src/core.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { eventWithTime } from '@rrweb/types';
2
+ import { generatePlaywrightTests } from './playwright';
3
+ import { TestFramework, type TestOptions } from './types';
4
+
5
+ const generateTests = (
6
+ events: Array<eventWithTime | string>,
7
+ options?: TestOptions,
8
+ ): string => {
9
+ switch (options?.framework) {
10
+ case TestFramework.PLAYWRIGHT:
11
+ return generatePlaywrightTests(events);
12
+ default:
13
+ return generatePlaywrightTests(events);
14
+ }
15
+ };
16
+
17
+ export const generateBrowserTests = (
18
+ events: Array<eventWithTime | string>,
19
+ options?: TestOptions,
20
+ ): string => {
21
+ return generateTests(events, options);
22
+ };
package/src/index.ts CHANGED
@@ -1 +1,5 @@
1
- export { add } from './utils.js';
1
+ import { generateBrowserTests } from './core';
2
+
3
+ import e from '/home/archish/Downloads/beb.json';
4
+
5
+ console.log(generateBrowserTests(e));
@@ -0,0 +1,125 @@
1
+ import {
2
+ EventType,
3
+ IncrementalSource,
4
+ MouseInteractions,
5
+ type eventWithTime,
6
+ type fullSnapshotEvent,
7
+ type inputData,
8
+ type mediaInteractionData,
9
+ type mouseInteractionData,
10
+ type mousemoveData,
11
+ type mutationData,
12
+ type scrollData,
13
+ type viewportResizeData,
14
+ } from '@rrweb/types';
15
+ import { buildNodeMapFromFullSnapshots, indent } from '../utils';
16
+
17
+ export const generatePlaywrightTests = (
18
+ events: Array<eventWithTime | string>,
19
+ ): string => {
20
+ let code = '';
21
+
22
+ code += `import { test, expect } from '@playwright/test';\n\n`;
23
+ code += `test('Generated Playwright Test from rrweb events with tabs', async ({ page, context }) => {\n`;
24
+
25
+ const fullSnapshotEvents = events.filter(
26
+ (event) => (event as eventWithTime).type === EventType.FullSnapshot,
27
+ ) as fullSnapshotEvent[];
28
+ const nodeMap = buildNodeMapFromFullSnapshots(fullSnapshotEvents);
29
+
30
+ code += `${indent(2)}const newTab = await context.newPage();\n`;
31
+
32
+ events.forEach((event: eventWithTime | string) => {
33
+ const e = event as eventWithTime;
34
+ switch (e.type) {
35
+ case EventType.Meta:
36
+ if (e.data.href) {
37
+ code += `${indent(2)}await page.goto('${e.data.href}');\n`;
38
+ code += `${indent(2)}await expect(page).toHaveURL('${e.data.href}');\n`;
39
+ }
40
+ if (e.data.width && e.data.height) {
41
+ code += `${indent(2)}await page.setViewportSize({ width: ${e.data.width}, height: ${e.data.height} });\n`;
42
+ code += `${indent(2)}await expect(page).toHaveViewportSize({ width: ${e.data.width}, height: ${e.data.height} });\n`; // Assert viewport size
43
+ }
44
+ break;
45
+
46
+ case EventType.IncrementalSnapshot:
47
+ const data = e.data;
48
+ const type = data.source;
49
+ let d;
50
+ switch (type) {
51
+ case IncrementalSource.MouseMove || IncrementalSource.TouchMove:
52
+ d = data as mousemoveData;
53
+ if (d.positions?.[0]?.x && d.positions?.[0]?.y) {
54
+ code += `${indent(2)}await page.mouse.move(${d.positions[0].x}, ${d.positions[0].y});\n`;
55
+ }
56
+ break;
57
+
58
+ case IncrementalSource.MouseInteraction:
59
+ d = data as mouseInteractionData;
60
+ if (d.type === MouseInteractions.Click) {
61
+ const clickNode = nodeMap.get(d.id);
62
+ const id = clickNode?.attributes?.id
63
+ ? `#${clickNode?.attributes?.id}`
64
+ : clickNode?.tagName === 'nav'
65
+ ? `${clickNode?.tagName}:text-is("${clickNode.childNodes
66
+ ?.filter((f) => f.tagName === 'a')
67
+ ?.map((node) =>
68
+ node.childNodes?.map(
69
+ (n1) => (n1 as any)?.['textContent'],
70
+ ),
71
+ )
72
+ .join(' ')}")`
73
+ : `${clickNode?.tagName}.${clickNode?.attributes?.class}`;
74
+
75
+ if (d.x && d.y) {
76
+ code += `${indent(2)}await page.locator(\`${id}\`).click();\n`;
77
+ code += `${indent(2)}await expect(page.locator(\`${id}\`)).toBeVisible();\n`; // Assert visibility after click
78
+ }
79
+ break;
80
+ }
81
+ break;
82
+
83
+ case IncrementalSource.Scroll:
84
+ d = data as scrollData;
85
+ if (d.x && d.y) {
86
+ code += `${indent(2)}await page.mouse.wheel(() => { document.querySelector('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]').scrollTo(${d.x}, ${d.y}); });\n`;
87
+ code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toBeVisible();\n`; // Assert element visibility after scroll
88
+ }
89
+ break;
90
+
91
+ case IncrementalSource.ViewportResize:
92
+ d = data as viewportResizeData;
93
+ if (d.width && d.height) {
94
+ code += `${indent(2)}await page.setViewportSize({ width: ${d.width}, height: ${d.height} });\n`;
95
+ code += `${indent(2)}await expect(page).toHaveViewportSize({ width: ${d.width}, height: ${d.height} });\n`; // Assert viewport size
96
+ }
97
+ break;
98
+
99
+ case IncrementalSource.Input:
100
+ d = data as inputData;
101
+ if (d.text) {
102
+ code += `${indent(2)}await page.fill('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]', '${d.text}');\n`;
103
+ code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toHaveValue('${d.text}');\n`; // Assert input value
104
+ }
105
+ break;
106
+
107
+ case IncrementalSource.MediaInteraction:
108
+ d = data as mediaInteractionData;
109
+ if (d.type) {
110
+ code += `${indent(2)}await page.$eval('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]', el => el.${d.type}());\n`;
111
+ code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toBeVisible();\n`; // Assert visibility of the media element
112
+ }
113
+ break;
114
+ }
115
+ break;
116
+
117
+ default:
118
+ break;
119
+ }
120
+ });
121
+
122
+ code += `${indent(1)}});\n`;
123
+
124
+ return code;
125
+ };
package/src/types.ts ADDED
@@ -0,0 +1,17 @@
1
+ export type TestOptions = {
2
+ framework: 'playwright' | 'puppeteer' | 'cypress' | 'selenium';
3
+ };
4
+
5
+ export type SerializedNodeWithId = {
6
+ id: number;
7
+ tagName?: string;
8
+ attributes?: Record<string, any>;
9
+ childNodes?: SerializedNodeWithId[];
10
+ };
11
+
12
+ export const TestFramework = {
13
+ PLAYWRIGHT: 'playwright',
14
+ PUPPETEER: 'puppeteer',
15
+ CYPRESS: 'cypress',
16
+ SELENIUM: 'selenium',
17
+ };
package/src/utils.test.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { add } from './utils.js';
2
- import { test, expect } from 'vitest';
1
+ import { expect, test } from 'vitest';
2
+ import { indent } from './utils.js';
3
3
 
4
4
  test('add', () => {
5
- expect(add(1, 2)).toBe(3);
5
+ expect(indent(2)).toBe(' ');
6
6
  });
package/src/utils.ts CHANGED
@@ -1 +1,36 @@
1
- export const add = (a: number, b: number) => a + b;
1
+ import type { fullSnapshotEvent } from '@rrweb/types';
2
+ import type { SerializedNodeWithId } from './types';
3
+
4
+ export const indent = (level: number) => ' '.repeat(level);
5
+ /**
6
+ * Builds a map of all nodes by their IDs from a FullSnapshotEvent.
7
+ * @param rootNode - The root node of the snapshot tree.
8
+ * @returns A map where keys are node IDs and values are the corresponding nodes.
9
+ */
10
+ export const buildNodeMapFromFullSnapshots = (
11
+ fullSnapshots: fullSnapshotEvent[],
12
+ ): Map<number, SerializedNodeWithId> => {
13
+ const idMap = new Map<number, SerializedNodeWithId>();
14
+
15
+ // Iterate over each FullSnapshotEvent
16
+ fullSnapshots.forEach((snapshot: fullSnapshotEvent) => {
17
+ const rootNode = snapshot.data.node; // Get the root node of the full snapshot
18
+
19
+ // Recursive function to process each node and add it to the map
20
+ function processNode(node: SerializedNodeWithId) {
21
+ idMap.set(node.id, node); // Add node to map using its ID
22
+
23
+ // Recursively process child nodes, if any
24
+ if (node.childNodes) {
25
+ node.childNodes.forEach((childNode) => {
26
+ processNode(childNode);
27
+ });
28
+ }
29
+ }
30
+
31
+ // Start the recursive node processing from the root node
32
+ processNode(rootNode);
33
+ });
34
+
35
+ return idMap;
36
+ };