@testim/testim-cli
Version:
Command line interface for running Testing on you CI
141 lines (119 loc) • 4.78 kB
JavaScript
var TestimSeleniumDriver = require('./testimSeleniumDriver.js');
var Promise = require('bluebird');
var retry = require('bluebird-retry');
var TEST_START_TIMEOUT = (60 * 1000);
var TEST_RUN_TIMEOUT = parseInt(process.env.TESTIM_TEST_RUN_TIMEOUT) || (10 * 60 * 1000);
var GET_BROWSER_TIMEOUT = parseInt(process.env.TESTIM_GET_BROWSER_TIMEOUT) || (60 * 1000);
var ordinal=1;
function buildFailureResult(testId, testName, err){
var result = {};
result[testId] = {
"testResults" : {
"failureReason" : err,
"name" : testName,
"resultId" : null,
"success" : false
}
};
return result;
}
var Worker = function(executionQueue){
this.id = ordinal++;
this.executionQueue = executionQueue;
};
Worker.prototype.start = function(baseUrl, browserOptions, onStart, onResult){
this.baseUrl = baseUrl;
this.browserOptions = browserOptions;
this.onStart = onStart;
this.onResult = onResult;
return this.run();
};
Worker.prototype.runWithRetries = function(task, times, interval) {
times = times || 3;
interval = interval || 3000;
return retry(task, {max_tries: times, interval: interval});
};
Worker.prototype.getBrowserOnce = function (options) {
var browser = new TestimSeleniumDriver(options);
console.log("W:" + this.id, "Get Browser", this.testName);
return browser.init().timeout(GET_BROWSER_TIMEOUT, "Get Browser timeout");
};
Worker.prototype.getBrowser = function(options){
return this.runWithRetries(this.getBrowserOnce.bind(this, options));
};
Worker.prototype.runTestOnce = function(testRunHandler, browser){
var url = testRunHandler.getTestUrl(this.baseUrl);
var that = this;
return new Promise(function(resolve, reject) {
browser.start(url)
.then(function () {
console.log("W:" + that.id, "Wait for test start");
return testRunHandler.onStarted().timeout(TEST_START_TIMEOUT, "Test Start timeout")
.then(function () {
console.log("W:" + that.id, "Wait for test complete");
return testRunHandler.onCompleted().timeout(TEST_RUN_TIMEOUT, "Test Complete timeout")
.then(function(result) {
return resolve(result);
})
.catch(function (err) {
// complete time out
return reject(new Error(err));
});
}).catch(function (err) {
// start time out
return reject(new Error(err));
});
});
});
};
Worker.prototype.getBrowserAndRunOnce = function(testRunHandler) {
var that = this;
/* make sure test results from previous tries are cleared */
testRunHandler.clearTestResult();
return new Promise(function(resolve, reject) {
return that.getBrowserOnce(that.browserOptions)
.then(function(browser) {
return that.runTestOnce(testRunHandler, browser)
.then(function(result) {
return resolve(result);
})
.catch(function(err) {
return reject(err);
})
.finally(function() {
browser.end();
});
})
.catch(function(err) {
return reject(err);
});
});
};
Worker.prototype.runTest = function(testRunHandler){
this.onStart(this.id, testRunHandler.getTestId());
return this.runWithRetries(this.getBrowserAndRunOnce.bind(this, testRunHandler));
};
Worker.prototype.run = function(){
var that = this;
function onRunComplete(result) {
if (that.onResult) {
that.onResult(that.id, result);
}
Promise.delay(1000).then(function() {
process.nextTick(that.run.bind(that));
});
}
this.executionQueue.getNext()
.then(function(testRunHandler) {
that.testId = testRunHandler.getTestId();
that.testName = testRunHandler.getTestName();
return that.runTest(testRunHandler);
})
.then(onRunComplete)
.catch(function(err) {
var isError = err instanceof Error;
var result = buildFailureResult(that.testId, that.testName, isError ? err.message : err);
onRunComplete(result);
});
};
module.exports = Worker;