tia
Version:
Time is All (logs driven test engine with ExtJs support)
141 lines • 4.18 kB
JavaScript
;
/* globals gIn: true, gT */
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
// TODO: Move to engine?
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
function getPidPath() {
return path.join(gT.rootResultsDir, gT.engineConsts.remoteChromeDriverPid);
}
function savePid(pid) {
fs.writeFileSync(getPidPath(), pid, 'utf8');
}
function removePid() {
gIn.fileUtils.safeUnlink(getPidPath());
}
function getPid() {
try {
return Number(fs.readFileSync(getPidPath(), 'utf8'));
}
catch (e) {
return null;
}
}
function getSidPath() {
return path.join(gT.rootResultsDir, gT.engineConsts.remoteChromeDriverSid);
}
function saveSid(sid) {
fs.writeFileSync(getSidPath(), sid, 'utf8');
}
exports.saveSid = saveSid;
function removeSid() {
gIn.fileUtils.safeUnlink(getSidPath());
}
exports.removeSid = removeSid;
function getSid() {
try {
return fs.readFileSync(getSidPath(), 'utf8');
}
catch (e) {
return null;
}
}
exports.getSid = getSid;
const ALREADY_STARTED = 'Remote driver is already started';
const SHOULD_BE_ONLINE = 'Remote driver should be online';
async function start() {
if (getPid()) {
gIn.tracer.msg3(ALREADY_STARTED);
return ALREADY_STARTED;
}
return new Promise(resolve => {
gIn.tracer.msg3('Starting remote driver');
const data = {
chromeDriverPath: gIn.chromeDriverPath,
pidPath: getPidPath(),
port: gT.globalConfig.remoteDriverPort,
waitAfterStart: gT.engineConsts.remoteDriverStartDelay,
};
try {
const child = child_process_1.spawn(`${process.execPath}`, [`${__dirname}/remote-driver-utils-inner.js`], {
stdio: ['ipc'],
});
child.send(data);
child.on('message', msg => {
gIn.tracer.msg3(`Message from child proc: ${msg}`);
gIn.tracer.msg3(SHOULD_BE_ONLINE);
resolve(SHOULD_BE_ONLINE);
});
}
catch (e) {
console.log(e);
}
});
}
exports.start = start;
// TODO: The same problem still here.
// https://stackoverflow.com/questions/38207590/nodejs-detached-child-process-killed-when-parent-process-is-killed
/**
* starts remote chromedriver
* @returns {Promise}
*/
// export async function startOld() {
// if (getPid()) {
// gIn.tracer.msg3(ALREADY_STARTED);
// return ALREADY_STARTED;
// }
//
// return new Promise((resolve) => {
// // http://stackoverflow.com/questions/37427360/parent-process-kills-child-process-even-though-detached-is-set-to-true
// // https://github.com/nodejs/node/issues/7269#issuecomment-225698625
//
// const child = spawn(gIn.chromeDriverPath, [`--port=${gT.globalConfig.remoteDriverPort}`], {
// detached: true,
// stdio: ['ignore', 'ignore', 'ignore'],
// });
//
// savePid(child.pid);
//
// setTimeout(() => {
// child.unref();
// // child.disconnect();
//
// // TODO: may be check remote chromedriver somehow ? instead o sleep.
// gIn.tracer.msg3(SHOULD_BE_ONLINE);
// resolve(SHOULD_BE_ONLINE);
// }, gT.engineConsts.remoteDriverStartDelay);
//
// // child.stdout.on('data', (data) => {
// // gIn.tracer.trace2('Output from chromedriver: ' + data);
// // });
// });
// };
/**
* Stops remote chromedriver.
*/
function stop() {
const pid = getPid();
if (!pid) {
gIn.tracer.msg3('No remote driver to stop');
return;
}
gIn.tracer.msg3('Stopping remote driver');
try {
process.kill(pid);
}
catch (e) {
console.error(e);
}
removePid();
removeSid();
}
exports.stop = stop;
//# sourceMappingURL=remote-driver-utils.js.map