siesta-lite
Version:
Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers
105 lines (73 loc) • 3.45 kB
JavaScript
/*
Siesta 5.6.1
Copyright(c) 2009-2022 Bryntum AB
https://bryntum.com/contact
https://bryntum.com/products/siesta/license
*/
Role('Siesta.Launcher.Role.CanLaunchSimulatorServer', {
requires : [
'debug',
'getOptions'
],
has : {
simulatorServerClass : null,
wsServer : null
},
methods : {
setupNativeEventsSimulator : function (simServerClass) {
var options = this.getOptions()
var ConnectionServer = require("../src/nodejs/channel/websocket/ConnectionServer").ConnectionServer
var LogLevel = require("../src/generic/util/role/CanLog").LogLevel
var simulatorServerClass = simServerClass || this.simulatorServerClass || require("../src/nodejs/simulator/robotjs/Server").SimulatorServerRobotJs
var server = this.wsServer = new ConnectionServer({
serverClass : simulatorServerClass,
logLevel : options[ 'log-level' ] ? LogLevel[ options[ 'log-level' ] ] : (options.debug ? LogLevel.debug : LogLevel.log)
})
var me = this
me.debug("Starting native events server")
return server.start().then(function () {
me.debug("Native events server has started on port " + server.port + ", awaiting for connections")
})
},
// promised method
setupNativeEventsSimulatorInSeparateProcess : function (displayNum) {
var me = this
return new Promise(function (resolve, reject) {
var child_process = require('child_process');
me.debug("Starting native events server")
// setup a DISPLAY env variable for simulator, since we don't initialize robotjs simulator
// if its not set
var options = displayNum ? { env : { DISPLAY : ':' + displayNum + '.0' } } : {}
var childProcess = child_process.spawn(
process.execPath,
[ me.launcher.binDir.replace(/\/$/, '') + '/simulator.js', 'auto', JSON.stringify({ displayNumber : displayNum }) ],
options
)
var readLineInterface = require('readline').createInterface({ input : childProcess.stdout })
readLineInterface.on('line', function (data) {
var match = /port (\d+)/.exec(data)
if (match) {
me.debug("Native events server has started on port: " + match[ 1 ])
me.wsServer.port = Number(match[ 1 ])
readLineInterface.close()
resolve()
}
});
childProcess.on('error', reject)
childProcess.on('exit', reject)
me.wsServer = {
stop : function () {
var handle = setTimeout(function () {
childProcess.kill('SIGKILL')
}, 3000)
childProcess.on('exit', function () {
clearTimeout(handle)
})
childProcess.kill()
return Promise.resolve()
}
}
})
}
}
})