Skip to content

Commit 1dca221

Browse files
Add within-iframe command
1 parent 5eb2d6f commit 1dca221

File tree

280 files changed

+479
-330
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

280 files changed

+479
-330
lines changed

src/commands.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class ParserWithContext {
273273
return context.ast.absolutePath;
274274
}
275275

276-
run_order(order, ast) {
276+
run_order(pages, order, ast) {
277277
// This is needed because for now, all commands get access to the ast
278278
// through `ParserWithContext`.
279279
this.elems = ast;
@@ -309,7 +309,7 @@ class ParserWithContext {
309309
}
310310
if (res.skipInstructions) {
311311
// We disable the `increasePos` in the context to prevent it to be done twice.
312-
return this.get_next_command(false);
312+
return this.get_next_command(pages, false);
313313
}
314314

315315
return {
@@ -321,13 +321,18 @@ class ParserWithContext {
321321
'instructions': res['instructions'],
322322
'infos': res['infos'],
323323
'warnings': res['warnings'],
324+
'callback': res['callback'],
325+
'noPosIncrease': res['noPosIncrease'],
324326
};
325327
}
326328

327-
get_next_command(increasePos = true) {
329+
get_next_command(pages, increasePos = true) {
328330
let context = this.get_current_context();
329331
while (context !== null && context.currentCommand >= context.commands.length) {
330-
this.contexts.pop();
332+
const prevContext = this.contexts.pop();
333+
if (prevContext.dropCallback !== undefined) {
334+
prevContext.dropCallback(pages);
335+
}
331336
context = this.get_current_context();
332337
if (context !== null) {
333338
context.currentCommand += 1;
@@ -345,8 +350,8 @@ class ParserWithContext {
345350
'errors': inferred.errors,
346351
};
347352
}
348-
const ret = this.run_order(command.commandName, inferred.ast);
349-
if (increasePos) {
353+
const ret = this.run_order(pages, command.commandName, inferred.ast);
354+
if (increasePos && !ret['noPosIncrease']) {
350355
this.increase_context_pos();
351356
}
352357
return ret;

src/commands/context_setters.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const consts = require('../consts.js');
44
const { validator } = require('../validator.js');
55
const { hasError } = require('../utils.js');
6+
const { getAndSetElements } = require('./utils.js');
67

78
// Possible inputs:
89
//
@@ -162,7 +163,7 @@ function parseSetTimeout(parser) {
162163
}
163164
return {
164165
'instructions': [
165-
`page.setDefaultTimeout(${ret.value.value})`,
166+
`pages[0].setDefaultTimeout(${ret.value.value})`,
166167
],
167168
'wait': false,
168169
'warnings': warnings.length > 0 ? warnings : undefined,
@@ -233,8 +234,38 @@ function parseWithinIFrame(parser) {
233234
if (hasError(ret)) {
234235
return ret;
235236
}
237+
const tuple = ret.value.entries;
238+
const selector = tuple[0].value;
239+
const isPseudo = !selector.isXPath && selector.pseudo !== null;
240+
241+
if (isPseudo) {
242+
return {
243+
'error': 'Pseudo elements cannot be <iframe>',
244+
};
245+
}
246+
247+
const code = `\
248+
${getAndSetElements(selector, 'iframe', false)}
249+
pages.push(iframe);`;
250+
236251
return {
237-
'instructions': [],
252+
'instructions': [code],
253+
'callback': () => {
254+
const context = parser.get_current_context();
255+
parser.pushNewContext({
256+
'ast': context.ast,
257+
'commands': tuple[1].value.value,
258+
'currentCommand': 0,
259+
'functionArgs': Object.create(null),
260+
'dropCallback': pages => {
261+
if (pages.length < 2) {
262+
throw new Error('`pages` is empty whereas it should never happen!');
263+
}
264+
pages.pop();
265+
},
266+
});
267+
},
268+
'noPosIncrease': true,
238269
};
239270
}
240271

src/commands/emulation.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ function parseSetWindowSize(parser) {
3434
return {
3535
'instructions': [
3636
`\
37-
const viewport = page.viewport();
37+
const viewport = pages[0].viewport();
3838
const newViewport = {
3939
...viewport,
4040
width: ${width},
4141
height: ${height},
4242
};
43-
await page.setViewport(newViewport);`,
43+
await pages[0].setViewport(newViewport);`,
4444
],
4545
};
4646
}
@@ -64,12 +64,12 @@ function parseSetDevicePixelRatio(parser) {
6464
return {
6565
'instructions': [
6666
`\
67-
const viewport = page.viewport();
67+
const viewport = pages[0].viewport();
6868
const newViewport = {
6969
...viewport,
7070
deviceScaleFactor: ${ret.value.getRaw()},
7171
};
72-
await page.setViewport(newViewport);`,
72+
await pages[0].setViewport(newViewport);`,
7373
],
7474
};
7575
}
@@ -97,7 +97,7 @@ if (arg.puppeteer.KnownDevices["${device}"] === undefined) {
9797
https://github.com/GoogleChrome/puppeteer/blob/master/lib/DeviceDescriptors.js or \
9898
you can use \`--show-devices\` option';
9999
} else {
100-
await page.emulate(arg.puppeteer.KnownDevices["${device}"]);
100+
await pages[0].emulate(arg.puppeteer.KnownDevices["${device}"]);
101101
}`,
102102
],
103103
};
@@ -128,7 +128,7 @@ function parseGeolocation(parser) {
128128
const tuple = ret.value.entries;
129129
return {
130130
'instructions': [
131-
`await page.setGeolocation(${tuple[0].value.getRaw()}, ${tuple[1].value.getRaw()});`,
131+
`await pages[0].setGeolocation(${tuple[0].value.getRaw()},${tuple[1].value.getRaw()});`,
132132
],
133133
};
134134
}
@@ -178,7 +178,7 @@ function parseJavascript(parser) {
178178

179179
return {
180180
'instructions': [
181-
`await page.setJavaScriptEnabled(${ret.value.getRaw()});`,
181+
`await pages[0].setJavaScriptEnabled(${ret.value.getRaw()});`,
182182
],
183183
};
184184
}
@@ -200,7 +200,7 @@ function parseSetFontSize(parser) {
200200

201201
return {
202202
'instructions': [`\
203-
const client = await page.target().createCDPSession();
203+
const client = await pages[0].target().createCDPSession();
204204
await client.send("Page.enable");
205205
await client.send("Page.setFontSizes", {
206206
fontSizes: {

src/commands/general.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function innerParseScreenshot(options, filename, selector) {
6969
}
7070
} else {
7171
// In case no selector was specified, we take a screenshot of the whole page.
72-
instructions += `const ${varName} = page;\n`;
72+
instructions += `const ${varName} = pages[0];\n`;
7373
}
7474

7575
const p = path.join(options.getImageFolder(), filename) + '.png';

src/commands/input.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function parseClick(parser) {
3535
const tuple = p.entries;
3636
return {
3737
'instructions': [
38-
`await page.mouse.click(${tuple[0].value.value}, ${tuple[1].value.value});`,
38+
`await page.click(${tuple[0].value.value}, ${tuple[1].value.value});`,
3939
],
4040
};
4141
}
@@ -158,13 +158,13 @@ function parseWrite(parser) {
158158
if (value.kind === 'number') {
159159
return {
160160
'instructions': [
161-
`await page.keyboard.press(String.fromCharCode(${value.value}));`,
161+
`await pages[0].keyboard.press(String.fromCharCode(${value.value}));`,
162162
],
163163
};
164164
}
165165
return {
166166
'instructions': [
167-
`await page.keyboard.type("${value.getStringValue()}");`,
167+
`await pages[0].keyboard.type("${value.getStringValue()}");`,
168168
],
169169
};
170170
}
@@ -213,7 +213,7 @@ await ${varName}.type("${value.getStringValue()}");`,
213213
${getAndSetElements(selector, varName, false)}
214214
${varName}.focus();
215215
await ${varName};
216-
page.keyboard.press(String.fromCharCode(${value.value}));`],
216+
pages[0].keyboard.press(String.fromCharCode(${value.value}));`],
217217
};
218218
}
219219

@@ -259,13 +259,13 @@ function parsePressKey(parser) {
259259
if (value.kind === 'number') {
260260
return {
261261
'instructions': [
262-
`await page.keyboard.press(String.fromCharCode(${value.value}))`,
262+
`await pages[0].keyboard.press(String.fromCharCode(${value.value}))`,
263263
],
264264
};
265265
} else if (value.kind === 'string') {
266266
return {
267267
'instructions': [
268-
`await page.keyboard.press("${value.getStringValue()}")`,
268+
`await pages[0].keyboard.press("${value.getStringValue()}")`,
269269
],
270270
};
271271
}
@@ -278,13 +278,13 @@ function parsePressKey(parser) {
278278
if (tuple[0].value.kind === 'string') {
279279
return {
280280
'instructions': [
281-
`await page.keyboard.press("${tuple[0].value.getStringValue()}", ${delay})`,
281+
`await pages[0].keyboard.press("${tuple[0].value.getStringValue()}", ${delay})`,
282282
],
283283
};
284284
}
285285
return {
286286
'instructions': [
287-
`await page.keyboard.press(String.fromCharCode(${tuple[0].value.value}), ${delay})`,
287+
`await pages[0].keyboard.press(String.fromCharCode(${tuple[0].value.value}), ${delay})`,
288288
],
289289
};
290290
}
@@ -316,7 +316,7 @@ function parseMoveCursorTo(parser) {
316316
const [x, y] = value.entries;
317317
return {
318318
'instructions': [
319-
`await page.mouse.move(${x.value.value}, ${y.value.value});`,
319+
`await pages[0].mouse.move(${x.value.value}, ${y.value.value});`,
320320
],
321321
};
322322
}
@@ -387,15 +387,15 @@ function parseDragAndDrop(parser) {
387387
const args = arg.value.entries;
388388
code += `const ${posName} = [${args[0].value.getRaw()}, ${args[1].value.getRaw()}];\n`;
389389
}
390-
return `${code}await page.mouse.move(${posName}[0], ${posName}[1]);`;
390+
return `${code}await pages[0].mouse.move(${posName}[0], ${posName}[1]);`;
391391
}
392392

393393
return {
394394
'instructions': [`\
395395
${setupThings(tuple[0], 'parseDragAndDropElem', 'start')}
396-
await page.mouse.down();
396+
await pages[0].mouse.down();
397397
${setupThings(tuple[1], 'parseDragAndDropElem2', 'end')}
398-
await page.mouse.up();`,
398+
await pages[0].mouse.up();`,
399399
],
400400
};
401401
}

src/commands/navigation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ if (ret === null) {
9090
}
9191
return {
9292
'instructions': [`\
93-
const ret = page.${jsFunc}({'waitUntil': 'domcontentloaded', 'timeout': ${timeout}});
93+
const ret = pages[0].${jsFunc}({'waitUntil': 'domcontentloaded', 'timeout': ${timeout}});
9494
${insertAfter}await ret;`,
9595
],
9696
'warnings': warnings,

src/commands/network.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function parseBlockNetworkRequest(parser) {
1717

1818
return {
1919
'instructions': [
20-
`await page.setRequestInterception(true);
21-
page.on('request', interceptedRequest => {
20+
`await pages[0].setRequestInterception(true);
21+
pages[0].on('request', interceptedRequest => {
2222
if (interceptedRequest.isInterceptResolutionHandled()) return;
2323
function matchesGlob(glob, text) {
2424
const wildcard = glob.indexOf("*");

src/commands/wait.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ${indentString(error, 1)}
2929

3030
function getWaitForElems(varName, code, error) {
3131
return [`\
32-
const timeLimit = page.getDefaultTimeout();
32+
const timeLimit = pages[0].getDefaultTimeout();
3333
const timeAdd = 50;
3434
let allTime = 0;
3535
let ${varName} = null;`,

0 commit comments

Comments
 (0)