Skip to content

Commit c725559

Browse files
committed
feat: Support local option "verbose" to increase only this tasks' log level
1 parent c9cc890 commit c725559

File tree

16 files changed

+98
-97
lines changed

16 files changed

+98
-97
lines changed

tasks/html-dom-snapshot.js

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

3-
const {writeFile} = require('fs')
3+
const { writeFile } = require('fs')
44
const pad = require('pad-left')
5-
const {basename, dirname, isAbsolute, join} = require('path')
5+
const { basename, dirname, isAbsolute, join } = require('path')
66
const mkdirp = require('mkdirp')
77
// Instructions are listed explicitly here to ensure their right execution
88
// order in a command object. In this order they will be looked for and
@@ -53,7 +53,8 @@ module.exports = grunt => {
5353
fileNumberSeparator: '.',
5454
hangOnError: false,
5555
snapshotOnError: '_last-error',
56-
force: false
56+
force: false,
57+
verbose: false
5758
})
5859
const target = this.target
5960
const pages = data.pages
@@ -74,6 +75,7 @@ module.exports = grunt => {
7475
let screenshotCount = 0
7576
let failed
7677
let commands
78+
grunt.output = options.verbose ? grunt.log : grunt.verbose
7779
if (browserCapabilities) {
7880
grunt.log.warn('The property "browserCapabilities" is deprecated. ' +
7981
'Use "webdriver.desiredCapabilities" with the same content.')
@@ -95,15 +97,15 @@ module.exports = grunt => {
9597
// TODO: Remove this, as soon as the moveTo command is re-implemented.
9698
webdriver.deprecationWarnings = false
9799

98-
grunt.verbose.writeln('Open web browser window for the target "' +
100+
grunt.output.writeln('Open web browser window for the target "' +
99101
target + '".')
100102
let client = webdriverio.remote(webdriver)
101103
client.init()
102-
.then(setViewportSize)
103-
.then(gatherCommands)
104-
.then(performComplexCommands)
105-
.then(() => {
106-
grunt.log.ok(commands.length + ' ' +
104+
.then(setViewportSize)
105+
.then(gatherCommands)
106+
.then(performComplexCommands)
107+
.then(() => {
108+
grunt.log.ok(commands.length + ' ' +
107109
grunt.util.pluralize(commands.length, 'command/commands') +
108110
' performed, ' + urlCount + ' ' +
109111
grunt.util.pluralize(urlCount, 'page/pages') +
@@ -112,38 +114,38 @@ module.exports = grunt => {
112114
' and ' + screenshotCount + ' ' +
113115
grunt.util.pluralize(screenshotCount, 'screenshot/screenshots') +
114116
' written.')
115-
return stop(false)
116-
})
117-
.catch(error => {
118-
failed = true
119-
grunt.verbose.error(error.stack)
120-
grunt.log.error(error)
121-
})
122-
.then(() => {
123-
if (failed && snapshotOnError) {
124-
return makeFailureSnapshotAndScreenshot()
125-
.then(() => true, () => false)
126-
}
127-
})
128-
.then(() => {
129-
if (failed && !hangOnError) {
130-
return stop(false)
131-
}
132-
})
133-
.then(() => {
134-
if (failed) {
135-
const warn = options.force || hangOnError ? grunt.log.warn : grunt.fail.warn
136-
warn('Taking snapshots failed.')
137-
if (hangOnError) {
138-
warn('Letting the browser run for your investigation.\nTerminate this process or interrupt it by Ctrl+C, once you are finished.')
139-
}
140-
}
141-
})
142-
.then(() => {
143-
if (!hangOnError) {
144-
done()
145-
}
146-
})
117+
return stop(false)
118+
})
119+
.catch(error => {
120+
failed = true
121+
grunt.output.error(error.stack)
122+
grunt.log.error(error)
123+
})
124+
.then(() => {
125+
if (failed && snapshotOnError) {
126+
return makeFailureSnapshotAndScreenshot()
127+
.then(() => true, () => false)
128+
}
129+
})
130+
.then(() => {
131+
if (failed && !hangOnError) {
132+
return stop(false)
133+
}
134+
})
135+
.then(() => {
136+
if (failed) {
137+
const warn = options.force || hangOnError ? grunt.log.warn : grunt.fail.warn
138+
warn('Taking snapshots failed.')
139+
if (hangOnError) {
140+
warn('Letting the browser run for your investigation.\nTerminate this process or interrupt it by Ctrl+C, once you are finished.')
141+
}
142+
}
143+
})
144+
.then(() => {
145+
if (!hangOnError) {
146+
done()
147+
}
148+
})
147149

148150
process
149151
.on('SIGINT', stop.bind(null, true))
@@ -182,7 +184,7 @@ module.exports = grunt => {
182184
.reduce((scenarios, scenario) =>
183185
scenarios.concat(grunt.file.expand(scenario)), [])
184186
.reduce((scenarios, scenario) => {
185-
grunt.verbose.writeln('Load scenario "' + scenario + '".')
187+
grunt.output.writeln('Load scenario "' + scenario + '".')
186188
if (!isAbsolute(scenario)) {
187189
scenario = join(currentDirectory, scenario)
188190
}
@@ -199,7 +201,7 @@ module.exports = grunt => {
199201
}
200202

201203
function setViewportSize () {
202-
grunt.verbose.writeln('Resize viewport to ' + lastViewport.width +
204+
grunt.output.writeln('Resize viewport to ' + lastViewport.width +
203205
'x' + lastViewport.height + '.')
204206
return client.setViewportSize(lastViewport)
205207
}
@@ -241,23 +243,23 @@ module.exports = grunt => {
241243
}
242244

243245
function performConditionalCommand (ifCommands, command) {
244-
grunt.verbose.writeln('Testing a condition.')
246+
grunt.output.writeln('Testing a condition.')
245247
const promise = new Promise((resolve, reject) => {
246248
performCommands(ifCommands)
247249
.then(() => performConditionalBranch(command.then, true)
248250
.then(resolve, reject))
249251
.catch(() => performConditionalBranch(command.else, false)
250252
.then(resolve, reject))
251253
})
252-
promise.finally(() => grunt.verbose.writeln('The conditional command ended.'))
254+
promise.finally(() => grunt.output.writeln('The conditional command ended.'))
253255
return promise
254256
}
255257

256258
function performConditionalBranch (branch, result) {
257259
const commands = ensureArray(branch)
258-
grunt.verbose.writeln('The condition evaluated to ' + result + '.')
260+
grunt.output.writeln('The condition evaluated to ' + result + '.')
259261
if (commands) {
260-
grunt.verbose.writeln('Continuing with the conditional branch.')
262+
grunt.output.writeln('Continuing with the conditional branch.')
261263
return performComplexCommands(commands)
262264
}
263265
return Promise.resolve()
@@ -267,7 +269,7 @@ module.exports = grunt => {
267269
function runWhile () {
268270
return new Promise((resolve, reject) => {
269271
updatePromise(reject)
270-
grunt.verbose.writeln('Testing a condition before loop.')
272+
grunt.output.writeln('Testing a condition before loop.')
271273
performCommands(whileCommands)
272274
.then(() => {
273275
performLoopBody(command.do, 'Continuing with')
@@ -297,7 +299,7 @@ module.exports = grunt => {
297299
updatePromise(reject)
298300
performLoopBody(command.do, 'Starting with')
299301
.then(() => {
300-
grunt.verbose.writeln('Testing a condition after loop.')
302+
grunt.output.writeln('Testing a condition after loop.')
301303
performCommands(untilCommands)
302304
.then(() => resolve())
303305
.catch(() => runUntil()
@@ -353,7 +355,7 @@ module.exports = grunt => {
353355
function performLoopBody (body, prefix) {
354356
const commands = ensureArray(body)
355357
if (commands) {
356-
grunt.verbose.writeln(prefix + ' the loop body.')
358+
grunt.output.writeln(prefix + ' the loop body.')
357359
return performComplexCommands(commands)
358360
}
359361
return Promise.resolve()
@@ -369,7 +371,7 @@ module.exports = grunt => {
369371
if (promise instanceof Promise) {
370372
promise.finally(() => {
371373
clearTimeout(timer)
372-
grunt.verbose.writeln('The loop ended.')
374+
grunt.output.writeln('The loop ended.')
373375
})
374376
} else {
375377
rejectAll = promise
@@ -435,14 +437,14 @@ module.exports = grunt => {
435437
const detected = instruction.detected
436438
if (detected) {
437439
return performInstruction(instruction.perform(grunt, target,
438-
client, command, commandOptions, detected))
440+
client, command, commandOptions, detected))
439441
}
440442
}), viewportSet)
441-
.then(() => {
442-
if (file) {
443-
return makeSnapshotAndScreenshot()
444-
}
445-
})
443+
.then(() => {
444+
if (file) {
445+
return makeSnapshotAndScreenshot()
446+
}
447+
})
446448

447449
function performInstruction (promise) {
448450
if (instructionDelay) {
@@ -470,12 +472,12 @@ module.exports = grunt => {
470472

471473
function makeSnapshot () {
472474
return client.getHTML('html')
473-
.then(saveContent)
475+
.then(saveContent)
474476
}
475477

476478
function makeScreenshot () {
477479
return client.screenshot()
478-
.then(saveImage)
480+
.then(saveImage)
479481
}
480482

481483
function saveContent (html) {
@@ -540,9 +542,9 @@ module.exports = grunt => {
540542
function makeScreenshotName (fileName) {
541543
fileName = fileName.toLowerCase()
542544
return fileName.endsWith('.html')
543-
? fileName.substr(0, fileName.length - 5)
544-
: fileName.endsWith('.htm')
545-
? fileName.substr(0, fileName.length - 4) : fileName
545+
? fileName.substr(0, fileName.length - 5)
546+
: fileName.endsWith('.htm')
547+
? fileName.substr(0, fileName.length - 4) : fileName
546548
}
547549

548550
function makeFailureSnapshotAndScreenshot () {
@@ -560,12 +562,12 @@ module.exports = grunt => {
560562

561563
function makeFailureSnapshot () {
562564
return client.getHTML('html')
563-
.then(saveFailureContent)
565+
.then(saveFailureContent)
564566
}
565567

566568
function makeFailureScreenshot () {
567569
return client.screenshot()
568-
.then(saveFailureImage)
570+
.then(saveFailureImage)
569571
}
570572

571573
function saveFailureContent (html) {

tasks/instructions/abort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77

88
perform: function (grunt, target, client, command) {
99
const reason = command.abort
10-
grunt.verbose.writeln('Aborting: "' + reason + '".')
10+
grunt.output.writeln('Aborting: "' + reason + '".')
1111
throw new Error('Aborted: "' + reason +
1212
'" in the target "' + target + '".\n' +
1313
JSON.stringify(command))

tasks/instructions/addValue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
const addValue = command.addValue
1010
const selector = addValue.selector
1111
const value = addValue.value
12-
grunt.verbose.writeln('Add "' + value + '" to value of "' +
12+
grunt.output.writeln('Add "' + value + '" to value of "' +
1313
selector + '".')
1414
return client.addValue(selector, value)
1515
}

tasks/instructions/break.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77

88
perform: function (grunt, target, client, command) {
99
const reason = command.break
10-
grunt.verbose.writeln('Breaking the loop: "' + reason + '".')
10+
grunt.output.writeln('Breaking the loop: "' + reason + '".')
1111
var error = new Error('Break the loop')
1212
error.break = reason
1313
throw error

tasks/instructions/clearValue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77

88
perform: function (grunt, target, client, command) {
99
const clearValue = command.clearValue
10-
grunt.verbose.writeln('Clear value of "' + clearValue + '".')
10+
grunt.output.writeln('Clear value of "' + clearValue + '".')
1111
return client.clearElement(clearValue)
1212
}
1313
}

tasks/instructions/click.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77

88
perform: function (grunt, target, client, command) {
99
const click = command.click
10-
grunt.verbose.writeln('Click on "' + click + '".')
10+
grunt.output.writeln('Click on "' + click + '".')
1111
return client.click(click)
1212
}
1313
}

tasks/instructions/clickIfVisible.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ module.exports = {
66
},
77

88
perform: function (grunt, target, client, command) {
9-
console.log('target "' + target + '".')
109
const clickIfVisible = command.clickIfVisible
11-
grunt.verbose.writeln('Click on "' + clickIfVisible + '" if visible.')
10+
grunt.output.writeln('Checking visibility of "' + clickIfVisible + '"...')
1211
client.isVisible(clickIfVisible).then(function (value) {
1312
if (value === true) {
14-
grunt.verbose.writeln('Visible: "' + clickIfVisible + '".')
13+
grunt.output.writeln('Click on "' + clickIfVisible + '".')
1514
return client.click(clickIfVisible)
1615
} else {
17-
grunt.verbose.writeln('Not visible: "' + clickIfVisible + '".')
16+
grunt.output.writeln('"' + clickIfVisible + '" is not visible.')
1817
}
1918
})
2019
}

tasks/instructions/focus.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ module.exports = {
1313

1414
perform: function (grunt, target, client, command) {
1515
const focus = command.focus
16-
grunt.verbose.writeln('Focus "' + focus + '".')
16+
grunt.output.writeln('Focus "' + focus + '".')
1717
return client.execute(function (selector) {
1818
var element = document.querySelector(selector)
1919
if (!element) {
2020
return false
2121
}
2222
element.focus()
2323
}, focus)
24-
.then(function (value) {
25-
if (value === false) {
26-
throw new Error('"' + focus + '" does not exist.')
27-
}
28-
})
24+
.then(function (value) {
25+
if (value === false) {
26+
throw new Error('"' + focus + '" does not exist.')
27+
}
28+
})
2929
}
3030
}

tasks/instructions/go.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
'" in the target "' + target + '".\n' +
1414
JSON.stringify(command))
1515
}
16-
grunt.verbose.writeln('Perform navigation: "' + go + '".')
16+
grunt.output.writeln('Perform navigation: "' + go + '".')
1717
return client[go]()
1818
}
1919
}

tasks/instructions/keys.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ module.exports = {
99
let keys = command.keys
1010
const separateKeys = Array.isArray(keys)
1111
const message = separateKeys
12-
? 'Send keys "' + keys.join('", "') + '".'
13-
: 'Send text "' + keys + '".'
12+
? 'Send keys "' + keys.join('", "') + '".'
13+
: 'Send text "' + keys + '".'
1414
if (!separateKeys) {
1515
keys = keys.split('')
1616
}
17-
grunt.verbose.writeln(message)
17+
grunt.output.writeln(message)
1818
return client.keys(keys)
1919
}
2020
}

0 commit comments

Comments
 (0)