rrweb-browser-test 1.0.1 → 2.0.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/CHANGELOG.md +12 -0
- package/dist/index.cjs +151 -3
- package/dist/index.d.cts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +154 -2
- package/package.json +5 -2
- package/src/core.ts +22 -0
- package/src/index.ts +1 -1
- package/src/playwright/index.ts +125 -0
- package/src/types.ts +17 -0
- package/src/utils.test.ts +3 -3
- package/src/utils.ts +36 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# rrweb-browser-test
|
|
2
2
|
|
|
3
|
+
## 2.0.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 76f7ce2: Exporting browser test function
|
|
8
|
+
|
|
9
|
+
## 2.0.0
|
|
10
|
+
|
|
11
|
+
### Major Changes
|
|
12
|
+
|
|
13
|
+
- e5ae4fa: :sparkle: Playwright auto-generation script code from rrweb events"
|
|
14
|
+
|
|
3
15
|
## 1.0.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -20,13 +20,161 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
-
|
|
23
|
+
generateBrowserTests: () => generateBrowserTests
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
26
|
|
|
27
|
+
// src/playwright/index.ts
|
|
28
|
+
var import_types = require("@rrweb/types");
|
|
29
|
+
|
|
27
30
|
// src/utils.ts
|
|
28
|
-
var
|
|
31
|
+
var indent = (level) => " ".repeat(level);
|
|
32
|
+
var buildNodeMapFromFullSnapshots = (fullSnapshots) => {
|
|
33
|
+
const idMap = /* @__PURE__ */ new Map();
|
|
34
|
+
fullSnapshots.forEach((snapshot) => {
|
|
35
|
+
const rootNode = snapshot.data.node;
|
|
36
|
+
function processNode(node) {
|
|
37
|
+
idMap.set(node.id, node);
|
|
38
|
+
if (node.childNodes) {
|
|
39
|
+
node.childNodes.forEach((childNode) => {
|
|
40
|
+
processNode(childNode);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
processNode(rootNode);
|
|
45
|
+
});
|
|
46
|
+
return idMap;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/playwright/index.ts
|
|
50
|
+
var generatePlaywrightTests = (events) => {
|
|
51
|
+
let code = "";
|
|
52
|
+
code += `import { test, expect } from '@playwright/test';
|
|
53
|
+
|
|
54
|
+
`;
|
|
55
|
+
code += `test('Generated Playwright Test from rrweb events with tabs', async ({ page, context }) => {
|
|
56
|
+
`;
|
|
57
|
+
const fullSnapshotEvents = events.filter(
|
|
58
|
+
(event) => event.type === import_types.EventType.FullSnapshot
|
|
59
|
+
);
|
|
60
|
+
const nodeMap = buildNodeMapFromFullSnapshots(fullSnapshotEvents);
|
|
61
|
+
code += `${indent(2)}const newTab = await context.newPage();
|
|
62
|
+
`;
|
|
63
|
+
events.forEach((event) => {
|
|
64
|
+
const e = event;
|
|
65
|
+
switch (e.type) {
|
|
66
|
+
case import_types.EventType.Meta:
|
|
67
|
+
if (e.data.href) {
|
|
68
|
+
code += `${indent(2)}await page.goto('${e.data.href}');
|
|
69
|
+
`;
|
|
70
|
+
code += `${indent(2)}await expect(page).toHaveURL('${e.data.href}');
|
|
71
|
+
`;
|
|
72
|
+
}
|
|
73
|
+
if (e.data.width && e.data.height) {
|
|
74
|
+
code += `${indent(2)}await page.setViewportSize({ width: ${e.data.width}, height: ${e.data.height} });
|
|
75
|
+
`;
|
|
76
|
+
code += `${indent(2)}await expect(page).toHaveViewportSize({ width: ${e.data.width}, height: ${e.data.height} });
|
|
77
|
+
`;
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
case import_types.EventType.IncrementalSnapshot:
|
|
81
|
+
const data = e.data;
|
|
82
|
+
const type = data.source;
|
|
83
|
+
let d;
|
|
84
|
+
switch (type) {
|
|
85
|
+
case (import_types.IncrementalSource.MouseMove || import_types.IncrementalSource.TouchMove):
|
|
86
|
+
d = data;
|
|
87
|
+
if (d.positions?.[0]?.x && d.positions?.[0]?.y) {
|
|
88
|
+
code += `${indent(2)}await page.mouse.move(${d.positions[0].x}, ${d.positions[0].y});
|
|
89
|
+
`;
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
case import_types.IncrementalSource.MouseInteraction:
|
|
93
|
+
d = data;
|
|
94
|
+
if (d.type === import_types.MouseInteractions.Click) {
|
|
95
|
+
const clickNode = nodeMap.get(d.id);
|
|
96
|
+
const id = clickNode?.attributes?.id ? `#${clickNode?.attributes?.id}` : clickNode?.tagName === "nav" ? `${clickNode?.tagName}:text-is("${clickNode.childNodes?.filter((f) => f.tagName === "a")?.map(
|
|
97
|
+
(node) => node.childNodes?.map(
|
|
98
|
+
(n1) => n1?.["textContent"]
|
|
99
|
+
)
|
|
100
|
+
).join(" ")}")` : `${clickNode?.tagName}.${clickNode?.attributes?.class}`;
|
|
101
|
+
if (d.x && d.y) {
|
|
102
|
+
code += `${indent(2)}await page.locator(\`${id}\`).click();
|
|
103
|
+
`;
|
|
104
|
+
code += `${indent(2)}await expect(page.locator(\`${id}\`)).toBeVisible();
|
|
105
|
+
`;
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
case import_types.IncrementalSource.Scroll:
|
|
111
|
+
d = data;
|
|
112
|
+
if (d.x && d.y) {
|
|
113
|
+
code += `${indent(2)}await page.mouse.wheel(() => { document.querySelector('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]').scrollTo(${d.x}, ${d.y}); });
|
|
114
|
+
`;
|
|
115
|
+
code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toBeVisible();
|
|
116
|
+
`;
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
case import_types.IncrementalSource.ViewportResize:
|
|
120
|
+
d = data;
|
|
121
|
+
if (d.width && d.height) {
|
|
122
|
+
code += `${indent(2)}await page.setViewportSize({ width: ${d.width}, height: ${d.height} });
|
|
123
|
+
`;
|
|
124
|
+
code += `${indent(2)}await expect(page).toHaveViewportSize({ width: ${d.width}, height: ${d.height} });
|
|
125
|
+
`;
|
|
126
|
+
}
|
|
127
|
+
break;
|
|
128
|
+
case import_types.IncrementalSource.Input:
|
|
129
|
+
d = data;
|
|
130
|
+
if (d.text) {
|
|
131
|
+
code += `${indent(2)}await page.fill('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]', '${d.text}');
|
|
132
|
+
`;
|
|
133
|
+
code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toHaveValue('${d.text}');
|
|
134
|
+
`;
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
case import_types.IncrementalSource.MediaInteraction:
|
|
138
|
+
d = data;
|
|
139
|
+
if (d.type) {
|
|
140
|
+
code += `${indent(2)}await page.$eval('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]', el => el.${d.type}());
|
|
141
|
+
`;
|
|
142
|
+
code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toBeVisible();
|
|
143
|
+
`;
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
break;
|
|
148
|
+
default:
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
code += `${indent(1)}});
|
|
153
|
+
`;
|
|
154
|
+
return code;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// src/types.ts
|
|
158
|
+
var TestFramework = {
|
|
159
|
+
PLAYWRIGHT: "playwright",
|
|
160
|
+
PUPPETEER: "puppeteer",
|
|
161
|
+
CYPRESS: "cypress",
|
|
162
|
+
SELENIUM: "selenium"
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// src/core.ts
|
|
166
|
+
var generateTests = (events, options) => {
|
|
167
|
+
switch (options?.framework) {
|
|
168
|
+
case TestFramework.PLAYWRIGHT:
|
|
169
|
+
return generatePlaywrightTests(events);
|
|
170
|
+
default:
|
|
171
|
+
return generatePlaywrightTests(events);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
var generateBrowserTests = (events, options) => {
|
|
175
|
+
return generateTests(events, options);
|
|
176
|
+
};
|
|
29
177
|
// Annotate the CommonJS export names for ESM import in node:
|
|
30
178
|
0 && (module.exports = {
|
|
31
|
-
|
|
179
|
+
generateBrowserTests
|
|
32
180
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { eventWithTime } from '@rrweb/types';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
type TestOptions = {
|
|
4
|
+
framework: 'playwright' | 'puppeteer' | 'cypress' | 'selenium';
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
declare const generateBrowserTests: (events: Array<eventWithTime | string>, options?: TestOptions) => string;
|
|
8
|
+
|
|
9
|
+
export { generateBrowserTests };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { eventWithTime } from '@rrweb/types';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
type TestOptions = {
|
|
4
|
+
framework: 'playwright' | 'puppeteer' | 'cypress' | 'selenium';
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
declare const generateBrowserTests: (events: Array<eventWithTime | string>, options?: TestOptions) => string;
|
|
8
|
+
|
|
9
|
+
export { generateBrowserTests };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,157 @@
|
|
|
1
|
+
// src/playwright/index.ts
|
|
2
|
+
import {
|
|
3
|
+
EventType,
|
|
4
|
+
IncrementalSource,
|
|
5
|
+
MouseInteractions
|
|
6
|
+
} from "@rrweb/types";
|
|
7
|
+
|
|
1
8
|
// src/utils.ts
|
|
2
|
-
var
|
|
9
|
+
var indent = (level) => " ".repeat(level);
|
|
10
|
+
var buildNodeMapFromFullSnapshots = (fullSnapshots) => {
|
|
11
|
+
const idMap = /* @__PURE__ */ new Map();
|
|
12
|
+
fullSnapshots.forEach((snapshot) => {
|
|
13
|
+
const rootNode = snapshot.data.node;
|
|
14
|
+
function processNode(node) {
|
|
15
|
+
idMap.set(node.id, node);
|
|
16
|
+
if (node.childNodes) {
|
|
17
|
+
node.childNodes.forEach((childNode) => {
|
|
18
|
+
processNode(childNode);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
processNode(rootNode);
|
|
23
|
+
});
|
|
24
|
+
return idMap;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// src/playwright/index.ts
|
|
28
|
+
var generatePlaywrightTests = (events) => {
|
|
29
|
+
let code = "";
|
|
30
|
+
code += `import { test, expect } from '@playwright/test';
|
|
31
|
+
|
|
32
|
+
`;
|
|
33
|
+
code += `test('Generated Playwright Test from rrweb events with tabs', async ({ page, context }) => {
|
|
34
|
+
`;
|
|
35
|
+
const fullSnapshotEvents = events.filter(
|
|
36
|
+
(event) => event.type === EventType.FullSnapshot
|
|
37
|
+
);
|
|
38
|
+
const nodeMap = buildNodeMapFromFullSnapshots(fullSnapshotEvents);
|
|
39
|
+
code += `${indent(2)}const newTab = await context.newPage();
|
|
40
|
+
`;
|
|
41
|
+
events.forEach((event) => {
|
|
42
|
+
const e = event;
|
|
43
|
+
switch (e.type) {
|
|
44
|
+
case EventType.Meta:
|
|
45
|
+
if (e.data.href) {
|
|
46
|
+
code += `${indent(2)}await page.goto('${e.data.href}');
|
|
47
|
+
`;
|
|
48
|
+
code += `${indent(2)}await expect(page).toHaveURL('${e.data.href}');
|
|
49
|
+
`;
|
|
50
|
+
}
|
|
51
|
+
if (e.data.width && e.data.height) {
|
|
52
|
+
code += `${indent(2)}await page.setViewportSize({ width: ${e.data.width}, height: ${e.data.height} });
|
|
53
|
+
`;
|
|
54
|
+
code += `${indent(2)}await expect(page).toHaveViewportSize({ width: ${e.data.width}, height: ${e.data.height} });
|
|
55
|
+
`;
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
case EventType.IncrementalSnapshot:
|
|
59
|
+
const data = e.data;
|
|
60
|
+
const type = data.source;
|
|
61
|
+
let d;
|
|
62
|
+
switch (type) {
|
|
63
|
+
case (IncrementalSource.MouseMove || IncrementalSource.TouchMove):
|
|
64
|
+
d = data;
|
|
65
|
+
if (d.positions?.[0]?.x && d.positions?.[0]?.y) {
|
|
66
|
+
code += `${indent(2)}await page.mouse.move(${d.positions[0].x}, ${d.positions[0].y});
|
|
67
|
+
`;
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
case IncrementalSource.MouseInteraction:
|
|
71
|
+
d = data;
|
|
72
|
+
if (d.type === MouseInteractions.Click) {
|
|
73
|
+
const clickNode = nodeMap.get(d.id);
|
|
74
|
+
const id = clickNode?.attributes?.id ? `#${clickNode?.attributes?.id}` : clickNode?.tagName === "nav" ? `${clickNode?.tagName}:text-is("${clickNode.childNodes?.filter((f) => f.tagName === "a")?.map(
|
|
75
|
+
(node) => node.childNodes?.map(
|
|
76
|
+
(n1) => n1?.["textContent"]
|
|
77
|
+
)
|
|
78
|
+
).join(" ")}")` : `${clickNode?.tagName}.${clickNode?.attributes?.class}`;
|
|
79
|
+
if (d.x && d.y) {
|
|
80
|
+
code += `${indent(2)}await page.locator(\`${id}\`).click();
|
|
81
|
+
`;
|
|
82
|
+
code += `${indent(2)}await expect(page.locator(\`${id}\`)).toBeVisible();
|
|
83
|
+
`;
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
case IncrementalSource.Scroll:
|
|
89
|
+
d = data;
|
|
90
|
+
if (d.x && d.y) {
|
|
91
|
+
code += `${indent(2)}await page.mouse.wheel(() => { document.querySelector('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]').scrollTo(${d.x}, ${d.y}); });
|
|
92
|
+
`;
|
|
93
|
+
code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toBeVisible();
|
|
94
|
+
`;
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
case IncrementalSource.ViewportResize:
|
|
98
|
+
d = data;
|
|
99
|
+
if (d.width && d.height) {
|
|
100
|
+
code += `${indent(2)}await page.setViewportSize({ width: ${d.width}, height: ${d.height} });
|
|
101
|
+
`;
|
|
102
|
+
code += `${indent(2)}await expect(page).toHaveViewportSize({ width: ${d.width}, height: ${d.height} });
|
|
103
|
+
`;
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
case IncrementalSource.Input:
|
|
107
|
+
d = data;
|
|
108
|
+
if (d.text) {
|
|
109
|
+
code += `${indent(2)}await page.fill('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]', '${d.text}');
|
|
110
|
+
`;
|
|
111
|
+
code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toHaveValue('${d.text}');
|
|
112
|
+
`;
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
case IncrementalSource.MediaInteraction:
|
|
116
|
+
d = data;
|
|
117
|
+
if (d.type) {
|
|
118
|
+
code += `${indent(2)}await page.$eval('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]', el => el.${d.type}());
|
|
119
|
+
`;
|
|
120
|
+
code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toBeVisible();
|
|
121
|
+
`;
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
default:
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
code += `${indent(1)}});
|
|
131
|
+
`;
|
|
132
|
+
return code;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// src/types.ts
|
|
136
|
+
var TestFramework = {
|
|
137
|
+
PLAYWRIGHT: "playwright",
|
|
138
|
+
PUPPETEER: "puppeteer",
|
|
139
|
+
CYPRESS: "cypress",
|
|
140
|
+
SELENIUM: "selenium"
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// src/core.ts
|
|
144
|
+
var generateTests = (events, options) => {
|
|
145
|
+
switch (options?.framework) {
|
|
146
|
+
case TestFramework.PLAYWRIGHT:
|
|
147
|
+
return generatePlaywrightTests(events);
|
|
148
|
+
default:
|
|
149
|
+
return generatePlaywrightTests(events);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
var generateBrowserTests = (events, options) => {
|
|
153
|
+
return generateTests(events, options);
|
|
154
|
+
};
|
|
3
155
|
export {
|
|
4
|
-
|
|
156
|
+
generateBrowserTests
|
|
5
157
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rrweb-browser-test",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
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 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { generateBrowserTests } from './core';
|
|
@@ -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
package/src/utils.ts
CHANGED
|
@@ -1 +1,36 @@
|
|
|
1
|
-
|
|
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
|
+
};
|