Skip to content

Commit 5eb2d6f

Browse files
Simplify handling of "special" commands not having instructions and updating context and adding beginning of within-iframe command
1 parent f014d02 commit 5eb2d6f

File tree

13 files changed

+97
-128
lines changed

13 files changed

+97
-128
lines changed

src/commands.js

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const { AstLoader } = require('./ast.js');
22
const process = require('process');
33
const commands = require('./commands/all.js');
44
const consts = require('./consts.js');
5-
const path = require('path');
65
const { stripCommonPathsPrefix } = require('./utils.js');
76

87
const ORDERS = {
@@ -117,6 +116,7 @@ const ORDERS = {
117116
'wait-for-text-false': commands.parseWaitForTextFalse,
118117
'wait-for-window-property': commands.parseWaitForWindowProperty,
119118
'wait-for-window-property-false': commands.parseWaitForWindowPropertyFalse,
119+
'within-iframe': commands.parseWithinIFrame,
120120
'write': commands.parseWrite,
121121
'write-into': commands.parseWriteInto,
122122
};
@@ -164,6 +164,7 @@ const FATAL_ERROR_COMMANDS = [
164164
'wait-for-property-false',
165165
'wait-for-text',
166166
'wait-for-text-false',
167+
'within-iframe',
167168
'write',
168169
'write-into',
169170
];
@@ -262,64 +263,6 @@ class ParserWithContext {
262263
}
263264
}
264265

