@@ -13,7 +13,6 @@ const {writeFile} = require('fs')
1313const pad = require ( 'pad-left' )
1414const { basename, dirname, isAbsolute, join} = require ( 'path' )
1515const mkdirp = require ( 'mkdirp' )
16- const nodeCleanup = require ( 'node-cleanup' )
1716const instructions = [
1817 'setViewport' , 'url' , 'go' , 'scroll' , 'clearValue' , 'setValue' , 'addValue' ,
1918 'selectOptionByIndex' , 'selectOptionByValue' , 'moveCursor' ,
@@ -69,6 +68,7 @@ module.exports = grunt => {
6968 let urlCount = 0
7069 let snapshotCount = 0
7170 let screenshotCount = 0
71+ let failed
7272 let commands
7373 if ( browserCapabilities ) {
7474 grunt . log . warn ( 'The property "browserCapabilities" is deprecated. ' +
@@ -93,7 +93,6 @@ module.exports = grunt => {
9393 target + '".' )
9494 let client = webdriverio . remote ( webdriver )
9595 client . init ( )
96- . then ( ( ) => nodeCleanup ( stop ) )
9796 . then ( setViewportSize )
9897 . then ( gatherCommands )
9998 . then ( performConditionalCommands )
@@ -107,25 +106,51 @@ module.exports = grunt => {
107106 ' and ' + screenshotCount + ' ' +
108107 grunt . util . pluralize ( screenshotCount , 'screenshot/screenshots' ) +
109108 ' written.' )
110- return stop ( )
109+ return stop ( false )
111110 } )
112111 . catch ( error => {
112+ failed = true
113113 grunt . verbose . error ( error . stack )
114114 grunt . log . error ( error )
115- const warn = options . force ? grunt . log . warn : grunt . fail . warn
116- warn ( 'Taking snapshots failed.' )
117- return stop ( )
115+ return stop ( false )
118116 } )
119- . then ( done )
117+ . then ( ( ) => {
118+ if ( failed ) {
119+ const warn = options . force ? grunt . log . warn : grunt . fail . warn
120+ warn ( 'Taking snapshots failed.' )
121+ }
122+ } )
123+ . then ( ( ) => {
124+ if ( ! failed ) {
125+ done ( )
126+ }
127+ } )
128+
129+ process
130+ . on ( 'SIGINT' , stop . bind ( null , true ) )
131+ . on ( 'SIGTERM' , stop . bind ( null , true ) )
120132
121- function stop ( ) {
133+ function stop ( exit ) {
134+ function exitProcess ( ) {
135+ if ( exit ) {
136+ grunt . log . writeln ( 'Stopping the process...' )
137+ process . exit ( 1 )
138+ }
139+ }
122140 if ( client ) {
123- const backup = client
141+ const oldClient = client
124142 client = null
125- return backup . end ( )
143+ grunt . log . writeln ( 'Closing the browser in one second...' )
144+ const result = oldClient
145+ . end ( )
126146 // Workaround for hanging chromedriver; for more information
127147 // see https://github.com/vvo/selenium-standalone/issues/351
128- . pause ( 100 )
148+ . pause ( 1000 )
149+ // The promise returned from pause() appears to never resolve.
150+ setTimeout ( exitProcess , 1500 )
151+ return result
152+ } else {
153+ exitProcess ( )
129154 }
130155 }
131156
0 commit comments