rrweb-browser-test 2.2.0 → 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 +131 -30
- package/dist/index.cjs +845 -412
- package/dist/index.js +854 -429
- package/package.json +60 -36
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/.claude/settings.local.json +0 -7
- package/.github/workflows/ci.yml +0 -29
- package/.prettierrc +0 -7
- package/CHANGELOG.md +0 -66
- package/example/index.js +0 -18
- package/src/appium/index.ts +0 -50
- package/src/core.ts +0 -43
- package/src/cypress/index.ts +0 -127
- package/src/detox/index.ts +0 -50
- package/src/espresso/index.ts +0 -61
- package/src/index.ts +0 -3
- package/src/mobile/shared.ts +0 -146
- package/src/playwright/index.ts +0 -131
- package/src/puppeteer/index.ts +0 -140
- package/src/selenium/index.ts +0 -136
- package/src/types.ts +0 -37
- package/src/utils.test.ts +0 -6
- package/src/utils.ts +0 -71
- package/src/xcuitest/index.ts +0 -58
- package/tsconfig.json +0 -20
- package/tsup.config.ts +0 -9
package/src/mobile/shared.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EventType,
|
|
3
|
-
IncrementalSource,
|
|
4
|
-
MouseInteractions,
|
|
5
|
-
type eventWithTime,
|
|
6
|
-
type fullSnapshotEvent,
|
|
7
|
-
type inputData,
|
|
8
|
-
type mouseInteractionData,
|
|
9
|
-
type scrollData,
|
|
10
|
-
} from '@rrweb/types';
|
|
11
|
-
import { buildNodeMapFromFullSnapshots } from '../utils';
|
|
12
|
-
import type { SerializedNodeWithId } from '../types';
|
|
13
|
-
|
|
14
|
-
export type MobileLocatorStrategy = 'id' | 'accessibility' | 'text';
|
|
15
|
-
|
|
16
|
-
export interface MobileLocator {
|
|
17
|
-
strategy: MobileLocatorStrategy;
|
|
18
|
-
value: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type MobileAction =
|
|
22
|
-
| { kind: 'launch'; url?: string }
|
|
23
|
-
| { kind: 'tap'; locator?: MobileLocator }
|
|
24
|
-
| { kind: 'input'; locator?: MobileLocator; text: string }
|
|
25
|
-
| { kind: 'swipe'; direction: 'up' | 'down' | 'left' | 'right' };
|
|
26
|
-
|
|
27
|
-
/** Native `resource-id` values often look like `com.app:id/foo` — keep the leaf. */
|
|
28
|
-
export const resourceIdLeaf = (value: string): string => {
|
|
29
|
-
const slash = value.lastIndexOf('/');
|
|
30
|
-
return slash >= 0 ? value.slice(slash + 1) : value;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Derive the most reliable locator for a native element from its captured
|
|
35
|
-
* attributes, preferring a stable id, then an accessibility label, then text.
|
|
36
|
-
*/
|
|
37
|
-
export const mobileLocator = (
|
|
38
|
-
node?: SerializedNodeWithId,
|
|
39
|
-
): MobileLocator | undefined => {
|
|
40
|
-
if (!node) return undefined;
|
|
41
|
-
const a = node.attributes ?? {};
|
|
42
|
-
|
|
43
|
-
const id =
|
|
44
|
-
a['resource-id'] ??
|
|
45
|
-
a['resourceId'] ??
|
|
46
|
-
a['testID'] ??
|
|
47
|
-
a['data-testid'] ??
|
|
48
|
-
a.id;
|
|
49
|
-
if (id) return { strategy: 'id', value: String(id) };
|
|
50
|
-
|
|
51
|
-
const accessibility =
|
|
52
|
-
a['content-desc'] ??
|
|
53
|
-
a['contentDescription'] ??
|
|
54
|
-
a['accessibilityIdentifier'] ??
|
|
55
|
-
a['accessibilityLabel'] ??
|
|
56
|
-
a['aria-label'] ??
|
|
57
|
-
a['name'];
|
|
58
|
-
if (accessibility)
|
|
59
|
-
return { strategy: 'accessibility', value: String(accessibility) };
|
|
60
|
-
|
|
61
|
-
const text =
|
|
62
|
-
a['text'] ??
|
|
63
|
-
(node.childNodes ?? [])
|
|
64
|
-
.map((n) => (n as any)?.textContent as string | undefined)
|
|
65
|
-
.filter(Boolean)
|
|
66
|
-
.join(' ')
|
|
67
|
-
.trim();
|
|
68
|
-
if (text) return { strategy: 'text', value: String(text) };
|
|
69
|
-
|
|
70
|
-
return undefined;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const swipeDirection = (
|
|
74
|
-
d: scrollData | { x?: number; y?: number },
|
|
75
|
-
): 'up' | 'down' | 'left' | 'right' => {
|
|
76
|
-
const x = (d as any).x ?? 0;
|
|
77
|
-
const y = (d as any).y ?? 0;
|
|
78
|
-
if (Math.abs(y) >= Math.abs(x)) return y >= 0 ? 'up' : 'down';
|
|
79
|
-
return x >= 0 ? 'left' : 'right';
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Walk the rrweb event stream and produce an ordered list of high-level mobile
|
|
84
|
-
* actions (launch / tap / input / swipe). Shared by every mobile generator so
|
|
85
|
-
* the frameworks only differ in how they render each action.
|
|
86
|
-
*/
|
|
87
|
-
export const extractMobileActions = (
|
|
88
|
-
events: Array<eventWithTime | string>,
|
|
89
|
-
): MobileAction[] => {
|
|
90
|
-
const actions: MobileAction[] = [];
|
|
91
|
-
|
|
92
|
-
const fullSnapshots = events.filter(
|
|
93
|
-
(event) => (event as eventWithTime).type === EventType.FullSnapshot,
|
|
94
|
-
) as fullSnapshotEvent[];
|
|
95
|
-
const nodeMap = buildNodeMapFromFullSnapshots(fullSnapshots);
|
|
96
|
-
|
|
97
|
-
events.forEach((event) => {
|
|
98
|
-
const e = event as eventWithTime;
|
|
99
|
-
if (e.type === EventType.Meta) {
|
|
100
|
-
if ((e.data as any)?.href)
|
|
101
|
-
actions.push({ kind: 'launch', url: (e.data as any).href });
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
if (e.type !== EventType.IncrementalSnapshot) return;
|
|
105
|
-
|
|
106
|
-
const data = e.data;
|
|
107
|
-
switch (data.source) {
|
|
108
|
-
case IncrementalSource.MouseInteraction: {
|
|
109
|
-
const d = data as mouseInteractionData;
|
|
110
|
-
if (
|
|
111
|
-
d.type === MouseInteractions.Click ||
|
|
112
|
-
d.type === MouseInteractions.TouchEnd
|
|
113
|
-
) {
|
|
114
|
-
actions.push({
|
|
115
|
-
kind: 'tap',
|
|
116
|
-
locator: mobileLocator(nodeMap.get(d.id)),
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
case IncrementalSource.Input: {
|
|
122
|
-
const d = data as inputData;
|
|
123
|
-
if (typeof d.text === 'string') {
|
|
124
|
-
actions.push({
|
|
125
|
-
kind: 'input',
|
|
126
|
-
locator: mobileLocator(nodeMap.get(d.id)),
|
|
127
|
-
text: d.text,
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
case IncrementalSource.Scroll:
|
|
133
|
-
case IncrementalSource.TouchMove: {
|
|
134
|
-
actions.push({
|
|
135
|
-
kind: 'swipe',
|
|
136
|
-
direction: swipeDirection(data as scrollData),
|
|
137
|
-
});
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
140
|
-
default:
|
|
141
|
-
break;
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
return actions;
|
|
146
|
-
};
|
package/src/playwright/index.ts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
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 scrollData,
|
|
12
|
-
type viewportResizeData,
|
|
13
|
-
} from '@rrweb/types';
|
|
14
|
-
import { buildNodeMapFromFullSnapshots, cssSelector, indent } from '../utils';
|
|
15
|
-
|
|
16
|
-
export const generatePlaywrightTests = (
|
|
17
|
-
events: Array<eventWithTime | string>,
|
|
18
|
-
): string => {
|
|
19
|
-
let code = '';
|
|
20
|
-
|
|
21
|
-
code += `import { test, expect } from '@playwright/test';\n\n`;
|
|
22
|
-
code += `test('TODO: name the test block', async ({ page, context }) => {\n`;
|
|
23
|
-
|
|
24
|
-
const fullSnapshotEvents = events.filter(
|
|
25
|
-
(event) => (event as eventWithTime).type === EventType.FullSnapshot,
|
|
26
|
-
) as fullSnapshotEvent[];
|
|
27
|
-
const nodeMap = buildNodeMapFromFullSnapshots(fullSnapshotEvents);
|
|
28
|
-
|
|
29
|
-
code += `${indent(2)}const newTab = await context.newPage();\n`;
|
|
30
|
-
|
|
31
|
-
events.forEach((event: eventWithTime | string) => {
|
|
32
|
-
const e = event as eventWithTime;
|
|
33
|
-
switch (e.type) {
|
|
34
|
-
case EventType.Meta:
|
|
35
|
-
if (e.data.href) {
|
|
36
|
-
code += `${indent(2)}await page.goto('${e.data.href}');\n`;
|
|
37
|
-
code += `${indent(2)}await expect(page).toHaveURL('${e.data.href}');\n`;
|
|
38
|
-
}
|
|
39
|
-
if (e.data.width && e.data.height) {
|
|
40
|
-
code += `${indent(2)}await page.setViewportSize({ width: ${e.data.width}, height: ${e.data.height} });\n`;
|
|
41
|
-
code += `${indent(2)}await expect(page).toHaveViewportSize({ width: ${e.data.width}, height: ${e.data.height} });\n`; // Assert viewport size
|
|
42
|
-
}
|
|
43
|
-
break;
|
|
44
|
-
|
|
45
|
-
case EventType.IncrementalSnapshot:
|
|
46
|
-
const data = e.data;
|
|
47
|
-
const type = data.source;
|
|
48
|
-
let d;
|
|
49
|
-
switch (type) {
|
|
50
|
-
case IncrementalSource.MouseMove:
|
|
51
|
-
case 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 === 'a'
|
|
74
|
-
? `${clickNode?.tagName}:has-text("${clickNode.childNodes
|
|
75
|
-
?.map((node) => (node as any)?.['textContent'])
|
|
76
|
-
.join(' ')}")`
|
|
77
|
-
: clickNode !== undefined
|
|
78
|
-
? cssSelector(clickNode)
|
|
79
|
-
: undefined;
|
|
80
|
-
|
|
81
|
-
if (id && d.x && d.y) {
|
|
82
|
-
code += `${indent(2)}await page.locator(\`${id}\`).click();\n`;
|
|
83
|
-
code += `${indent(2)}await expect(page.locator(\`${id}\`)).toBeVisible();\n`; // Assert visibility after click
|
|
84
|
-
}
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
break;
|
|
88
|
-
|
|
89
|
-
case IncrementalSource.Scroll:
|
|
90
|
-
d = data as scrollData;
|
|
91
|
-
if (d.x && d.y) {
|
|
92
|
-
code += `${indent(2)}await page.mouse.wheel(() => { document.querySelector('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]').scrollTo(${d.x}, ${d.y}); });\n`;
|
|
93
|
-
code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toBeVisible();\n`; // Assert element visibility after scroll
|
|
94
|
-
}
|
|
95
|
-
break;
|
|
96
|
-
|
|
97
|
-
case IncrementalSource.ViewportResize:
|
|
98
|
-
d = data as viewportResizeData;
|
|
99
|
-
if (d.width && d.height) {
|
|
100
|
-
code += `${indent(2)}await page.setViewportSize({ width: ${d.width}, height: ${d.height} });\n`;
|
|
101
|
-
code += `${indent(2)}await expect(page).toHaveViewportSize({ width: ${d.width}, height: ${d.height} });\n`; // Assert viewport size
|
|
102
|
-
}
|
|
103
|
-
break;
|
|
104
|
-
|
|
105
|
-
case IncrementalSource.Input:
|
|
106
|
-
d = data as inputData;
|
|
107
|
-
if (d.text) {
|
|
108
|
-
code += `${indent(2)}await page.fill('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]', '${d.text}');\n`;
|
|
109
|
-
code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toHaveValue('${d.text}');\n`; // Assert input value
|
|
110
|
-
}
|
|
111
|
-
break;
|
|
112
|
-
|
|
113
|
-
case IncrementalSource.MediaInteraction:
|
|
114
|
-
d = data as mediaInteractionData;
|
|
115
|
-
if (d.type) {
|
|
116
|
-
code += `${indent(2)}await page.$eval('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]', el => el.${d.type}());\n`;
|
|
117
|
-
code += `${indent(2)}await expect(page.locator('[data-id="${nodeMap.get(d.id)?.attributes?.id}"]')).toBeVisible();\n`; // Assert visibility of the media element
|
|
118
|
-
}
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
break;
|
|
122
|
-
|
|
123
|
-
default:
|
|
124
|
-
break;
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
code += `${indent(1)}});\n`;
|
|
129
|
-
|
|
130
|
-
return code;
|
|
131
|
-
};
|
package/src/puppeteer/index.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
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 scrollData,
|
|
12
|
-
type viewportResizeData,
|
|
13
|
-
} from '@rrweb/types';
|
|
14
|
-
import { buildNodeMapFromFullSnapshots, cssSelector, indent } from '../utils';
|
|
15
|
-
|
|
16
|
-
export const generatePuppeteerTests = (
|
|
17
|
-
events: Array<eventWithTime | string>,
|
|
18
|
-
): string => {
|
|
19
|
-
let code = '';
|
|
20
|
-
|
|
21
|
-
code += `const puppeteer = require('puppeteer');\n\n`;
|
|
22
|
-
code += `describe('TODO: name the test block', () => {\n`;
|
|
23
|
-
code += `${indent(1)}let browser, page;\n\n`;
|
|
24
|
-
code += `${indent(1)}beforeAll(async () => {\n`;
|
|
25
|
-
code += `${indent(2)}browser = await puppeteer.launch();\n`;
|
|
26
|
-
code += `${indent(2)}page = await browser.newPage();\n`;
|
|
27
|
-
code += `${indent(1)}});\n\n`;
|
|
28
|
-
code += `${indent(1)}afterAll(async () => {\n`;
|
|
29
|
-
code += `${indent(2)}await browser.close();\n`;
|
|
30
|
-
code += `${indent(1)}});\n\n`;
|
|
31
|
-
|
|
32
|
-
code += `${indent(1)}test('TODO: name the test case', async () => {\n`;
|
|
33
|
-
|
|
34
|
-
const fullSnapshotEvents = events.filter(
|
|
35
|
-
(event) => (event as eventWithTime).type === EventType.FullSnapshot,
|
|
36
|
-
) as fullSnapshotEvent[];
|
|
37
|
-
const nodeMap = buildNodeMapFromFullSnapshots(fullSnapshotEvents);
|
|
38
|
-
|
|
39
|
-
events.forEach((event: eventWithTime | string) => {
|
|
40
|
-
const e = event as eventWithTime;
|
|
41
|
-
switch (e.type) {
|
|
42
|
-
case EventType.Meta:
|
|
43
|
-
if (e.data.href) {
|
|
44
|
-
code += `${indent(2)}await page.goto('${e.data.href}');\n`;
|
|
45
|
-
code += `${indent(2)}await expect(page.url()).toBe('${e.data.href}');\n`;
|
|
46
|
-
}
|
|
47
|
-
if (e.data.width && e.data.height) {
|
|
48
|
-
code += `${indent(2)}await page.setViewport({ width: ${e.data.width}, height: ${e.data.height} });\n`;
|
|
49
|
-
}
|
|
50
|
-
break;
|
|
51
|
-
|
|
52
|
-
case EventType.IncrementalSnapshot:
|
|
53
|
-
const data = e.data;
|
|
54
|
-
const type = data.source;
|
|
55
|
-
let d;
|
|
56
|
-
switch (type) {
|
|
57
|
-
case IncrementalSource.MouseMove:
|
|
58
|
-
case IncrementalSource.TouchMove:
|
|
59
|
-
d = data as mousemoveData;
|
|
60
|
-
if (d.positions?.[0]?.x && d.positions?.[0]?.y) {
|
|
61
|
-
code += `${indent(2)}await page.mouse.move(${d.positions[0].x}, ${d.positions[0].y});\n`;
|
|
62
|
-
}
|
|
63
|
-
break;
|
|
64
|
-
|
|
65
|
-
case IncrementalSource.MouseInteraction:
|
|
66
|
-
d = data as mouseInteractionData;
|
|
67
|
-
if (d.type === MouseInteractions.Click) {
|
|
68
|
-
const clickNode = nodeMap.get(d.id);
|
|
69
|
-
const id = clickNode?.attributes?.id
|
|
70
|
-
? `#${clickNode?.attributes?.id}`
|
|
71
|
-
: clickNode?.tagName === 'nav'
|
|
72
|
-
? `${clickNode?.tagName}:text-is("${clickNode.childNodes
|
|
73
|
-
?.filter((f) => f.tagName === 'a')
|
|
74
|
-
?.map((node) =>
|
|
75
|
-
node.childNodes?.map(
|
|
76
|
-
(n1) => (n1 as any)?.['textContent'],
|
|
77
|
-
),
|
|
78
|
-
)
|
|
79
|
-
.join(' ')}")`
|
|
80
|
-
: clickNode?.tagName === 'a'
|
|
81
|
-
? `${clickNode?.tagName}:has-text("${clickNode.childNodes
|
|
82
|
-
?.map((node) => (node as any)?.['textContent'])
|
|
83
|
-
.join(' ')}")`
|
|
84
|
-
: clickNode !== undefined
|
|
85
|
-
? cssSelector(clickNode)
|
|
86
|
-
: undefined;
|
|
87
|
-
|
|
88
|
-
if (id) {
|
|
89
|
-
code += `${indent(2)}await page.click('${id}');\n`;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
break;
|
|
93
|
-
|
|
94
|
-
case IncrementalSource.Scroll:
|
|
95
|
-
d = data as scrollData;
|
|
96
|
-
if (d.x && d.y) {
|
|
97
|
-
const elementId = nodeMap.get(d.id)?.attributes?.id || '';
|
|
98
|
-
code += `${indent(2)}await page.evaluate(() => {\n`;
|
|
99
|
-
code += `${indent(3)}document.querySelector('#${elementId}').scrollTo(${d.x}, ${d.y});\n`;
|
|
100
|
-
code += `${indent(2)}});\n`;
|
|
101
|
-
}
|
|
102
|
-
break;
|
|
103
|
-
|
|
104
|
-
case IncrementalSource.ViewportResize:
|
|
105
|
-
d = data as viewportResizeData;
|
|
106
|
-
if (d.width && d.height) {
|
|
107
|
-
code += `${indent(2)}await page.setViewport({ width: ${d.width}, height: ${d.height} });\n`;
|
|
108
|
-
}
|
|
109
|
-
break;
|
|
110
|
-
|
|
111
|
-
case IncrementalSource.Input:
|
|
112
|
-
d = data as inputData;
|
|
113
|
-
if (d.text) {
|
|
114
|
-
const elementId = nodeMap.get(d.id)?.attributes?.id || '';
|
|
115
|
-
code += `${indent(2)}await page.type('#${elementId}', '${d.text}');\n`;
|
|
116
|
-
}
|
|
117
|
-
break;
|
|
118
|
-
|
|
119
|
-
case IncrementalSource.MediaInteraction:
|
|
120
|
-
d = data as mediaInteractionData;
|
|
121
|
-
if (d.type) {
|
|
122
|
-
const elementId = nodeMap.get(d.id)?.attributes?.id || '';
|
|
123
|
-
code += `${indent(2)}await page.evaluate(() => {\n`;
|
|
124
|
-
code += `${indent(3)}document.querySelector('#${elementId}').${d.type}();\n`;
|
|
125
|
-
code += `${indent(2)}});\n`;
|
|
126
|
-
}
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
break;
|
|
130
|
-
|
|
131
|
-
default:
|
|
132
|
-
break;
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
code += `${indent(1)}});\n`;
|
|
137
|
-
code += `});\n`;
|
|
138
|
-
|
|
139
|
-
return code;
|
|
140
|
-
};
|
package/src/selenium/index.ts
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
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 scrollData,
|
|
12
|
-
type viewportResizeData,
|
|
13
|
-
} from '@rrweb/types';
|
|
14
|
-
import { buildNodeMapFromFullSnapshots, cssSelector, indent } from '../utils';
|
|
15
|
-
|
|
16
|
-
export const generateSeleniumTests = (
|
|
17
|
-
events: Array<eventWithTime | string>,
|
|
18
|
-
): string => {
|
|
19
|
-
let code = '';
|
|
20
|
-
|
|
21
|
-
code += `const { Builder, By, until } = require('selenium-webdriver');\n\n`;
|
|
22
|
-
code += `describe('TODO: name the test block', () => {\n`;
|
|
23
|
-
code += `${indent(1)}let driver;\n\n`;
|
|
24
|
-
code += `${indent(1)}beforeAll(async () => {\n`;
|
|
25
|
-
code += `${indent(2)}driver = await new Builder().forBrowser('chrome').build();\n`;
|
|
26
|
-
code += `${indent(1)}});\n\n`;
|
|
27
|
-
code += `${indent(1)}afterAll(async () => {\n`;
|
|
28
|
-
code += `${indent(2)}await driver.quit();\n`;
|
|
29
|
-
code += `${indent(1)}});\n\n`;
|
|
30
|
-
|
|
31
|
-
code += `${indent(1)}test('TODO: name the test case', async () => {\n`;
|
|
32
|
-
|
|
33
|
-
const fullSnapshotEvents = events.filter(
|
|
34
|
-
(event) => (event as eventWithTime).type === EventType.FullSnapshot,
|
|
35
|
-
) as fullSnapshotEvent[];
|
|
36
|
-
const nodeMap = buildNodeMapFromFullSnapshots(fullSnapshotEvents);
|
|
37
|
-
|
|
38
|
-
events.forEach((event: eventWithTime | string) => {
|
|
39
|
-
const e = event as eventWithTime;
|
|
40
|
-
switch (e.type) {
|
|
41
|
-
case EventType.Meta:
|
|
42
|
-
if (e.data.href) {
|
|
43
|
-
code += `${indent(2)}await driver.get('${e.data.href}');\n`;
|
|
44
|
-
code += `${indent(2)}await driver.wait(until.urlIs('${e.data.href}'), 5000);\n`; // Assert URL
|
|
45
|
-
}
|
|
46
|
-
if (e.data.width && e.data.height) {
|
|
47
|
-
code += `${indent(2)}await driver.manage().window().setRect({ width: ${e.data.width}, height: ${e.data.height} });\n`;
|
|
48
|
-
}
|
|
49
|
-
break;
|
|
50
|
-
|
|
51
|
-
case EventType.IncrementalSnapshot:
|
|
52
|
-
const data = e.data;
|
|
53
|
-
const type = data.source;
|
|
54
|
-
let d;
|
|
55
|
-
switch (type) {
|
|
56
|
-
case IncrementalSource.MouseMove:
|
|
57
|
-
case IncrementalSource.TouchMove:
|
|
58
|
-
d = data as mousemoveData;
|
|
59
|
-
if (d.positions?.[0]?.x && d.positions?.[0]?.y) {
|
|
60
|
-
code += `${indent(2)}await driver.actions().move({ x: ${d.positions[0].x}, y: ${d.positions[0].y} }).perform();\n`;
|
|
61
|
-
}
|
|
62
|
-
break;
|
|
63
|
-
|
|
64
|
-
case IncrementalSource.MouseInteraction:
|
|
65
|
-
d = data as mouseInteractionData;
|
|
66
|
-
if (d.type === MouseInteractions.Click) {
|
|
67
|
-
const clickNode = nodeMap.get(d.id);
|
|
68
|
-
const id = clickNode?.attributes?.id
|
|
69
|
-
? `#${clickNode?.attributes?.id}`
|
|
70
|
-
: clickNode?.tagName === 'nav'
|
|
71
|
-
? `${clickNode?.tagName}:text-is("${clickNode.childNodes
|
|
72
|
-
?.filter((f) => f.tagName === 'a')
|
|
73
|
-
?.map((node) =>
|
|
74
|
-
node.childNodes?.map(
|
|
75
|
-
(n1) => (n1 as any)?.['textContent'],
|
|
76
|
-
),
|
|
77
|
-
)
|
|
78
|
-
.join(' ')}")`
|
|
79
|
-
: clickNode?.tagName === 'a'
|
|
80
|
-
? `${clickNode?.tagName}:has-text("${clickNode.childNodes
|
|
81
|
-
?.map((node) => (node as any)?.['textContent'])
|
|
82
|
-
.join(' ')}")`
|
|
83
|
-
: clickNode !== undefined
|
|
84
|
-
? cssSelector(clickNode)
|
|
85
|
-
: undefined;
|
|
86
|
-
if (id) {
|
|
87
|
-
code += `${indent(2)}await driver.findElement(By.css('${id}')).click();\n`;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
break;
|
|
91
|
-
|
|
92
|
-
case IncrementalSource.Scroll:
|
|
93
|
-
d = data as scrollData;
|
|
94
|
-
if (d.x && d.y) {
|
|
95
|
-
const elementId = nodeMap.get(d.id)?.attributes?.id || '';
|
|
96
|
-
code += `${indent(2)}await driver.executeScript('document.querySelector("[id=${elementId}]").scrollTo(${d.x}, ${d.y});');\n`;
|
|
97
|
-
}
|
|
98
|
-
break;
|
|
99
|
-
|
|
100
|
-
case IncrementalSource.ViewportResize:
|
|
101
|
-
d = data as viewportResizeData;
|
|
102
|
-
if (d.width && d.height) {
|
|
103
|
-
code += `${indent(2)}await driver.manage().window().setRect({ width: ${d.width}, height: ${d.height} });\n`;
|
|
104
|
-
}
|
|
105
|
-
break;
|
|
106
|
-
|
|
107
|
-
case IncrementalSource.Input:
|
|
108
|
-
d = data as inputData;
|
|
109
|
-
if (d.text) {
|
|
110
|
-
const elementId = nodeMap.get(d.id)?.attributes?.id || '';
|
|
111
|
-
code += `${indent(2)}const input = await driver.findElement(By.css('#${elementId}'));\n`;
|
|
112
|
-
code += `${indent(2)}await input.clear();\n`;
|
|
113
|
-
code += `${indent(2)}await input.sendKeys('${d.text}');\n`;
|
|
114
|
-
}
|
|
115
|
-
break;
|
|
116
|
-
|
|
117
|
-
case IncrementalSource.MediaInteraction:
|
|
118
|
-
d = data as mediaInteractionData;
|
|
119
|
-
if (d.type) {
|
|
120
|
-
const elementId = nodeMap.get(d.id)?.attributes?.id || '';
|
|
121
|
-
code += `${indent(2)}await driver.executeScript('document.querySelector("[id=${elementId}]").${d.type}();');\n`;
|
|
122
|
-
}
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
break;
|
|
126
|
-
|
|
127
|
-
default:
|
|
128
|
-
break;
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
code += `${indent(1)}});\n`;
|
|
133
|
-
code += `});\n`;
|
|
134
|
-
|
|
135
|
-
return code;
|
|
136
|
-
};
|
package/src/types.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
export type TestOptions = {
|
|
2
|
-
framework:
|
|
3
|
-
| 'playwright'
|
|
4
|
-
| 'puppeteer'
|
|
5
|
-
| 'cypress'
|
|
6
|
-
| 'selenium'
|
|
7
|
-
| 'appium'
|
|
8
|
-
| 'espresso'
|
|
9
|
-
| 'xcuitest'
|
|
10
|
-
| 'detox';
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type SerializedNodeWithId = {
|
|
14
|
-
id: number;
|
|
15
|
-
tagName?: string;
|
|
16
|
-
attributes?: Record<string, any>;
|
|
17
|
-
childNodes?: SerializedNodeWithId[];
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const TestFramework = {
|
|
21
|
-
PLAYWRIGHT: 'playwright',
|
|
22
|
-
PUPPETEER: 'puppeteer',
|
|
23
|
-
CYPRESS: 'cypress',
|
|
24
|
-
SELENIUM: 'selenium',
|
|
25
|
-
APPIUM: 'appium',
|
|
26
|
-
ESPRESSO: 'espresso',
|
|
27
|
-
XCUITEST: 'xcuitest',
|
|
28
|
-
DETOX: 'detox',
|
|
29
|
-
} as const;
|
|
30
|
-
|
|
31
|
-
/** Frameworks that target native mobile apps (vs. web browsers). */
|
|
32
|
-
export const MOBILE_FRAMEWORKS = [
|
|
33
|
-
'appium',
|
|
34
|
-
'espresso',
|
|
35
|
-
'xcuitest',
|
|
36
|
-
'detox',
|
|
37
|
-
] as const;
|
package/src/utils.test.ts
DELETED
package/src/utils.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import type { fullSnapshotEvent } from '@rrweb/types';
|
|
2
|
-
import type { SerializedNodeWithId } from './types';
|
|
3
|
-
|
|
4
|
-
export const indent = (level: number) => ' '.repeat(level);
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Builds a safe CSS selector for a node. Handles the common cases:
|
|
8
|
-
* an `id`, multiple space-separated classes (joined with `.`), links/nav
|
|
9
|
-
* with their text, or a bare tag name — never emits `tag.undefined`.
|
|
10
|
-
*/
|
|
11
|
-
export const cssSelector = (
|
|
12
|
-
node?: SerializedNodeWithId,
|
|
13
|
-
): string | undefined => {
|
|
14
|
-
if (!node?.tagName) return undefined;
|
|
15
|
-
const attrs = node.attributes ?? {};
|
|
16
|
-
if (attrs.id) return `#${attrs.id}`;
|
|
17
|
-
|
|
18
|
-
const text = (node.childNodes ?? [])
|
|
19
|
-
.map((n) => (n as any)?.textContent as string | undefined)
|
|
20
|
-
.filter(Boolean)
|
|
21
|
-
.join(' ')
|
|
22
|
-
.trim();
|
|
23
|
-
|
|
24
|
-
if (node.tagName === 'a' && text) return `a:has-text("${text}")`;
|
|
25
|
-
|
|
26
|
-
const cls = (attrs.class as string | undefined)?.trim();
|
|
27
|
-
if (cls) {
|
|
28
|
-
const chain = cls
|
|
29
|
-
.split(/\s+/)
|
|
30
|
-
.filter(Boolean)
|
|
31
|
-
// CSS.escape-lite: guard classes that start with a digit or contain
|
|
32
|
-
// characters that aren't valid in a bare class selector.
|
|
33
|
-
.map((c) => (/^[A-Za-z_-][\w-]*$/.test(c) ? c : `[class~="${c}"]`))
|
|
34
|
-
.map((c) => (c.startsWith('[') ? c : `.${c}`))
|
|
35
|
-
.join('');
|
|
36
|
-
return `${node.tagName}${chain}`;
|
|
37
|
-
}
|
|
38
|
-
return node.tagName;
|
|
39
|
-
};
|
|
40
|
-
/**
|
|
41
|
-
* Builds a map of all nodes by their IDs from a FullSnapshotEvent.
|
|
42
|
-
* @param rootNode - The root node of the snapshot tree.
|
|
43
|
-
* @returns A map where keys are node IDs and values are the corresponding nodes.
|
|
44
|
-
*/
|
|
45
|
-
export const buildNodeMapFromFullSnapshots = (
|
|
46
|
-
fullSnapshots: fullSnapshotEvent[],
|
|
47
|
-
): Map<number, SerializedNodeWithId> => {
|
|
48
|
-
const idMap = new Map<number, SerializedNodeWithId>();
|
|
49
|
-
|
|
50
|
-
// Iterate over each FullSnapshotEvent
|
|
51
|
-
fullSnapshots.forEach((snapshot: fullSnapshotEvent) => {
|
|
52
|
-
const rootNode = snapshot.data.node; // Get the root node of the full snapshot
|
|
53
|
-
|
|
54
|
-
// Recursive function to process each node and add it to the map
|
|
55
|
-
function processNode(node: SerializedNodeWithId) {
|
|
56
|
-
idMap.set(node.id, node); // Add node to map using its ID
|
|
57
|
-
|
|
58
|
-
// Recursively process child nodes, if any
|
|
59
|
-
if (node.childNodes) {
|
|
60
|
-
node.childNodes.forEach((childNode) => {
|
|
61
|
-
processNode(childNode);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Start the recursive node processing from the root node
|
|
67
|
-
processNode(rootNode);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
return idMap;
|
|
71
|
-
};
|