@testim/testim-cli
Version:
Command line interface for running Testing on you CI
83 lines (74 loc) • 2.76 kB
JavaScript
var webdriverio = require('webdriverio');
var fs = require('fs');
var Promise = require('bluebird');
var LOG_LEVEL_SILENT = 'silent';
//var LOG_LEVEL_VERBOSE = 'verbose';
var TestimSeleniumDriver = function(browserOptions){
this.keepAliveTimer = null;
this.seleniumOptions = {
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
}
},
host: browserOptions.host,
port: browserOptions.port,
path: '/wd/hub',
logLevel: LOG_LEVEL_SILENT
};
if(browserOptions.sauceLab && browserOptions.sauceLab.user && browserOptions.sauceLab.key){
this.seleniumOptions.user = browserOptions.sauceLab.user;
this.seleniumOptions.key = browserOptions.sauceLab.key;
}
var chromeOptions = this.seleniumOptions.desiredCapabilities.chromeOptions;
if (browserOptions.ext) {
var ext = typeof(browserOptions.ext) === 'string' ? browserOptions.ext : (__dirname + '/..');
chromeOptions.args = ['--load-extension=' + ext];
} else {
var crxContent = fs.readFileSync(__dirname + "/resource/testim-editor.zip", { encoding: 'base64'});
chromeOptions.extensions = [crxContent];
}
};
TestimSeleniumDriver.prototype.init = function(){
this.clientDriver = webdriverio.remote(this.seleniumOptions);
var that = this;
return new Promise(function(resolve, reject) {
that.clientDriver.init()
.then(function() {
// a small delay for the browser to open properly
return Promise.delay(1500).then(function() {
resolve(that);
});
})
.catch(function() {
return reject();
});
});
};
TestimSeleniumDriver.prototype.start = function(executionRunUrl){
var clientDriver = this.clientDriver;
var keepAlive = function () {
return clientDriver.execute('return window.getTestimStatus && window.getTestimStatus()')
.catch(function(err){
if(err.message === 'unexpected alert open'){
clientDriver.alertAccept().catch(function(err){
clearInterval(this.keepAliveTimer);
//reject(err);
}.bind(this));
}
}.bind(this));
};
return clientDriver
.url(executionRunUrl)
.then(function(){
this.keepAliveTimer = setInterval(keepAlive, 5000);
}.bind(this));
};
TestimSeleniumDriver.prototype.end = function(){
clearInterval(this.keepAliveTimer);
this.clientDriver.end()
.catch(function() {
/* !!!!SILENCE!!!! */
});
};
module.exports = TestimSeleniumDriver;