11'use strict'
22
3- const { writeFile} = require ( 'fs' )
3+ const { writeFile } = require ( 'fs' )
44const pad = require ( 'pad-left' )
5- const { basename, dirname, isAbsolute, join} = require ( 'path' )
5+ const { basename, dirname, isAbsolute, join } = require ( 'path' )
66const 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 ) {
0 commit comments