Skip to content

Commit f9e1d9a

Browse files
committed
fix: Report unknown instructions as errors
BREAKING CHANGE: If you include a key in your command object, which is not recognized as an instruction or other known key (file, options), the task execution will fail. Earlier you could put any key to a command object. You could use it as a comment or to store any other data there. Unfortunately, it lead to mistakes like typos in instruction names, which were difficult to detect. From now on, only recognized keys are allowed to appear in commands. You can use JavaScript comments to explain what a particular command or instruction does in your scenario.
1 parent 2d2a228 commit f9e1d9a

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

Gruntfile.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ module.exports = function (grunt) {
277277
isNotFocused: 'input',
278278
isNotVisible: 'input',
279279
isNotVisibleWithinViewport: 'input',
280-
hasAtribute: {
280+
hasAttribute: {
281281
selector: 'input',
282282
name: 'disabled',
283-
value: 'disabled'
283+
value: 'true'
284284
},
285285
hasValue: {
286286
selector: 'input',
@@ -396,6 +396,14 @@ module.exports = function (grunt) {
396396
{}
397397
]
398398
},
399+
'invalid-instruction': {
400+
options: {
401+
force: true
402+
},
403+
commands: [
404+
{invalid: {}}
405+
]
406+
},
399407
'invalid-file': {
400408
options: {
401409
browserCapabilities: {

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ You can use sub-tasks, `commands` and `scenarios` to structure your code and exe
263263

264264
### Instructions
265265

266-
One of the [instructions] has to be present in every command. These properties are evaluated (and their effect is executed) in the order, in which they are listed below:
266+
One of the [instructions] has to be present in every command, otherwise its execution will fail. If you include a key in your command object, which is not recognised as an instruction or other known key (`file`, `options`), the execution will fail too. The following instructions are recognised (and their effect is executed) in the order, in which they are listed below:
267267

268268
* [setViewport](INSTRUCTIONS.md#setviewport)
269269
* [url](INSTRUCTIONS.md#url)
@@ -597,6 +597,7 @@ your code using Grunt.
597597
598598
## Release History
599599
600+
* 2019-07-20 [v3.0.0] Report unrecognised instructions as errors
600601
* 2019-07-08 [v2.2.0] Optionally hang the browser in case of failure to be able to inspect the web page in developer tools
601602
* 2018-11-26 [v2.0.0] Use headless Chrome instead of PhantomJS by default, introduce conditional if-then-else instructions
602603
* 2018-05-14 [v1.3.0] Allow saving snapshots to sub-directories, file numbering per-directory, add `scroll` instruction
@@ -642,6 +643,7 @@ Licensed under the MIT license.
642643
[grunt-reg-viz]: https://github.com/prantlf/grunt-reg-viz
643644
[grunt-selenium-standalone]: https://github.com/zs-zs/grunt-selenium-standalone
644645
[keyboard key identifiers]: https://w3c.github.io/webdriver/webdriver-spec.html#keyboard-actions
646+
[v3.0.0]: https://github.com/prantlf/grunt-html-dom-snapshot/releases/tag/v3.0.0
645647
[v2.2.0]: https://github.com/prantlf/grunt-html-dom-snapshot/releases/tag/v2.2.0
646648
[v2.0.0]: https://github.com/prantlf/grunt-html-dom-snapshot/releases/tag/v2.0.0
647649
[v1.3.0]: https://github.com/prantlf/grunt-html-dom-snapshot/releases/tag/v1.3.0

tasks/html-dom-snapshot.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ const {writeFile} = require('fs')
44
const pad = require('pad-left')
55
const {basename, dirname, isAbsolute, join} = require('path')
66
const mkdirp = require('mkdirp')
7-
const instructions = [
7+
// Instructions are listed explicitly here to ensure their right execution
8+
// order in a command object. In this order they will be looked for and
9+
// executed.
10+
const instructionKeys = [
811
'setViewport', 'url', 'go', 'scroll', 'clearValue', 'setValue', 'addValue',
912
'selectOptionByIndex', 'selectOptionByValue', 'moveCursor',
1013
'click', 'clickIfVisible', 'keys', 'wait', 'hasAttribute', 'hasClass', 'hasValue',
@@ -13,7 +16,10 @@ const instructions = [
1316
'isVisibleWithinViewport', 'isNotEnabled', 'isNotExisting',
1417
'isNotFocused', 'isNotSelected', 'isNotVisible',
1518
'isNotVisibleWithinViewport', 'abort'
16-
].map(instruction => require('./instructions/' + instruction))
19+
]
20+
const instructions = instructionKeys.map(instruction =>
21+
require('./instructions/' + instruction))
22+
const additionalKeys = ['file', 'options']
1723
const directoryCounts = {}
1824
let fileCount = 0
1925

@@ -255,6 +261,13 @@ module.exports = grunt => {
255261
} else {
256262
snapshots = commandOptions.snapshots
257263
}
264+
Object
265+
.keys(command)
266+
.forEach(key => {
267+
if (!(instructionKeys.includes(key) || additionalKeys.includes(key))) {
268+
throw new Error('Unrecognized instruction: "' + key + '".')
269+
}
270+
})
258271
if (!(commandInstructions.some(instruction => instruction.detected) || file)) {
259272
throw new Error('Missing instruction in the command ' +
260273
'in the target "' + target + '".\n' +

0 commit comments

Comments
 (0)