UNPKG

hae-uitest

Version:

A UI Test Framework for Huawei HAE

166 lines (151 loc) 4.57 kB
//selenium-server var fs = require('fs'); var path = require('path'); var child_process = require('child_process'); var config = require('../config'); var seleniumPath = path.normalize(__dirname + '/../server/selenium-server-standalone.jar'); var seleniumHubPath = 'http://' + config.server +':' + config.port + '/grid/register'; var ieDriverPath = path.normalize(__dirname + '/../webdriver/IEDriverServer.exe'); var chromeDriverPath = path.normalize(__dirname + '/../webdriver/chromedriver.exe'); var serviceMode = { "standalone": { "startCmd": ['-port', config.port], "statusCmd": "netstat -aon|findstr TCP.*0.0.0.0:" + config.port + ".*LISTENING" }, "hub": { "startCmd": ['-role', 'hub', '-port', config.port], "statusCmd": "netstat -aon|findstr TCP.*0.0.0.0:" + config.port + ".*LISTENING" }, "node": { "startCmd": ['-role', 'node', '-hub', seleniumHubPath], "statusCmd": "netstat -aon|findstr TCP.*:.*" + config.server + ":" + config.port + ".*ESTABLISHED" } }; var execOptions = { encoding: 'utf8', timeout: 5 * 1000, maxBuffer: 200*1024, killSignal: 'SIGTERM', cwd: null, env: null } module.exports = { //初始化 init: function(program, callback) { var self = this; //当用户指定启动模式 if (program.mode) { //检查模式是否合法 if (!(program.mode in serviceMode)) { throw new Error('invalid selenium-server starting mode.'); } //先停止再启动 self.getStatus(program.mode, function(err, pid) { if (pid) { self.stop(pid, function(err) { if (err) throw err; self.start(program.mode, function(err, mode) { callback(err, mode); }); }); } else { self.start(program.mode, function(err, mode) { callback(err, mode); }); } }); } else { //未指定启动模式时 服务未启动则默认启用 standalone 模式 var serviceCmd = "netstat -aon|findstr TCP.*:" + config.port; var child = child_process.exec(serviceCmd, execOptions, function (err, stdout, stderr) { if (stdout) { callback(null, 'unknown'); } else { //先停止再启动 standalone 模式 self.getStatus('standalone', function(err, pid) { if (pid) { self.stop(pid, function(err) { if (err) throw err; self.start('standalone', function(err, mode) { callback(err, 'standalone'); }); }); } else { self.start('standalone', function(err, mode) { callback(err, 'standalone'); }); } }); } }); child.on('exit', function() { if ('SIGTERM' === child.signalCode) { throw new Error('failed to check the service.'); } }); } }, //检查服务状态 getStatus: function(mode, callback) { var pid; var child = child_process.exec(serviceMode[mode].statusCmd, execOptions, function (err, stdout, stderr) { if (stdout) { pid = stdout.match(/\d{2,}/g); pid = pid ? pid[pid.length - 1] : null; } callback(err, pid); }); child.on('exit', function() { if ('SIGTERM' === child.signalCode) { throw new Error('check the service status timeout.'); } }); }, //启动 selenium-server start: function(mode, callback) { var self = this; var timeout = 7 * 1000; //启动服务超时时长 var cmdStartService = ['-jar', '-Dwebdriver.ie.driver=' + ieDriverPath, '-Dwebdriver.chrome.driver=' + chromeDriverPath, seleniumPath].concat(serviceMode[mode].startCmd); var child = child_process.spawn('java', cmdStartService, { detached: true, stdio: 'ignore' }); var checkStatus = function() { timeout -= 1000; setTimeout(function() { self.getStatus(mode, function(err, pid) { if (pid) { callback(null, mode); } else { if (0 < timeout) { checkStatus(); } else { callback(new Error('failed to start the selenium-server.'), null); } } }); }, 1000); } checkStatus(); }, //停止服务 stop: function(pid, callback) { var pidReg = /\d+/g; if (pidReg.test(pid)) { var cmdTaskkill = 'taskkill /f /pid ' + pid; var child = child_process.exec(cmdTaskkill, execOptions, function (err, stdout, stderr) { callback(err); }); child.on('exit', function() { if ('SIGTERM' === child.signalCode) { throw new Error('failed to kill the service.'); } }); } else { callback(null); } }, }