@@ -13,6 +13,7 @@ const fs = require('fs')
1313const pad = require ( 'pad-left' )
1414const path = require ( 'path' )
1515const mkdirp = require ( 'mkdirp' )
16+ const nodeCleanup = require ( 'node-cleanup' )
1617const instructions = [
1718 'setViewport' , 'url' , 'go' , 'clearValue' , 'setValue' , 'addValue' ,
1819 'selectOptionByIndex' , 'selectOptionByValue' , 'moveCursor' ,
@@ -25,7 +26,7 @@ const instructions = [
2526] . map ( function ( instruction ) {
2627 return require ( './instructions/' + instruction )
2728} )
28- var fileCount = 0
29+ let fileCount = 0
2930
3031module . exports = function ( grunt ) {
3132 grunt . registerMultiTask ( 'html-dom-snapshot' ,
@@ -61,13 +62,13 @@ module.exports = function (grunt) {
6162 width : viewport . width ,
6263 height : viewport . height
6364 }
64- const client = webdriverio . remote ( {
65+ let client = webdriverio . remote ( {
6566 desiredCapabilities : options . browserCapabilities
6667 } )
67- var urlCount = 0
68- var snapshotCount = 0
69- var screenshotCount = 0
70- var commands
68+ let urlCount = 0
69+ let snapshotCount = 0
70+ let screenshotCount = 0
71+ let commands
7172 if ( pages ) {
7273 grunt . log . warn ( 'The property "pages" is deprecated. ' +
7374 'Use "commands" with the same content.' )
@@ -82,6 +83,9 @@ module.exports = function (grunt) {
8283 grunt . verbose . writeln ( 'Open web browser window for the target "' +
8384 target + '".' )
8485 client . init ( )
86+ . then ( function ( ) {
87+ nodeCleanup ( stop )
88+ } )
8589 . then ( setViewportSize )
8690 . then ( gatherCommands )
8791 . then ( performCommands )
@@ -95,19 +99,28 @@ module.exports = function (grunt) {
9599 ' and ' + screenshotCount + ' ' +
96100 grunt . util . pluralize ( screenshotCount , 'screenshot/screenshots' ) +
97101 ' written.' )
98- return client . end ( )
99- // Workaround for hanging chromedriver; for more information
100- // see https://github.com/vvo/selenium-standalone/issues/351
101- . pause ( 100 )
102+ return stop ( )
102103 } )
103104 . catch ( function ( error ) {
104105 grunt . verbose . error ( error . stack )
105106 grunt . log . error ( error )
106107 const warn = options . force ? grunt . log . warn : grunt . fail . warn
107108 warn ( 'Taking snapshots failed.' )
109+ return stop ( )
108110 } )
109111 . then ( done )
110112
113+ function stop ( ) {
114+ if ( client ) {
115+ const backup = client
116+ client = null
117+ return backup . end ( )
118+ // Workaround for hanging chromedriver; for more information
119+ // see https://github.com/vvo/selenium-standalone/issues/351
120+ . pause ( 100 )
121+ }
122+ }
123+
111124 function gatherCommands ( ) {
112125 let scenarios = data . scenarios
113126 commands = data . commands || pages
@@ -179,8 +192,8 @@ module.exports = function (grunt) {
179192 detected : instruction . detect ( command )
180193 }
181194 } )
182- var snapshots = commandOptions . dest
183- var viewportSet
195+ let snapshots = commandOptions . dest
196+ let viewportSet
184197 if ( snapshots ) {
185198 grunt . log . warn ( 'The property "dest" is deprecated. ' +
186199 'Use "snapshots" with the same content.' )
@@ -241,7 +254,7 @@ module.exports = function (grunt) {
241254
242255 function saveContent ( html ) {
243256 if ( file ) {
244- var fileName = file . toLowerCase ( )
257+ let fileName = file . toLowerCase ( )
245258 fileName = fileName . endsWith ( '.html' ) ||
246259 fileName . endsWith ( '.htm' ) ? file : file + '.html'
247260 if ( fileNumbering ) {
@@ -268,7 +281,7 @@ module.exports = function (grunt) {
268281
269282 function saveImage ( png ) {
270283 if ( file ) {
271- var fileName = file . toLowerCase ( )
284+ let fileName = file . toLowerCase ( )
272285 fileName = fileName . endsWith ( '.html' )
273286 ? file . substr ( 0 , file . length - 5 )
274287 : fileName . endsWith ( '.htm' )
0 commit comments