forked from checkly/puppeteer-to-playwright
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.js
284 lines (250 loc) · 8.56 KB
/
transform.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
export default function (fileInfo, api) {
const j = api.jscodeshift;
const root = j(fileInfo.source);
// If strict mode is set the codemod will act more conservatively
// and assume all explicit waits are actually necessary
const MODE_STRICT = process.env.STRICT;
// If possible, reuse existing variable names for context and page
// otherwise, use defaults
let varBrowser = 'browser';
let varContext = 'context';
let varPage = 'page';
// Check if target file contains Puppeteer requires/imports, skip if it doesn't
const puppeteerRequire = root
.find(j.VariableDeclaration)
.filter((path) => {
return (
path.value?.declarations?.[0]?.init?.arguments?.[0]?.value === 'puppeteer' &&
path.value?.declarations?.[0]?.init?.callee?.name === 'require'
);
})
const puppeteerImport = root
.find(j.ImportDeclaration)
.filter((path) => {
return (
path.value?.source?.value === 'puppeteer' &&
path.value?.source?.type === 'Literal'
);
})
if (puppeteerImport.paths().length || puppeteerRequire.paths().length) {
let initDeclaration = root
.find(j.VariableDeclaration)
.filter((path) => {
return (
path.value?.declarations?.[0]?.init?.argument?.callee?.object?.name === 'puppeteer'
);
})
if (initDeclaration.length) {
varBrowser = initDeclaration.get().value.declarations[0].id.name
}
let initAssignment = root
.find(j.AssignmentExpression)
.filter((path) => {
return (
path.value?.right?.argument?.callee?.object?.name === 'puppeteer'
);
})
if (initAssignment.length) {
varBrowser = initAssignment.get().value.left.name
}
// Handle puppeteer require
root
.find(j.Identifier, { name: 'puppeteer' })
.filter((path) => {
return (
path.parent?.value?.type === 'VariableDeclarator' &&
path.parent?.value?.init?.type === 'CallExpression' &&
path.parent?.value?.init?.callee?.type === 'Identifier' &&
path.parent?.value?.init?.callee?.name === 'require' &&
path.parent?.value?.init?.arguments[0]?.type === 'Literal' &&
path.parent?.value?.init?.arguments[0]?.value === 'puppeteer'
);
})
.replaceWith((path) => {
return j.identifier('{ chromium }');
});
// Handle puppeteer import
root
.find(j.Identifier, { name: 'puppeteer' })
.filter((path) => {
return (
path.parent?.value?.type === 'ImportDefaultSpecifier'
);
})
.replaceWith((path) => {
return j.identifier('{ chromium }');
});
root.find(j.Identifier, { name: 'puppeteer' }).replaceWith(j.identifier('chromium'));
root.find(j.Literal).replaceWith((path) => {
if (path.value?.value === 'puppeteer') {
return j.identifier("'playwright'");
}
return path.value;
});
// Remove existing context creation, save context variable name for reuse
root
.find(j.VariableDeclaration)
.filter((path) => {
if (path.value?.declarations[0]?.init?.argument?.callee?.property?.name === 'createIncognitoBrowserContext') {
varContext = path.value.declarations[0].id.name;
}
return (
path.value?.declarations[0]?.init?.argument?.callee?.property?.name === 'createIncognitoBrowserContext' &&
path.value?.declarations[0]?.init?.argument?.callee?.object?.name === varBrowser
);
})
.remove();
// Force context creation, save page variable name for reuse
root
.find(j.VariableDeclaration)
.filter((path) => {
if (path.value?.declarations[0]?.init?.argument?.callee?.property?.name === 'newPage') {
varPage = path.value.declarations[0].id.name;
}
return (
path.value?.declarations[0]?.init?.argument?.callee?.property?.name === 'newPage' &&
path.value?.declarations[0]?.init?.argument?.callee?.object?.name === varBrowser
);
})
.insertBefore(`const ${varContext} = await ${varBrowser}.newContext();`);
// Handle page creation from context
root
.find(j.VariableDeclaration)
.filter((path) => {
return (
path.value?.declarations[0]?.init?.argument?.callee?.property?.name === 'newPage' &&
path.value?.declarations[0]?.init?.argument?.callee?.object?.name === varBrowser
);
})
.replaceWith(`const ${varPage} = await ${varContext}.newPage();`);
// Remove existing context creation, save context variable name for reuse (assignment)
root
.find(j.AssignmentExpression)
.filter((path) => {
if (path.value?.right?.argument?.callee?.property?.name === 'createIncognitoBrowserContext') {
varContext = path.value.left.name;
}
return (
path.value?.right?.argument?.callee?.property?.name === 'createIncognitoBrowserContext' &&
path.value?.right?.argument?.callee?.object?.name === varBrowser
);
})
.remove();
// Force context creation, save page variable name for reuse (assignment)
root
.find(j.ExpressionStatement)
.filter((path) => {
if (path.value?.expression?.right?.argument?.callee?.property?.name === 'newPage') {
varPage = path.value?.expression?.left?.name;
}
return (
path.value?.expression?.right?.argument?.callee?.property?.name === 'newPage' &&
path.value?.expression?.right?.argument?.callee?.object?.name === varBrowser
);
})
.insertBefore(`const ${varContext} = await ${varBrowser}.newContext();`);
// Handle page creation from context (assignment)
root
.find(j.AssignmentExpression)
.filter((path) => {
return (
path.value?.right?.argument?.callee?.property?.name === 'newPage' &&
path.value?.right?.argument?.callee?.object?.name === varBrowser
);
})
.replaceWith(`${varPage} = await ${varContext}.newPage()`);
// Remove sleeps
root
.find(j.AwaitExpression)
.filter((path) => {
return !MODE_STRICT && path.value?.argument?.callee?.name === 'sleep';
})
.remove();
// Remove waitForTimeout and waitFor
root
.find(j.AwaitExpression)
.filter((path) => {
return (
!MODE_STRICT &&
(path.value?.argument?.callee?.property?.name === 'waitForTimeout' ||
path?.value?.argument?.callee?.property?.name === 'waitFor')
);
})
.remove();
// Remove waitForNavigation
root
.find(j.VariableDeclaration)
.filter((path) => {
return !MODE_STRICT && path.value?.declarations[0]?.init?.callee?.property?.name === 'waitForNavigation';
})
.remove();
// Remove navigationPromise
root
.find(j.AwaitExpression)
.filter((path) => {
return !MODE_STRICT && path.value?.argument?.name === 'navigationPromise';
})
.remove();
// Remove waitForNetworkIdle
root
.find(j.AwaitExpression)
.filter((path) => {
return !MODE_STRICT && path.value?.argument?.callee?.property?.name === 'waitForNetworkIdle';
})
.remove();
// Remove waitForSelector only when returned element is not used
root
.find(j.AwaitExpression)
.filter((path) => {
return (
!MODE_STRICT &&
path.value?.argument?.callee?.property?.name === 'waitForSelector' &&
path.parent?.value?.type !== 'VariableDeclarator'
);
})
.remove();
// Update method names
root.find(j.Identifier, { name: 'setViewport' }).replaceWith(j.identifier('setViewportSize'));
root.find(j.Identifier, { name: 'waitForXPath' }).replaceWith(j.identifier('waitForSelector'));
root.find(j.Identifier, { name: '$x' }).replaceWith(j.identifier('$'));
root.find(j.Identifier, { name: 'type' }).replaceWith(j.identifier('fill'));
// Handle reading cookies
root
.find(j.CallExpression)
.filter((path) => {
return path.value?.callee?.property?.name === 'cookies';
})
.replaceWith(`await ${varContext}.cookies()`);
// Handle setting cookies
const varCookies = root.find(j.AwaitExpression).filter((path) => {
return path.value?.argument?.callee?.property?.name == 'setCookie';
});
if (varCookies.length > 0) {
const elName = varCookies.get().value.argument.arguments[0].argument.name;
root
.find(j.CallExpression)
.filter((path) => {
return path.value?.callee?.property?.name == 'setCookie';
})
.replaceWith(
j.callExpression(j.memberExpression(j.identifier(varContext), j.identifier('addCookies'), false), [
j.identifier(elName),
])
);
root
.find(j.ExpressionStatement)
.filter((path) => {
return path.value?.expression?.argument?.callee?.property?.name === 'addCookies';
})
.insertBefore('// TODO: ensure the following line references the right context');
}
// Handle clearing cookies
root
.find(j.AwaitExpression)
.filter((path) => {
return path.value?.argument?.callee?.property?.name == 'deleteCookie';
})
.replaceWith(`// TODO: this deletes all cookies - ensure this is fine\nawait ${varContext}.clearCookies()`);
return root.toSource();
}
}