265-
setup_user_function_call(ast) {
266-
const ret = commands.parseCallFunction(this);
267-
if (ret.error !== undefined) {
268-
ret.line = this.get_current_command_line();
269-
if (ast.length !== 0) {
270-
ret.error += ` (from command \`${ast[0].getErrorText()}\`)`;
271-
}
272-
return ret;
273-
}
274-
const args = Object.create(null);
275-
const func = this.definedFunctions[ret['function']];
276-
for (const arg_name of func['arguments']) {
277-
const index = ret['args'].findIndex(arg => arg.key.value === arg_name);
278-
if (index === -1) {
279-
return {
280-
'error': `Missing argument "${arg_name}"`,
281-
'line': this.get_current_command_line(),
282-
'fatal_error': true,
283-
};
284-
}
285-
args[arg_name] = ret['args'][index].value;
286-
}
287-
const context = this.get_current_context();
288-
this.pushNewContext({
289-
'ast': context.ast,
290-
'commands': func.commands,
291-
'currentCommand': 0,
292-
'functionArgs': Object.assign({}, context.functionArgs, args),
293-
'filePath': func.filePath,
294-
});
295-
// We disable the `increasePos` in the context to prevent it to be done twice.
296-
return this.get_next_command(false);
297-
}
298-
299-
setup_include() {
300-
const ret = commands.parseInclude(this);
301-
if (ret.error !== undefined) {
302-
ret.line = this.get_current_command_line();
303-
if (ast.length !== 0) {
304-
ret.error += ` (from command \`${ast[0].getErrorText()}\`)`;
305-
}
306-
return ret;
307-
}
308-
const dirPath = path.dirname(this.get_current_context().ast.absolutePath);
309-
const ast = new AstLoader(ret.path, dirPath);
310-
if (ast.hasErrors()) {
311-
return {'errors': ast.errors};
312-
}
313-
this.pushNewContext({
314-
'ast': ast,
315-
'commands': ast.commands,
316-
'currentCommand': 0,
317-
'functionArgs': Object.create(null),
318-
});
319-
// We disable the `increasePos` in the context to prevent it to be done twice.
320-
return this.get_next_command(false);
321-
}
322-
323266
getCurrentFile() {
324267
const context = this.get_current_context();
325268
if (context === null) {
@@ -335,14 +278,7 @@ class ParserWithContext {
335278
// through `ParserWithContext`.
336279
this.elems = ast;
337280

338-
if (order === 'call-function') {
339-
// We need to special-case `call-function` since it needs to access variables of this
340-
// class.
341-
return this.setup_user_function_call(ast);
342-
} else if (order === 'include') {
343-
// We need to special-case `include` since it needs to parse a new file when called.
344-
return this.setup_include();
345-
} else if (!Object.prototype.hasOwnProperty.call(ORDERS, order)) {
281+
if (!Object.prototype.hasOwnProperty.call(ORDERS, order)) {
346282
return {'error': `Unknown command "${order}"`, 'line': this.get_current_command_line()};
347283
}
348284
if (this.firstGotoParsed === false) {
@@ -367,10 +303,15 @@ class ParserWithContext {
367303
if (res.error !== undefined) {
368304
res.line = this.get_current_command_line();
369305
if (this.elems.length !== 0) {
370-
res.error += ` (from command \`${this.elems[0].getErrorText()}\`)`;
306+
res.error += ` (from command \`${order}: ${this.elems[0].getErrorText()}\`)`;
371307
}
372308
return res;
373309
}
310+
if (res.skipInstructions) {
311+
// We disable the `increasePos` in the context to prevent it to be done twice.
312+
return this.get_next_command(false);
313+
}
314+
374315
return {
375316
'fatal_error': FATAL_ERROR_COMMANDS.indexOf(order) !== -1,
376317
'wait': res['wait'],

src/commands/all.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ module.exports = {
125125
'parseWaitForTextFalse': wait.parseWaitForTextFalse,
126126
'parseWaitForWindowProperty': wait.parseWaitForWindowProperty,
127127
'parseWaitForWindowPropertyFalse': wait.parseWaitForWindowPropertyFalse,
128+
'parseWithinIFrame': context_setters.parseWithinIFrame,
128129
'parseWrite': input.parseWrite,
129130
'parseWriteInto': input.parseWriteInto,
130131
};

src/commands/context_setters.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,31 @@ if (oldValue !== true) {
213213
};
214214
}
215215

216+
// Possible inputs:
217+
//
218+
// * (selector, block)
219+
function parseWithinIFrame(parser) {
220+
const ret = validator(parser,
221+
{
222+
kind: 'tuple',
223+
elements: [
224+
{
225+
kind: 'selector',
226+
},
227+
{
228+
kind: 'block',
229+
},
230+
],
231+
},
232+
);
233+
if (hasError(ret)) {
234+
return ret;
235+
}
236+
return {
237+
'instructions': [],
238+
};
239+
}
240+
216241
module.exports = {
217242
'parseDebug': parseDebug,
218243
'parseExpectFailure': parseExpectFailure,
@@ -223,4 +248,5 @@ module.exports = {
223248
'parseScreenshotOnFailure': parseScreenshotOnFailure,
224249
'parseShowText': parseShowText,
225250
'parseSetTimeout': parseSetTimeout,
251+
'parseWithinIFrame': parseWithinIFrame,
226252
};

src/commands/functions.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,30 @@ the \`define-function\` command`,
9999
${plural('argument', expected_args)}, found ${args.length}`,
100100
};
101101
}
102+
103+
const funcArgs = Object.create(null);
104+
const func = parser.definedFunctions[func_name];
105+
for (const arg_name of func['arguments']) {
106+
const index = args.findIndex(arg => arg.key.value === arg_name);
107+
if (index === -1) {
108+
return {
109+
'error': `Missing argument "${arg_name}"`,
110+
'line': parser.get_current_command_line(),
111+
'fatal_error': true,
112+
};
113+
}
114+
funcArgs[arg_name] = args[index].value;
115+
}
116+
const context = parser.get_current_context();
117+
parser.pushNewContext({
118+
'ast': context.ast,
119+
'commands': func.commands,
120+
'currentCommand': 0,
121+
'functionArgs': Object.assign({}, context.functionArgs, funcArgs),
122+
'filePath': func.filePath,
123+
});
102124
return {
103-
'function': func_name,
104-
'args': args,
125+
'skipInstructions': true,
105126
};
106127
}
107128

src/commands/general.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Contains commands which don't really have a "category".
22

3+
const { AstLoader } = require('../ast.js');
34
const path = require('path');
45
const { getAndSetElements, indentString } = require('./utils.js');
56
const { validator } = require('../validator.js');
@@ -224,7 +225,22 @@ function parseInclude(parser) {
224225
return ret;
225226
}
226227

227-
return {'path': ret.value.value };
228+
const includePath = ret.value.value;
229+
const dirPath = path.dirname(parser.get_current_context().ast.absolutePath);
230+
const ast = new AstLoader(includePath, dirPath);
231+
if (ast.hasErrors()) {
232+
return {'errors': ast.errors};
233+
}
234+
parser.pushNewContext({
235+
'ast': ast,
236+
'commands': ast.commands,
237+
'currentCommand': 0,
238+
'functionArgs': Object.create(null),
239+
});
240+
241+
return {
242+
'skipInstructions': true,
243+
};
228244
}
229245

230246
module.exports = {

tests/api-output/parseInclude/basic-1.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/api-output/parseInclude/basic-2.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
instructions = [
2+
"""const iframe = await page.$(\"::-p-xpath(//a)\");
3+
if (iframe === null) { throw '\"//a\" not found'; }
4+
await iframe.evaluate(el => {
5+
if (el.tagName !== \"IFRAME\") {
6+
throw \"selector `//a` is not an `<iframe>` but a `<\" + el.tagName.toLowerCase() + \">`\";
7+
}
8+
});
9+
pages.push(iframe);""",
10+
]
11+
noPosIncrease = true

tests/ui/device-pixel-ratio.output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=> Starting doc-ui tests...
22

33
device-pixel-ratio... FAILED
4-
[ERROR] `tests/ui/device-pixel-ratio.goml` line 7: expected numbers above 0, found `-2` (from command `-2`)
4+
[ERROR] `tests/ui/device-pixel-ratio.goml` line 7: expected numbers above 0, found `-2` (from command `set-device-pixel-ratio: -2`)
55

66
<= doc-ui tests done: 0 succeeded, 1 failed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=> Starting doc-ui tests...
22

33
function-named-map-extra-argument... FAILED
4-
[ERROR] `tests/ui/function-named-map-extra-argument.goml` line 14: function `fn1` expected 2 arguments, found 3 (from command `("fn1", {"background_color": "a", "whole_check": {"color": "a"}, "k": {"color": "a"}})`)
4+
[ERROR] `tests/ui/function-named-map-extra-argument.goml` line 14: function `fn1` expected 2 arguments, found 3 (from command `call-function: ("fn1", {"background_color": "a", "whole_check": {"color": "a"}, "k": {"color": "a"}})`)
55

66
<= doc-ui tests done: 0 succeeded, 1 failed

0 commit comments

Comments
 (0)