Skip to content

Commit

Permalink
updated selenium server unit tests and webdriver startup settings
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Aug 31, 2019
1 parent 0ddb8e5 commit 7c34929
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 78 deletions.
92 changes: 43 additions & 49 deletions lib/runner/wd-instances/base-wd-server.js
Expand Up @@ -57,46 +57,13 @@ class BaseWDServer {
/**
* @return {number}
*/
static get defaultGlobalStartTimeout() {
return 120000;
}

constructor(settings) {
this.settings = settings;
this.statusPingTries = 0;
this.statusCheckEndpoint = BaseWDServer.STATUS_ENDPOINT;
this.sessionsEndpoint = BaseWDServer.SESSIONS_ENDPOINT;
this.singleSessionEndpoint = BaseWDServer.SINGLE_SESSION_ENDPOINT;
this.process = null;
this.output = '';
this.error_out = '';
this.cliArgs = [];
this.resolved = false;
this.exited = false;
this.pollStarted = false;
this.processCreatedTimeout = null;
this.processStatusCheckTimeout = null;

this.settings.port = String(this.settings.port || this.defaultPort);
this.settings.host = this.settings.host || BaseWDServer.DEFAULT_HOST;

if (!this.settings.server_path) {
throw this.getStartupErrorMessage(this.errorMessages.binaryMissing);
}

this.promiseStarted = {
resolve: null,
reject: null
};

process.on('exit', () => {
this.stop();
});
get processCreatedTimeoutMs() {
return this.settings.process_create_timeout || 120000;
}

/**
* Maximum number of ping status check attempts before returning an error
*
*
* @return {number}
*/
get maxStatusPollTries() {
Expand All @@ -105,7 +72,7 @@ class BaseWDServer {

/**
* Interval (in ms) to use between 2 ping status checks
*
*
* @return {number}
*/
get statusPollInterval() {
Expand Down Expand Up @@ -141,6 +108,37 @@ class BaseWDServer {
}, []).join('\n ');
}

constructor(settings) {
this.settings = settings;
this.statusPingTries = 0;
this.statusCheckEndpoint = BaseWDServer.STATUS_ENDPOINT;
this.sessionsEndpoint = BaseWDServer.SESSIONS_ENDPOINT;
this.singleSessionEndpoint = BaseWDServer.SINGLE_SESSION_ENDPOINT;
this.process = null;
this.output = '';
this.error_out = '';
this.cliArgs = [];
this.processCreated = false;
this.exited = false;
this.pollStarted = false;
this.processCreatedTimeout = null;
this.processStatusCheckTimeout = null;

this.settings.port = String(this.settings.port || this.defaultPort);
this.settings.host = this.settings.host || BaseWDServer.DEFAULT_HOST;

if (!this.settings.server_path) {
throw this.getStartupErrorMessage(this.errorMessages.binaryMissing);
}

this.promiseStarted = {
resolve: null,
reject: null
};

process.on('exit', () => this.stop());
}

setCliArgs() {
if (Array.isArray(this.settings.cli_args)) {
this.settings.cli_args.forEach(item => {
Expand All @@ -162,11 +160,11 @@ class BaseWDServer {

startProcessCreatedTimer() {
this.processCreatedTimeout = setTimeout(() => {
if (!this.resolved) {
if (!this.processCreated) {
Logger.error(`Timeout while waiting for ${this.serviceName} to start.`);
this.stop();
}
}, BaseWDServer.defaultGlobalStartTimeout);
}, this.processCreatedTimeoutMs);

this.checkProcessStarted();
}
Expand Down Expand Up @@ -219,15 +217,13 @@ class BaseWDServer {
});
});

req.on('error', (err) => {
this.checkPingStatus();
});
req.on('error', err => this.checkPingStatus());
}

checkPingStatus() {
if (this.statusPingTries < BaseWDServer.maxStatusPollTries) {
if (this.statusPingTries < this.maxStatusPollTries) {
this.statusPingTries++;
this.processStatusCheckTimeout = setTimeout(this.pingStatus.bind(this), BaseWDServer.statusPollInterval);
this.processStatusCheckTimeout = setTimeout(this.pingStatus.bind(this), this.statusPollInterval);
} else {
this.promiseStarted.reject(this.createError(`Timeout while trying to connect to ${this.serviceName} `+
`on port ${this.settings.port}.`));
Expand Down Expand Up @@ -284,9 +280,7 @@ class BaseWDServer {
console.warn('Please check that the "webdriver.server_path" config property is set correctly.\n');
}

process.nextTick(() => {
this.stop();
});
process.nextTick(() => this.stop());
}

onClose() {
Expand Down Expand Up @@ -334,7 +328,7 @@ class BaseWDServer {

this.exited = false;
this.pollStarted = false;
this.resolved = false;
this.processCreated = false;
this.startTime = new Date();

this.setCliArgs();
Expand All @@ -358,7 +352,7 @@ class BaseWDServer {
this.promiseStarted.resolve = () => {
this.process.removeListener('exit', exitHandler);

this.resolved = true;
this.processCreated = true;
if (this.processCreatedTimeout) {
clearTimeout(this.processCreatedTimeout);
}
Expand Down
10 changes: 10 additions & 0 deletions lib/settings/defaults.js
Expand Up @@ -107,8 +107,18 @@ module.exports = {
server_path: null,
log_path: '',
use_legacy_jsonwire: undefined,

// Time to wait (in ms) before starting to check the Webdriver server is up and running
check_process_delay: 100,

// Maximum number of ping status check attempts before returning a timeout error
max_status_poll_tries: 5,

// Interval (in ms) to use between status ping checks when checking if the Webdriver server is up and running
status_poll_interval: 100,

// The entire time (in ms) to wait for the Node.js process to be created and running (default is 2 min), including spawning the child process and checking the status
process_create_timeout: 120000,
host: undefined,
port: undefined,
ssl: undefined,
Expand Down
25 changes: 14 additions & 11 deletions test/src/runner/cli/testCliRunner.js
Expand Up @@ -348,6 +348,9 @@ describe('Test CLI Runner', function() {
},
setDetailedOutput() {

},
setErrorLog() {

},
disableColors() {
disableColorsCalled = true;
Expand Down Expand Up @@ -380,9 +383,9 @@ describe('Test CLI Runner', function() {

assert.deepEqual(runner.test_settings.src_folders, ['tests']);
assert.deepEqual(runner.test_settings.skipgroup, ['tobeskipped']);
assert.equal(runner.test_settings.output, false);
assert.equal(runner.test_settings.silent, false);
assert.equal(runner.test_settings.filename_filter, 'tests*.js');
assert.strictEqual(runner.test_settings.output, false);
assert.strictEqual(runner.test_settings.silent, false);
assert.strictEqual(runner.test_settings.filename_filter, 'tests*.js');
assert.ok(disableColorsCalled, 'disable colors not called');
done();
});
Expand All @@ -407,14 +410,14 @@ describe('Test CLI Runner', function() {
env: 'extra'
}).setup();

assert.equal(runner.isWebDriverManaged(), true);
assert.equal(runner.test_settings.selenium.host, 'other.host');
assert.equal(runner.test_settings.detailed_output, false);
assert.equal(runner.test_settings.output, false);
assert.equal(runner.test_settings.disable_colors, true);
assert.equal(runner.test_settings.username, 'testuser');
assert.equal(runner.test_settings.credentials.service.user, 'testuser');
assert.equal(runner.test_settings.desiredCapabilities['test.user'], 'testuser');
assert.strictEqual(runner.isWebDriverManaged(), true);
assert.strictEqual(runner.test_settings.selenium.host, 'other.host');
assert.strictEqual(runner.test_settings.detailed_output, false);
assert.strictEqual(runner.test_settings.output, false);
assert.strictEqual(runner.test_settings.disable_colors, true);
assert.strictEqual(runner.test_settings.username, 'testuser');
assert.strictEqual(runner.test_settings.credentials.service.user, 'testuser');
assert.strictEqual(runner.test_settings.desiredCapabilities['test.user'], 'testuser');
});

it('testGetTestSourceSingle', function() {
Expand Down
78 changes: 60 additions & 18 deletions test/src/wd-manager/testSelenium.js
@@ -1,9 +1,9 @@
const assert = require('assert');
const mockSpawn = require('mock-spawn');
const nock = require('nock');
const common = require('../../common.js');
const WDServer = common.require('runner/webdriver-server.js');
const SeleniumServer = common.require('runner/wd-instances/selenium-server.js');
const mockSpawn = require('mock-spawn');
const nock = require('nock');
const Settings = common.require('settings/settings.js');

describe('Webdriver Manager', function () {
Expand All @@ -14,8 +14,7 @@ describe('Webdriver Manager', function () {
this.origSpawn = require('child_process').spawn;

require('child_process').spawn = this.mockedSpawn;
require('util').print = function () {
};
require('util').print = function () {};

try {
nock.activate();
Expand All @@ -30,7 +29,6 @@ describe('Webdriver Manager', function () {

afterEach(function () {
// clean up

require('child_process').spawn = this.origSpawn;
require('util').print = this.origPrint;
});
Expand Down Expand Up @@ -64,7 +62,7 @@ describe('Webdriver Manager', function () {
it('testStartServer', function () {
this.mockedSpawn.setStrategy(function (command, args, opts) {
assert.deepEqual(opts, {stdio: ['ignore', 'pipe', 'pipe']});
wdServer.instance.resolved = true;
wdServer.instance.processCreated = true;

if (command !== 'java') {
return null;
Expand All @@ -79,7 +77,10 @@ describe('Webdriver Manager', function () {

let settings = Settings.parse({
selenium: {
check_process_delay: 100,
check_process_delay: 10,
max_status_poll_tries: 7,
status_poll_interval: 250,
process_create_timeout: 1000,
start_process: true,
server_path: './selenium.jar',
log_path: false,
Expand All @@ -105,21 +106,28 @@ describe('Webdriver Manager', function () {
4444
]);

assert.equal(wdServer.instance.process.host, undefined);
assert.equal(wdServer.instance.pollStarted, true);
assert.equal(wdServer.instance.resolved, true);
assert.equal(wdServer.instance.process.command, 'java');
assert.equal(wdServer.instance.sessionsEndpoint, '/sessions');
assert.equal(wdServer.instance.singleSessionEndpoint, '/session');
assert.equal(wdServer.instance.output, 'Started org.openqa.jetty.jetty.Server');
assert.strictEqual(wdServer.settings.selenium.max_status_poll_tries, 7);
assert.strictEqual(wdServer.settings.selenium.status_poll_interval, 250);
assert.strictEqual(wdServer.settings.selenium.check_process_delay, 10);
assert.strictEqual(wdServer.settings.selenium.process_create_timeout, 1000);
assert.strictEqual(wdServer.settings.webdriver.max_status_poll_tries, 7);
assert.strictEqual(wdServer.settings.webdriver.status_poll_interval, 250);
assert.strictEqual(wdServer.settings.webdriver.check_process_delay, 10);
assert.strictEqual(wdServer.settings.webdriver.process_create_timeout, 1000);
assert.strictEqual(wdServer.instance.process.host, undefined);
assert.strictEqual(wdServer.instance.pollStarted, true);
assert.strictEqual(wdServer.instance.processCreated, true);
assert.strictEqual(wdServer.instance.process.command, 'java');
assert.strictEqual(wdServer.instance.sessionsEndpoint, '/sessions');
assert.strictEqual(wdServer.instance.singleSessionEndpoint, '/session');
assert.strictEqual(wdServer.instance.output, 'Started org.openqa.jetty.jetty.Server');
assert.strictEqual(wdServer.instance.error_out, '');
});
});

it('testStartServerWithExitCode', function () {
this.mockedSpawn.setStrategy(function (command, args, opts) {
assert.deepEqual(opts, {stdio: ['ignore', 'pipe', 'pipe']});
wdServer.instance.resolved = true;

if (command !== 'java') {
return null;
Expand All @@ -135,8 +143,7 @@ describe('Webdriver Manager', function () {
let settings = Settings.parse({
selenium: {
check_process_delay: 1000,
max_status_poll_tries: 10,
status_poll_interval: 200,
process_create_timeout: 2000,
start_process: true,
server_path: './selenium.jar',
log_path: false,
Expand All @@ -147,10 +154,45 @@ describe('Webdriver Manager', function () {
let wdServer = new WDServer(settings);

return wdServer.start().then(_ => {
// Simulate an error thrown
assert.ok(false, 'Selenium Server should have failed to start.');
}).catch(err => {
assert.strictEqual(wdServer.settings.webdriver.max_status_poll_tries, 5);
assert.strictEqual(wdServer.settings.webdriver.status_poll_interval, 100);
assert.strictEqual(wdServer.settings.webdriver.check_process_delay, 1000);
assert.strictEqual(wdServer.settings.webdriver.process_create_timeout, 2000);
assert.ok(err.message.includes('Selenium Server process exited with code: 1'));
});
});

it('test start server with timeout', function () {
this.mockedSpawn.setStrategy(function (command, args, opts) {
if (command !== 'java') {
return null;
}
});

let settings = Settings.parse({
selenium: {
check_process_delay: 10,
start_process: true,
status_poll_interval: 10,
max_status_poll_tries: 3,
server_path: './selenium.jar',
log_path: false,
port: 1024
}
});

let wdServer = new WDServer(settings);

return wdServer.start().then(_ => {
// Simulate an error thrown
assert.ok(false, 'Selenium Server should have failed to start.');
}).catch(err => {
assert.strictEqual(wdServer.instance.statusPingTries, 3);
assert.ok(err.message.includes('Timeout while trying to connect to Selenium Server on port 1024.'));
});
});
});
});
});

0 comments on commit 7c34929

Please sign in to comment.