Skip to content

Commit 7087f88

Browse files
committed
feat: Introduce conditional if-then-else instructions
1 parent aa4d8ce commit 7087f88

File tree

4 files changed

+113
-11
lines changed

4 files changed

+113
-11
lines changed

Gruntfile.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,35 @@ module.exports = function (grunt) {
344344
value: '1'
345345
}
346346
},
347+
{
348+
if: {
349+
hasClass: {
350+
selector: 'div',
351+
value: 'class none'
352+
}
353+
},
354+
else: {
355+
wait: function () {
356+
throw new Error('executed else branch')
357+
}
358+
}
359+
},
360+
{
361+
if: [{
362+
hasClass: {
363+
selector: 'div',
364+
value: 'dummy'
365+
}
366+
}],
367+
then: [{
368+
wait: function () {
369+
throw new Error('executed then branch')
370+
}
371+
}],
372+
else: [{
373+
wait: 1
374+
}]
375+
},
347376
{
348377
setViewport: {
349378
width: 768,

INSTRUCTIONS.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
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:
44

5+
- [if-then-else](#if-then-else)
56
- [setViewport](#setviewport)
67
- [url](#url)
78
- [go](#go)
@@ -36,6 +37,42 @@ One of the instructions has to be present in every command. These properties are
3637
- [abort](#abort)
3738
- [file](#file)
3839

40+
## if-then-else
41+
42+
Conditional command consisting of two or three three instructions, which each points to a sub-command or to an array of sub-commands to perform:
43+
44+
### if
45+
Type: `Object` | `Array` (mandatory)
46+
47+
### then
48+
Type: `Object` | `Array` (optional)
49+
50+
### else
51+
Type: `Object` | `Array` (optional)
52+
53+
At first the sub-commands from the `if` instruction are performed. If they succeed, the execution continues with sub-sommands from the `then` instruction. If they fail, the execution continues with sub-sommands from the `else` instruction. The success or failure of the whole conditional command will depend on either of `then` and `else` instructions, which are executed. The success or failure of the `if` instruction decides only the following condition branch.
54+
55+
```js
56+
{
57+
url: 'https://google.com',
58+
wait: '#lst-ib',
59+
if: {
60+
hasAttribute: {
61+
selector: '#lst-ib',
62+
name: 'autocomplete',
63+
value: 'off'
64+
}
65+
},
66+
then: {
67+
setValue: {
68+
selector: '#lst-ib',
69+
value: 'autocomplete is off'
70+
}
71+
},
72+
file: 'google'
73+
}
74+
```
75+
3976
#### setViewport
4077
Type: `Object`
4178

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ your code using Grunt.
546546
547547
## Release History
548548
549-
* 2018-11-26 [v2.0.0] Use headless Chrome instead of PhantomJS by default
549+
* 2018-11-26 [v2.0.0] Use headless Chrome instead of PhantomJS by default, introduce conditional if-then-else instructions
550550
* 2018-05-14 [v1.3.0] Allow saving snapshots to sub-directories, file numbering per-directory, add `scroll` instruction
551551
* 2018-05-11 [v1.2.0] Introduce delay after every instruction to be able to visually follow the actions when debugging
552552
* 2018-03-29 [v1.1.0] Allow specifying all initialization parameters supported by WebdriverIO

tasks/html-dom-snapshot.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ module.exports = grunt => {
9696
.then(() => nodeCleanup(stop))
9797
.then(setViewportSize)
9898
.then(gatherCommands)
99-
.then(performCommands)
99+
.then(performConditionalCommands)
100100
.then(() => {
101101
grunt.log.ok(commands.length + ' ' +
102102
grunt.util.pluralize(commands.length, 'command/commands') +
@@ -130,12 +130,9 @@ module.exports = grunt => {
130130
}
131131

132132
function gatherCommands () {
133-
let scenarios = data.scenarios
133+
let scenarios = ensureArray(data.scenarios)
134134
commands = data.commands || pages
135135
if (scenarios) {
136-
if (!Array.isArray(scenarios)) {
137-
scenarios = [scenarios]
138-
}
139136
const currentDirectory = process.cwd()
140137
commands = scenarios
141138
.reduce((scenarios, scenario) =>
@@ -157,11 +154,6 @@ module.exports = grunt => {
157154
}
158155
}
159156

160-
function performCommands () {
161-
return commands.reduce((promise, command) =>
162-
promise.then(() => performCommand(command)), Promise.resolve())
163-
}
164-
165157
function setViewportSize () {
166158
grunt.verbose.writeln('Resize viewport to ' + lastViewport.width +
167159
'x' + lastViewport.height + '.')
@@ -179,6 +171,43 @@ module.exports = grunt => {
179171
}))
180172
}
181173

174+
function performConditionalCommands (subCommands) {
175+
return (subCommands || commands).reduce((promise, command) =>
176+
promise.then(() => performConditionalCommand(command)), Promise.resolve())
177+
}
178+
179+
function performConditionalCommand (command) {
180+
const ifCommands = ensureArray(command.if)
181+
if (!ifCommands) {
182+
return performCommand(command)
183+
}
184+
grunt.verbose.writeln('Testing a condition.')
185+
const promise = performCommands(ifCommands)
186+
.then(() => performConditionalBranch(command.then, true))
187+
.catch(() => performConditionalBranch(command.else, false))
188+
promise.then(logEnd, logEnd)
189+
return promise
190+
191+
function logEnd () {
192+
grunt.verbose.writeln('The conditional command ended.')
193+
}
194+
}
195+
196+
function performConditionalBranch (branch, result) {
197+
const commands = ensureArray(branch)
198+
grunt.verbose.writeln('The condition evaluated to ' + result + '.')
199+
if (commands) {
200+
grunt.verbose.writeln('Continuing with the conditional branch.')
201+
return performConditionalCommands(commands)
202+
}
203+
return Promise.resolve()
204+
}
205+
206+
function performCommands (subCommands) {
207+
return (subCommands || commands).reduce((promise, command) =>
208+
promise.then(() => performCommand(command)), Promise.resolve())
209+
}
210+
182211
function performCommand (command) {
183212
const commandOptions = Object.assign({
184213
lastViewport: lastViewport
@@ -350,5 +379,12 @@ module.exports = grunt => {
350379
return fileName
351380
}
352381
}
382+
383+
function ensureArray (item) {
384+
if (item && !Array.isArray(item)) {
385+
item = [item]
386+
}
387+
return item
388+
}
353389
})
354390
}

0 commit comments

Comments
 (0)