grunt-protractor-webdriver
Advanced tools
Comparing version
{ | ||
"name": "grunt-protractor-webdriver", | ||
"description": "grunt plugin for starting Protractor's bundled Selenium Webdriver", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"homepage": "https://github.com/seckardt/grunt-protractor-webdriver", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -83,2 +83,3 @@ # grunt-protractor-webdriver | ||
* v0.1.2 - Harden waiting for all browser sessions to be deleted before shutdown. Due to possible race-conditions with log statements for multiple browser sessions in one line, the session counter didn't work properly. | ||
* v0.1.1 - Ensure waiting for eventual `Selenium is already running` message on failure. Ensure waiting for all browser sessions to be deleted before shutdown. | ||
@@ -85,0 +86,0 @@ * v0.1.0 - Initial commit. |
@@ -21,11 +21,12 @@ /** | ||
noop = function () {}, | ||
REMOTE_REGEXP = /RemoteWebDriver instances should connect to: (.*)/, | ||
STARTED_REGEXP = /Started org\.openqa\.jetty\.jetty\.Server/, | ||
RUNNING_REGEXP = /Selenium is already running/, | ||
FAILURE_REGEXP = /Failed to start/, | ||
SESSION_DELETE_REGEXP = /Executing: \[delete session: (.*)\]/, | ||
SESSION_NEW_REGEXP = /Executing: \[new session: (.*)\]/, | ||
EXCEPTION_REGEXP = /Exception thrown(.*)/m, | ||
FATAL_REGEXP = /Fatal error/, | ||
SHUTDOWN_OK_REGEXP = /OKOK/i, | ||
REGEXP_REMOTE = /RemoteWebDriver instances should connect to: (.*)/, | ||
REGEXP_STARTED = /Started org\.openqa\.jetty\.jetty\.Server/, | ||
REGEXP_RUNNING = /Selenium is already running/, | ||
REGEXP_FAILURE = /Failed to start/, | ||
REGEXP_SESSION_DELETE = /Executing: \[delete session: (.*)\]/, | ||
REGEXP_SESSION_NEW = /Executing: \[new session: (.*)\]/, | ||
REGEXP_CAPABILITIES = /Capabilities \[\{(.*)\}\]/, | ||
REGEXP_EXCEPTION = /Exception thrown(.*)/m, | ||
REGEXP_FATAL = /Fatal error/, | ||
REGEXP_SHUTDOWN_OK = /OKOK/i, | ||
DEFAULT_CMD = 'webdriver-manager start', | ||
@@ -50,7 +51,11 @@ DEFAULT_INSTANCE = 'http://localhost:4444'; | ||
} | ||
return spawn(file, args, opts); | ||
} | ||
var proc = spawn(file, args, opts); | ||
proc.stdout.setEncoding('utf8'); | ||
proc.stderr.setEncoding('utf8'); | ||
return proc; | ||
function extract(regexp, value, idx) { | ||
var result; | ||
if (regexp.test(value) && (result = regexp.exec(value)) && typeof result[idx] === 'string') { | ||
return result[idx].trim(); | ||
} | ||
return ''; | ||
} | ||
@@ -67,3 +72,4 @@ | ||
sessions = 0, // Running sessions | ||
status = [false, false]; // [0 = Stopping, 1 = Stopped] | ||
status = [false, false], // [0 = Stopping, 1 = Stopped] | ||
stopCallbacks = []; | ||
@@ -77,2 +83,4 @@ function start() { | ||
selenium.stdout.setEncoding('utf8'); | ||
selenium.stderr.setEncoding('utf8'); | ||
selenium.stdout.on('data', data); | ||
@@ -94,2 +102,3 @@ selenium.stderr.on('data', data); | ||
if (status[0] || status[1]) { | ||
stopCallbacks.push(callback); | ||
return; | ||
@@ -107,5 +116,9 @@ } | ||
if (callback) { | ||
var success = status[1] = SHUTDOWN_OK_REGEXP.test(response); | ||
var success = status[1] = REGEXP_SHUTDOWN_OK.test(response); | ||
stopCallbacks.push(callback); | ||
grunt.log.writeln('Shut down'.cyan + ' Selenium server: ' + server + ' (' + (success ? response.green : response.red) + ')'); | ||
callback(success); | ||
stopCallbacks.forEach(function (cb) { | ||
cb(success); | ||
}); | ||
stopCallbacks = []; | ||
} | ||
@@ -135,12 +148,10 @@ }); | ||
grunt.verbose.writeln('>> '.red + out); | ||
var lines; | ||
if (REMOTE_REGEXP.test(out)) { | ||
var result = REMOTE_REGEXP.exec(out); | ||
if (typeof result[1] === 'string' && result[1].trim().length > 0) { | ||
server = result[1].replace(/\/wd\/hub/, ''); | ||
} | ||
} else if (STARTED_REGEXP.test(out)) { | ||
if (REGEXP_REMOTE.test(out)) { | ||
server = extract(REGEXP_REMOTE, out, 1) || server; | ||
} else if (REGEXP_STARTED.test(out)) { | ||
// Success | ||
started(done); | ||
} else if (RUNNING_REGEXP.test(out)) { | ||
} else if (REGEXP_RUNNING.test(out)) { | ||
if (failureTimeout) { | ||
@@ -160,22 +171,46 @@ clearTimeout(failureTimeout); | ||
}); | ||
} else if (FAILURE_REGEXP.test(out)) { | ||
} else if (REGEXP_FAILURE.test(out)) { | ||
// Failure -> Exit after timeout. The timeout is needed to | ||
// enable further console sniffing as the output needed to | ||
// match `RUNNING_REGEXP` is coming behind the failure message. | ||
// match `REGEXP_RUNNING` is coming behind the failure message. | ||
failureTimeout = setTimeout(destroy, 500); | ||
} else if (EXCEPTION_REGEXP.test(out)) { | ||
} else if (REGEXP_EXCEPTION.test(out)) { | ||
// Failure -> Exit | ||
grunt.log.writeln('Exception thrown.'.red + ' Going to shut down the Selenium server.'); | ||
grunt.log.writeln('Exception thrown:'.red + ' Going to shut down the Selenium server'); | ||
stackTrace = out; | ||
destroy(); | ||
} else if (FATAL_REGEXP.test(out)) { | ||
} else if (REGEXP_FATAL.test(out)) { | ||
// Failure -> Exit | ||
destroy(); | ||
} else if (SESSION_NEW_REGEXP.test(out)) { | ||
sessions++; | ||
} else if (SESSION_DELETE_REGEXP.test(out)) { | ||
// Done -> Exit | ||
if (--sessions <= 0) { | ||
destroy(noop); | ||
} | ||
} else if (REGEXP_SESSION_NEW.test(out)) { | ||
// As there might be race-conditions with multiple logs for | ||
// `REGEXP_SESSION_NEW` in one log statement, we have to parse | ||
// parse the data | ||
lines = out.split(/[\n\r]/); | ||
lines.forEach(function (line) { | ||
if (REGEXP_SESSION_NEW.test(line)) { | ||
sessions++; | ||
var caps = extract(REGEXP_CAPABILITIES, line, 1); | ||
grunt.log.writeln('Session created: '.cyan + caps); | ||
} | ||
}); | ||
} else if (REGEXP_SESSION_DELETE.test(out)) { | ||
// As there might be race-conditions with multiple logs for | ||
// `REGEXP_SESSION_NEW` in one log statement, we have to parse | ||
// parse the data | ||
lines = out.split(/[\n\r]/); | ||
lines.forEach(function (line) { | ||
if (REGEXP_SESSION_DELETE.test(line)) { | ||
sessions--; | ||
var msg = 'Session deleted: '.cyan; | ||
if (sessions <= 0) { | ||
// Done -> Exit | ||
grunt.log.writeln(msg + 'Going to shut down the Selenium server'); | ||
destroy(noop); | ||
} else { | ||
grunt.log.writeln(msg + sessions + ' session(s) left'); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -182,0 +217,0 @@ } |
13417
12.24%243
16.27%89
1.14%