class Config {
static osArch() {
return Config.osArch_;
}
static osType() {
return Config.osType_;
}
static noProxy() {
return Config.noProxy_;
}
static httpProxy() {
return Config.httpProxy_;
}
static httpsProxy() {
return Config.httpsProxy_;
}
static getConfigFile_() {
return path.resolve(Config.dir, '..', Config.configFile);
}
static getPackageFile_() {
return path.resolve(Config.dir, '..', Config.packageFile);
}
static getSeleniumDir() {
return path.resolve(Config.dir, '..', '..', 'selenium/');
}
static getBaseDir() {
return path.resolve(Config.dir, '..', '..');
}
/**
* Get the binary versions from the configuration file.
* @returns A map of the versions defined in the configuration file.
*/
static binaryVersions() {
let configFile = require(Config.getConfigFile_());
let configVersions = {};
configVersions.selenium = configFile.webdriverVersions.selenium;
configVersions.chrome = configFile.webdriverVersions.chromedriver;
configVersions.gecko = configFile.webdriverVersions.geckodriver;
configVersions.ie = configFile.webdriverVersions.iedriver;
configVersions.android = configFile.webdriverVersions.androidsdk;
configVersions.appium = configFile.webdriverVersions.appium;
return configVersions;
}
/**
* Get the CDN urls from the configuration file.
* @returns A map of the CDN versions defined in the configuration file.
*/
static cdnUrls() {
let configFile = require(Config.getConfigFile_());
let configCdnUrls = {};
configCdnUrls.selenium = configFile.cdnUrls.selenium;
configCdnUrls.chrome = configFile.cdnUrls.chromedriver;
configCdnUrls.gecko = configFile.cdnUrls.geckodriver;
configCdnUrls.ie = configFile.cdnUrls.iedriver;
configCdnUrls.android = configFile.cdnUrls.androidsdk;
return configCdnUrls;
}
/**
* Get the package version.
*/
static getVersion() {
let packageFile = require(Config.getPackageFile_());
return packageFile.version;
}
}n/a
class HttpUtils {
static initOptions(url, timeout) {
let options = {
url: url,
// default Linux can be anywhere from 20-120 seconds
// increasing this arbitrarily to 4 minutes
timeout: 240000
};
return options;
}
static optionsSSL(options, opt_ignoreSSL) {
if (opt_ignoreSSL) {
logger.info('ignoring SSL certificate');
options.strictSSL = !opt_ignoreSSL;
options.rejectUnauthorized = !opt_ignoreSSL;
}
return options;
}
static optionsProxy(options, requestUrl, opt_proxy) {
if (opt_proxy) {
options.proxy = HttpUtils.resolveProxy(requestUrl, opt_proxy);
if (url.parse(requestUrl).protocol === 'https:') {
options.url = requestUrl.replace('https:', 'http:');
}
}
return options;
}
static optionsHeader(options, key, value) {
if (options.headers == null) {
options.headers = {};
}
options.headers[key] = value;
return options;
}
/**
* Resolves proxy based on values set
* @param fileUrl The url to download the file.
* @param opt_proxy The proxy to connect to to download files.
* @return Either undefined or the proxy.
*/
static resolveProxy(fileUrl, opt_proxy) {
let protocol = url.parse(fileUrl).protocol;
let hostname = url.parse(fileUrl).hostname;
if (opt_proxy) {
return opt_proxy;
}
else {
// If the NO_PROXY environment variable exists and matches the host name,
// to ignore the resolve proxy.
// the checks to see if it exists and equal to empty string is to help with testing
let noProxy = config_1.Config.noProxy();
if (noProxy) {
// array of hostnames/domain names listed in the NO_PROXY environment variable
let noProxyTokens = noProxy.split(',');
// check if the fileUrl hostname part does not end with one of the
// NO_PROXY environment variable's hostnames/domain names
for (let noProxyToken of noProxyTokens) {
if (hostname.indexOf(noProxyToken) !== -1) {
return undefined;
}
}
}
// If the HTTPS_PROXY and HTTP_PROXY environment variable is set, use that as the proxy
if (protocol === 'https:') {
return config_1.Config.httpsProxy() || config_1.Config.httpProxy();
}
else if (protocol === 'http:') {
return config_1.Config.httpProxy();
}
}
return undefined;
}
}n/a
function adb(sdkPath, port, command, timeout, args) {
return new Promise((resolve, reject) => {
let child = exports.spawn(path.resolve(sdkPath, 'platform-tools', 'adb'), ['-s', 'emulator-' + port, command].concat(args
|| []), 'pipe');
let done = false;
let buffer = [];
child.stdout.on('data', buffer.push.bind(buffer));
child.on('error', (err) => {
if (!done) {
done = true;
reject(err);
}
});
child.on('exit', (code, signal) => {
if (!done) {
done = true;
if (code === 0) {
resolve(buffer.join(''));
}
else {
reject({
code: code,
message: 'abd command "' + command + '" ' +
(signal ? 'received signal ' + signal : 'returned with a non-zero exit code') +
'for emulator-' + port
});
}
}
});
if (timeout) {
setTimeout(() => {
if (!done) {
done = true;
child.kill();
reject({
code: 'TIMEOUT',
message: 'adb command "' + command + '" timed out for emulator-' + port
});
}
}, timeout);
}
});
}n/a
function request(method, port, path, timeout, data) {
let headers = {};
let hasContent = data && ((method == 'POST') || (method == 'PUT'));
if (hasContent) {
data = data ? JSON.stringify(data) : '';
headers['Content-Length'] = data.length;
headers['Content-Type'] = 'application/json;charset=UTF-8';
}
return new Promise((resolve, reject) => {
let unexpectedEnd = () => {
reject({ code: 'UNKNOWN', message: 'Request ended unexpectedly' });
};
let req = http.request({ port: parseInt(port), method: method, path: path, headers: headers }, (res) => {
req.removeListener('end', unexpectedEnd);
if (res.statusCode !== 200) {
reject({ code: res.statusCode, message: res.statusMessage });
}
else {
let buffer = [];
res.on('data', buffer.push.bind(buffer));
res.on('end', () => {
resolve(buffer.join('').replace(/\0/g, ''));
});
}
});
if (timeout) {
req.setTimeout(timeout, () => {
reject({ code: 'TIMEOUT', message: 'Request timed out' });
});
}
req.on('error', reject);
req.on('end', unexpectedEnd);
if (hasContent) {
req.write(data);
}
req.end();
});
}...
headers['Content-Length'] = data.length;
headers['Content-Type'] = 'application/json;charset=UTF-8';
}
return new Promise((resolve, reject) => {
let unexpectedEnd = () => {
reject({ code: 'UNKNOWN', message: 'Request ended unexpectedly' });
};
let req = http.request({ port: parseInt(port), method: method, path: path, headers
: headers }, (res) => {
req.removeListener('end', unexpectedEnd);
if (res.statusCode !== 200) {
reject({ code: res.statusCode, message: res.statusMessage });
}
else {
let buffer = [];
res.on('data', buffer.push.bind(buffer));
...(cmd, args, stdio, opts) => {
if ((config_1.Config.osType() === 'Windows_NT') && (cmd.slice(-4) !== '.exe')) {
if (fs.existsSync(cmd + '.exe')) {
cmd += '.exe';
}
else {
args = ['/c'].concat([cmd], args);
cmd = 'cmd';
}
}
if (stdio) {
opts = opts || {};
opts.stdio = stdio;
}
if (sync) {
return child_process.spawnSync(cmd, args, opts);
}
else {
return child_process.spawn(cmd, args, opts);
}
}...
opts = opts || {};
opts.stdio = stdio;
}
if (sync) {
return child_process.spawnSync(cmd, args, opts);
}
else {
return child_process.spawn(cmd, args, opts);
}
};
}
exports.spawn = spawnFactory(false);
exports.spawnSync = spawnFactory(true);
function request(method, port, path, timeout, data) {
let headers = {};
...(cmd, args, stdio, opts) => {
if ((config_1.Config.osType() === 'Windows_NT') && (cmd.slice(-4) !== '.exe')) {
if (fs.existsSync(cmd + '.exe')) {
cmd += '.exe';
}
else {
args = ['/c'].concat([cmd], args);
cmd = 'cmd';
}
}
if (stdio) {
opts = opts || {};
opts.stdio = stdio;
}
if (sync) {
return child_process.spawnSync(cmd, args, opts);
}
else {
return child_process.spawn(cmd, args, opts);
}
}...
}
}
if (stdio) {
opts = opts || {};
opts.stdio = stdio;
}
if (sync) {
return child_process.spawnSync(cmd, args, opts);
}
else {
return child_process.spawn(cmd, args, opts);
}
};
}
exports.spawn = spawnFactory(false);
...