appium-helios-driver
Version:
Appium bridge to AppiumForHelios
95 lines (80 loc) • 2.6 kB
JavaScript
import { JWProxy } from 'mx-appium-base-driver';
import log from './logger';
import { SubProcess } from 'teen_process';
import { fs, logger, process } from 'appium-support';
import path from 'path';
// Route back to the Appium Server -- We want to send these messages through the websocket
const DEFAULT_A4H_HOST = '127.0.0.1';
const DEFAULT_A4H_PORT = 4723;
const a4hLog = logger.getLogger('Appium4Helios');
class AppiumForHelios {
constructor () {
this.proxyHost = DEFAULT_A4H_HOST;
this.proxyPort = DEFAULT_A4H_PORT;
this.proc = null;
this.jwproxy = new JWProxy({server: this.proxyHost, port: this.proxyPort, base: "/helios-command"});
}
sessionId () {
if (this.state !== AppiumForHelios.STATE_ONLINE) {
return null;
}
return this.jwproxy.sessionId;
}
async waitForOnline () {
// TODO: Actually check
return true;
}
async getStatus () {
return await this.sendCommand('/status', 'GET');
}
async startSession (caps) {
this.proxyReqRes = this.jwproxy.proxyReqRes.bind(this.jwproxy);
await this.sendCommand('/session', 'POST', {desiredCapabilities: caps});
}
async stop () {
try {
if (this.proc) {
await this.proc.stop();
}
} catch (e) {
log.error(e);
}
}
async sendCommand (url, method, body) {
let res;
// need to cover over A4H's bad handling of responses, which sometimes
// don't have 'value' properties
try {
res = await this.jwproxy.command(url, method, body);
} catch (e) {
if (e.message.indexOf("Did not get a valid response object") === -1 ||
e.message.indexOf("value") !== -1) {
throw e;
}
}
return res;
}
async proxyReq (req, res) {
return await this.jwproxy.proxyReqRes(req, res);
}
async killAll () {
const processName = "AppiumForHelios";
// js hint cannot handle backticks, even escaped, within template literals
log.info(`Killing any old AppiumForHelios`);
await process.killProcess(processName);
log.info("Successfully cleaned up old Appium4Helios servers");
}
async deleteSession () {
log.debug('Deleting AppiumForHelios server session');
// rely on jwproxy's intelligence to know what we're talking about and
// delete the current session
try {
await this.sendCommand('/', 'DELETE');
} catch (err) {
log.warn(`Did not get confirmation AppiumForHelios deleteSession worked; ` +
`Error was: ${err}`);
}
}
}
export { AppiumForHelios, DEFAULT_A4H_HOST, DEFAULT_A4H_PORT};
export default AppiumForHelios;