-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
156 lines (146 loc) · 4.44 KB
/
parser.js
File metadata and controls
156 lines (146 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import YAML from "yaml"
import z, { refine } from "zod"
import { KnownDevices } from "puppeteer"
const stepsSchema = z.object({
newPage: z.boolean().optional(),
emulate: z.object({
device: z.enum(Object.keys(KnownDevices))
}).optional(),
goto: z.object({
url: z.string().url(),
waitUntil: z.enum(["domcontentloaded", "networkidle0", "networkidle2", "load"]).default("load").optional()
}).optional(),
wait: z.object({
timeout: z.string().regex(/(\d+)(ms|m|s)*/)
}).optional(),
waitForSelector: z.object({
selector: z.string()
}).optional(),
screenshot: z.object({
path: z.string(),
fullPage: z.boolean().optional()
}).optional(),
type: z.object({
selector: z.string(),
text: z.string(),
delay: z.string().default("0ms")
}).optional(),
click: z.object({
selector: z.string().optional(),
coords: z.object({
x: z.number(),
y: z.number()
}).optional()
}).refine(data => data.selector || data.coords, {
message: "Either selector or coords must be provided"
}).optional(),
scroll: z.object({
to: z.object({
selector: z.string().optional(),
coords: z.object({
x: z.number(),
y: z.number()
}).optional()
}).refine(data => data.selector || data.coords, {
message: "Either selector or coords must be provided"
}).optional(),
by: z.object({
dx: z.number(),
dy: z.number()
}).optional()
}).refine(data => data.to || data.by, {
message: "Either to or by must be provided"
}).optional(),
press: z.object({
key: z.string()
}).optional(),
focus: z.object({
selector: z.string()
}).optional(),
hover: z.object({
selector: z.string()
}).optional(),
baseline_scan: z.object({
availability: z.array(z.enum(["high", "low", "false"])),
year: z.number(),
delay: z.string().regex(/(\d+)(ms|m|s)*/).optional()
}).optional(),
assert: z.object({
selector: z.string(),
exists: z.boolean().optional(),
contains: z.string().optional(),
equals: z.string().optional(),
matches: z.string().optional(),
visible: z.boolean().optional(),
timeout: z.string().regex(/(\d+)(ms|m|s)*/).optional(),
throwOnFail: z.boolean().default(false).optional()
}).refine(data => {
const assertionTypes = [data.exists, data.contains, data.equals, data.matches, data.visible]
const definedAssertions = assertionTypes.filter(type => type != undefined).length
return definedAssertions >= 1
}, {
message: "At least one assertion type must be provided (exists, contains, equals, matches, or visible)"
}).optional(),
close: z.boolean().optional()
})
const browserConfigSchema = z.discriminatedUnion("mode", [
z.object({
mode: z.literal("launch"),
launch: z.object({
executablePath: z.string().optional(),
headless: z.boolean().default(false),
viewport: z.object({
width: z.number(),
height: z.number()
}).optional()
})
}),
z.object({
mode: z.literal("connect"),
connect: z.object({
wsUrl: z.string()
})
})
])
const schema = z.object({
framework: z.enum(["puppeteer", "playwright", "selenium"]),
browser: browserConfigSchema,
steps: z.array(stepsSchema).optional()
})
function parse(code) {
try {
const doc = YAML.parse(code)
return schema.parse(doc)
} catch (err) {
console.error(`Error parsing YAML document: ${err.message}`)
}
}
function generateIR(doc) {
const ast = []
for (let key of Object.keys(doc)) {
if (key === "steps") {
for (let obj of doc["steps"]) {
let k = Object.keys(obj)[0]
ast.push({
cmd: k,
value: obj[k]
})
}
} else {
ast.push({
cmd: key,
value: doc[key]
})
}
}
const mode = ast.find(e => e.cmd === "browser")?.value.mode || "launch"
ast.push({
cmd: "EOF",
value: { operation: (mode === "launch") ? "close" : "disconnect" }
})
return ast
}
export {
parse,
generateIR
}