Skip to content

Commit c490a77

Browse files
committed
fix: Stop Selenium and Shromedriver processes on unexpected Grunt process abortion
1 parent 73a4082 commit c490a77

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ your code using Grunt.
497497
498498
## Release History
499499
500+
* 2018-03-28 [v1.0.2] Stop Selenium and Chromedriver processes on unexpected Grunt process abortion
500501
* 2018-03-28 [v1.0.1] Workaround for hanging chromedriver after finishing the task
501502
* 2018-03-11 [v1.0.0] Require Node.js >= 6
502503
* 2018-03-11 [v0.8.0] Add a new instruction - "abort"

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
},
3737
"dependencies": {
3838
"mkdirp": "^0.5.1",
39+
"node-cleanup": "^2.1.2",
3940
"pad-left": "^2.1.0",
4041
"webdriverio": "^4.12.0"
4142
},

tasks/html-dom-snapshot.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const fs = require('fs')
1313
const pad = require('pad-left')
1414
const path = require('path')
1515
const mkdirp = require('mkdirp')
16+
const nodeCleanup = require('node-cleanup')
1617
const 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

3031
module.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

Comments
 (0)