2016-12-31 15:21:34 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Preferences } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/Preferences.jsm"
|
|
|
|
);
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2019-07-05 12:01:24 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { assert } = ChromeUtils.import("chrome://marionette/content/assert.js");
|
|
|
|
const { InvalidArgumentError } = ChromeUtils.import(
|
|
|
|
"chrome://marionette/content/error.js"
|
|
|
|
);
|
|
|
|
const { pprint } = ChromeUtils.import("chrome://marionette/content/format.js");
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2018-06-08 15:16:29 +03:00
|
|
|
XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]);
|
|
|
|
|
2018-06-26 19:15:28 +03:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
|
|
|
"Capabilities",
|
|
|
|
"PageLoadStrategy",
|
|
|
|
"Proxy",
|
|
|
|
"Timeouts",
|
2018-06-10 15:37:19 +03:00
|
|
|
"UnhandledPromptBehavior",
|
2018-06-26 19:15:28 +03:00
|
|
|
];
|
2016-12-31 15:21:34 +03:00
|
|
|
|
|
|
|
// Enable testing this module, as Services.appinfo.* is not available
|
|
|
|
// in xpcshell tests.
|
|
|
|
const appinfo = { name: "<missing>", version: "<missing>" };
|
|
|
|
try {
|
|
|
|
appinfo.name = Services.appinfo.name.toLowerCase();
|
|
|
|
} catch (e) {}
|
|
|
|
try {
|
|
|
|
appinfo.version = Services.appinfo.version;
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
/** Representation of WebDriver session timeouts. */
|
2018-06-26 19:15:28 +03:00
|
|
|
class Timeouts {
|
2017-06-30 02:40:24 +03:00
|
|
|
constructor() {
|
2016-12-31 15:21:34 +03:00
|
|
|
// disabled
|
|
|
|
this.implicit = 0;
|
|
|
|
// five mintues
|
|
|
|
this.pageLoad = 300000;
|
|
|
|
// 30 seconds
|
|
|
|
this.script = 30000;
|
|
|
|
}
|
|
|
|
|
2018-06-26 19:15:28 +03:00
|
|
|
toString() {
|
|
|
|
return "[object Timeouts]";
|
|
|
|
}
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/** Marshals timeout durations to a JSON Object. */
|
2017-06-30 02:40:24 +03:00
|
|
|
toJSON() {
|
2016-12-31 15:21:34 +03:00
|
|
|
return {
|
2017-02-23 21:33:26 +03:00
|
|
|
implicit: this.implicit,
|
|
|
|
pageLoad: this.pageLoad,
|
|
|
|
script: this.script,
|
2016-12-31 15:21:34 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
static fromJSON(json) {
|
2018-05-31 08:17:15 +03:00
|
|
|
assert.object(
|
|
|
|
json,
|
|
|
|
pprint`Expected "timeouts" to be an object, got ${json}`
|
|
|
|
);
|
2018-06-26 19:15:28 +03:00
|
|
|
let t = new Timeouts();
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2018-05-31 08:17:15 +03:00
|
|
|
for (let [type, ms] of Object.entries(json)) {
|
|
|
|
switch (type) {
|
2016-12-31 15:21:34 +03:00
|
|
|
case "implicit":
|
2018-05-31 08:17:15 +03:00
|
|
|
t.implicit = assert.positiveInteger(
|
|
|
|
ms,
|
|
|
|
pprint`Expected ${type} to be a positive integer, got ${ms}`
|
|
|
|
);
|
2016-12-31 15:21:34 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "script":
|
2019-01-07 02:04:41 +03:00
|
|
|
if (ms !== null) {
|
|
|
|
assert.positiveInteger(
|
|
|
|
ms,
|
|
|
|
pprint`Expected ${type} to be a positive integer, got ${ms}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
t.script = ms;
|
2016-12-31 15:21:34 +03:00
|
|
|
break;
|
|
|
|
|
2017-02-23 21:33:26 +03:00
|
|
|
case "pageLoad":
|
2018-05-31 08:17:15 +03:00
|
|
|
t.pageLoad = assert.positiveInteger(
|
|
|
|
ms,
|
|
|
|
pprint`Expected ${type} to be a positive integer, got ${ms}`
|
|
|
|
);
|
2016-12-31 15:21:34 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-05-31 08:17:15 +03:00
|
|
|
throw new InvalidArgumentError("Unrecognised timeout: " + type);
|
2016-12-31 15:21:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
2018-06-26 19:15:28 +03:00
|
|
|
}
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
|
|
|
* Enum of page loading strategies.
|
|
|
|
*
|
|
|
|
* @enum
|
|
|
|
*/
|
2018-06-26 19:15:28 +03:00
|
|
|
const PageLoadStrategy = {
|
2017-07-26 15:11:53 +03:00
|
|
|
/** No page load strategy. Navigation will return immediately. */
|
2016-12-31 15:21:34 +03:00
|
|
|
None: "none",
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
|
|
|
* Eager, causing navigation to complete when the document reaches
|
|
|
|
* the <code>interactive</code> ready state.
|
|
|
|
*/
|
2016-12-31 15:21:34 +03:00
|
|
|
Eager: "eager",
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
|
|
|
* Normal, causing navigation to return when the document reaches the
|
|
|
|
* <code>complete</code> ready state.
|
|
|
|
*/
|
2016-12-31 15:21:34 +03:00
|
|
|
Normal: "normal",
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Proxy configuration object representation. */
|
2018-06-26 19:15:28 +03:00
|
|
|
class Proxy {
|
2017-07-26 15:11:53 +03:00
|
|
|
/** @class */
|
2016-12-31 15:21:34 +03:00
|
|
|
constructor() {
|
|
|
|
this.proxyType = null;
|
2017-08-18 15:49:00 +03:00
|
|
|
this.ftpProxy = null;
|
|
|
|
this.ftpProxyPort = null;
|
2016-12-31 15:21:34 +03:00
|
|
|
this.httpProxy = null;
|
|
|
|
this.httpProxyPort = null;
|
2017-08-31 18:49:36 +03:00
|
|
|
this.noProxy = null;
|
2016-12-31 15:21:34 +03:00
|
|
|
this.sslProxy = null;
|
|
|
|
this.sslProxyPort = null;
|
|
|
|
this.socksProxy = null;
|
|
|
|
this.socksProxyPort = null;
|
|
|
|
this.socksVersion = null;
|
|
|
|
this.proxyAutoconfigUrl = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets Firefox proxy settings.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
* True if proxy settings were updated as a result of calling this
|
|
|
|
* function, or false indicating that this function acted as
|
|
|
|
* a no-op.
|
|
|
|
*/
|
|
|
|
init() {
|
|
|
|
switch (this.proxyType) {
|
2017-08-15 20:23:33 +03:00
|
|
|
case "autodetect":
|
|
|
|
Preferences.set("network.proxy.type", 4);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case "direct":
|
|
|
|
Preferences.set("network.proxy.type", 0);
|
|
|
|
return true;
|
|
|
|
|
2016-12-31 15:21:34 +03:00
|
|
|
case "manual":
|
|
|
|
Preferences.set("network.proxy.type", 1);
|
2017-08-18 15:49:00 +03:00
|
|
|
|
|
|
|
if (this.ftpProxy) {
|
|
|
|
Preferences.set("network.proxy.ftp", this.ftpProxy);
|
|
|
|
if (Number.isInteger(this.ftpProxyPort)) {
|
|
|
|
Preferences.set("network.proxy.ftp_port", this.ftpProxyPort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.httpProxy) {
|
2016-12-31 15:21:34 +03:00
|
|
|
Preferences.set("network.proxy.http", this.httpProxy);
|
2017-08-18 15:49:00 +03:00
|
|
|
if (Number.isInteger(this.httpProxyPort)) {
|
|
|
|
Preferences.set("network.proxy.http_port", this.httpProxyPort);
|
|
|
|
}
|
2016-12-31 15:21:34 +03:00
|
|
|
}
|
2017-08-18 15:49:00 +03:00
|
|
|
|
|
|
|
if (this.sslProxy) {
|
2016-12-31 15:21:34 +03:00
|
|
|
Preferences.set("network.proxy.ssl", this.sslProxy);
|
2017-08-18 15:49:00 +03:00
|
|
|
if (Number.isInteger(this.sslProxyPort)) {
|
|
|
|
Preferences.set("network.proxy.ssl_port", this.sslProxyPort);
|
|
|
|
}
|
2016-12-31 15:21:34 +03:00
|
|
|
}
|
2017-08-18 15:49:00 +03:00
|
|
|
|
2016-12-31 15:21:34 +03:00
|
|
|
if (this.socksProxy) {
|
|
|
|
Preferences.set("network.proxy.socks", this.socksProxy);
|
2017-08-18 15:49:00 +03:00
|
|
|
if (Number.isInteger(this.socksProxyPort)) {
|
|
|
|
Preferences.set("network.proxy.socks_port", this.socksProxyPort);
|
|
|
|
}
|
2016-12-31 15:21:34 +03:00
|
|
|
if (this.socksVersion) {
|
|
|
|
Preferences.set("network.proxy.socks_version", this.socksVersion);
|
|
|
|
}
|
|
|
|
}
|
2017-08-31 18:49:36 +03:00
|
|
|
|
2017-09-07 11:21:36 +03:00
|
|
|
if (this.noProxy) {
|
2017-08-31 18:49:36 +03:00
|
|
|
Preferences.set(
|
|
|
|
"network.proxy.no_proxies_on",
|
|
|
|
this.noProxy.join(", ")
|
|
|
|
);
|
|
|
|
}
|
2016-12-31 15:21:34 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case "pac":
|
|
|
|
Preferences.set("network.proxy.type", 2);
|
2017-06-30 02:40:24 +03:00
|
|
|
Preferences.set(
|
|
|
|
"network.proxy.autoconfig_url",
|
|
|
|
this.proxyAutoconfigUrl
|
|
|
|
);
|
2016-12-31 15:21:34 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case "system":
|
|
|
|
Preferences.set("network.proxy.type", 5);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
|
|
|
* @param {Object.<string, ?>} json
|
|
|
|
* JSON Object to unmarshal.
|
2017-08-18 15:49:00 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
|
|
|
* When proxy configuration is invalid.
|
2017-07-26 15:11:53 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
static fromJSON(json) {
|
2017-10-09 15:10:03 +03:00
|
|
|
function stripBracketsFromIpv6Hostname(hostname) {
|
|
|
|
return hostname.includes(":")
|
|
|
|
? hostname.replace(/[\[\]]/g, "")
|
|
|
|
: hostname;
|
|
|
|
}
|
|
|
|
|
2017-08-18 15:49:00 +03:00
|
|
|
// Parse hostname and optional port from host
|
|
|
|
function fromHost(scheme, host) {
|
2017-12-05 01:32:44 +03:00
|
|
|
assert.string(
|
|
|
|
host,
|
|
|
|
pprint`Expected proxy "host" to be a string, got ${host}`
|
|
|
|
);
|
2017-08-18 15:49:00 +03:00
|
|
|
|
|
|
|
if (host.includes("://")) {
|
|
|
|
throw new InvalidArgumentError(`${host} contains a scheme`);
|
|
|
|
}
|
|
|
|
|
|
|
|
let url;
|
|
|
|
try {
|
|
|
|
// To parse the host a scheme has to be added temporarily.
|
|
|
|
// If the returned value for the port is an empty string it
|
|
|
|
// could mean no port or the default port for this scheme was
|
|
|
|
// specified. In such a case parse again with a different
|
|
|
|
// scheme to ensure we filter out the default port.
|
|
|
|
url = new URL("http://" + host);
|
|
|
|
if (url.port == "") {
|
|
|
|
url = new URL("https://" + host);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
throw new InvalidArgumentError(e.message);
|
|
|
|
}
|
|
|
|
|
2017-10-09 15:10:03 +03:00
|
|
|
let hostname = stripBracketsFromIpv6Hostname(url.hostname);
|
|
|
|
|
2017-08-18 15:49:00 +03:00
|
|
|
// If the port hasn't been set, use the default port of
|
|
|
|
// the selected scheme (except for socks which doesn't have one).
|
|
|
|
let port = parseInt(url.port);
|
|
|
|
if (!Number.isInteger(port)) {
|
|
|
|
if (scheme === "socks") {
|
|
|
|
port = null;
|
|
|
|
} else {
|
|
|
|
port = Services.io.getProtocolHandler(scheme).defaultPort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
url.username != "" ||
|
|
|
|
url.password != "" ||
|
|
|
|
url.pathname != "/" ||
|
|
|
|
url.search != "" ||
|
|
|
|
url.hash != ""
|
|
|
|
) {
|
|
|
|
throw new InvalidArgumentError(
|
|
|
|
`${host} was not of the form host[:port]`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-10-09 15:10:03 +03:00
|
|
|
return [hostname, port];
|
2017-08-18 15:49:00 +03:00
|
|
|
}
|
|
|
|
|
2018-06-26 19:15:28 +03:00
|
|
|
let p = new Proxy();
|
2016-12-31 15:21:34 +03:00
|
|
|
if (typeof json == "undefined" || json === null) {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-12-05 01:32:44 +03:00
|
|
|
assert.object(json, pprint`Expected "proxy" to be an object, got ${json}`);
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2017-12-05 01:32:44 +03:00
|
|
|
assert.in(
|
|
|
|
"proxyType",
|
|
|
|
json,
|
|
|
|
pprint`Expected "proxyType" in "proxy" object, got ${json}`
|
|
|
|
);
|
|
|
|
p.proxyType = assert.string(
|
|
|
|
json.proxyType,
|
|
|
|
pprint`Expected "proxyType" to be a string, got ${json.proxyType}`
|
|
|
|
);
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2017-08-16 22:58:55 +03:00
|
|
|
switch (p.proxyType) {
|
2017-08-18 15:49:00 +03:00
|
|
|
case "autodetect":
|
|
|
|
case "direct":
|
|
|
|
case "system":
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "pac":
|
2017-12-05 01:32:44 +03:00
|
|
|
p.proxyAutoconfigUrl = assert.string(
|
|
|
|
json.proxyAutoconfigUrl,
|
|
|
|
`Expected "proxyAutoconfigUrl" to be a string, ` +
|
|
|
|
pprint`got ${json.proxyAutoconfigUrl}`
|
|
|
|
);
|
2017-08-18 15:49:00 +03:00
|
|
|
break;
|
|
|
|
|
2017-08-16 22:58:55 +03:00
|
|
|
case "manual":
|
2017-08-18 15:49:00 +03:00
|
|
|
if (typeof json.ftpProxy != "undefined") {
|
|
|
|
[p.ftpProxy, p.ftpProxyPort] = fromHost("ftp", json.ftpProxy);
|
|
|
|
}
|
2017-08-16 22:58:55 +03:00
|
|
|
if (typeof json.httpProxy != "undefined") {
|
2017-08-18 15:49:00 +03:00
|
|
|
[p.httpProxy, p.httpProxyPort] = fromHost("http", json.httpProxy);
|
2017-08-16 22:58:55 +03:00
|
|
|
}
|
|
|
|
if (typeof json.sslProxy != "undefined") {
|
2017-08-18 15:49:00 +03:00
|
|
|
[p.sslProxy, p.sslProxyPort] = fromHost("https", json.sslProxy);
|
2017-08-16 22:58:55 +03:00
|
|
|
}
|
|
|
|
if (typeof json.socksProxy != "undefined") {
|
2017-08-18 15:49:00 +03:00
|
|
|
[p.socksProxy, p.socksProxyPort] = fromHost("socks", json.socksProxy);
|
2018-05-31 08:17:15 +03:00
|
|
|
p.socksVersion = assert.positiveInteger(
|
|
|
|
json.socksVersion,
|
2020-03-09 00:45:16 +03:00
|
|
|
pprint`Expected "socksVersion" to be a positive integer, got ${json.socksVersion}`
|
2018-05-31 08:17:15 +03:00
|
|
|
);
|
2017-08-16 22:58:55 +03:00
|
|
|
}
|
2017-08-31 18:49:36 +03:00
|
|
|
if (typeof json.noProxy != "undefined") {
|
2017-12-05 01:32:44 +03:00
|
|
|
let entries = assert.array(
|
|
|
|
json.noProxy,
|
|
|
|
pprint`Expected "noProxy" to be an array, got ${json.noProxy}`
|
|
|
|
);
|
2017-10-09 15:10:03 +03:00
|
|
|
p.noProxy = entries.map(entry => {
|
2017-12-05 01:32:44 +03:00
|
|
|
assert.string(
|
|
|
|
entry,
|
|
|
|
pprint`Expected "noProxy" entry to be a string, got ${entry}`
|
|
|
|
);
|
2017-10-09 15:10:03 +03:00
|
|
|
return stripBracketsFromIpv6Hostname(entry);
|
|
|
|
});
|
2017-08-31 18:49:36 +03:00
|
|
|
}
|
2017-08-16 22:58:55 +03:00
|
|
|
break;
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2017-08-18 15:49:00 +03:00
|
|
|
default:
|
|
|
|
throw new InvalidArgumentError(`Invalid type of proxy: ${p.proxyType}`);
|
2016-12-31 15:21:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
2017-08-18 15:49:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {Object.<string, (number|string)>}
|
|
|
|
* JSON serialisation of proxy object.
|
|
|
|
*/
|
|
|
|
toJSON() {
|
2017-10-09 15:10:03 +03:00
|
|
|
function addBracketsToIpv6Hostname(hostname) {
|
|
|
|
return hostname.includes(":") ? `[${hostname}]` : hostname;
|
|
|
|
}
|
|
|
|
|
2017-08-18 15:49:00 +03:00
|
|
|
function toHost(hostname, port) {
|
|
|
|
if (!hostname) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-10-09 15:10:03 +03:00
|
|
|
// Add brackets around IPv6 addresses
|
|
|
|
hostname = addBracketsToIpv6Hostname(hostname);
|
|
|
|
|
2017-08-18 15:49:00 +03:00
|
|
|
if (port != null) {
|
|
|
|
return `${hostname}:${port}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostname;
|
|
|
|
}
|
|
|
|
|
2017-10-09 15:10:03 +03:00
|
|
|
let excludes = this.noProxy;
|
|
|
|
if (excludes) {
|
|
|
|
excludes = excludes.map(addBracketsToIpv6Hostname);
|
|
|
|
}
|
|
|
|
|
2017-08-18 15:49:00 +03:00
|
|
|
return marshal({
|
|
|
|
proxyType: this.proxyType,
|
|
|
|
ftpProxy: toHost(this.ftpProxy, this.ftpProxyPort),
|
|
|
|
httpProxy: toHost(this.httpProxy, this.httpProxyPort),
|
2017-10-09 15:10:03 +03:00
|
|
|
noProxy: excludes,
|
2017-08-18 15:49:00 +03:00
|
|
|
sslProxy: toHost(this.sslProxy, this.sslProxyPort),
|
|
|
|
socksProxy: toHost(this.socksProxy, this.socksProxyPort),
|
2017-08-23 12:47:01 +03:00
|
|
|
socksVersion: this.socksVersion,
|
2017-08-18 15:49:00 +03:00
|
|
|
proxyAutoconfigUrl: this.proxyAutoconfigUrl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-26 19:15:28 +03:00
|
|
|
toString() {
|
|
|
|
return "[object Proxy]";
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2018-06-10 15:37:19 +03:00
|
|
|
/**
|
|
|
|
* Enum of unhandled prompt behavior.
|
|
|
|
*
|
|
|
|
* @enum
|
|
|
|
*/
|
|
|
|
const UnhandledPromptBehavior = {
|
|
|
|
/** All simple dialogs encountered should be accepted. */
|
|
|
|
Accept: "accept",
|
|
|
|
/**
|
|
|
|
* All simple dialogs encountered should be accepted, and an error
|
|
|
|
* returned that the dialog was handled.
|
|
|
|
*/
|
|
|
|
AcceptAndNotify: "accept and notify",
|
|
|
|
/** All simple dialogs encountered should be dismissed. */
|
|
|
|
Dismiss: "dismiss",
|
|
|
|
/**
|
|
|
|
* All simple dialogs encountered should be dismissed, and an error
|
|
|
|
* returned that the dialog was handled.
|
|
|
|
*/
|
|
|
|
DismissAndNotify: "dismiss and notify",
|
|
|
|
/** All simple dialogs encountered should be left to the user to handle. */
|
|
|
|
Ignore: "ignore",
|
|
|
|
};
|
|
|
|
|
2016-12-31 15:21:34 +03:00
|
|
|
/** WebDriver session capabilities representation. */
|
2018-06-26 19:15:28 +03:00
|
|
|
class Capabilities extends Map {
|
2017-07-26 15:11:53 +03:00
|
|
|
/** @class */
|
2017-06-30 02:40:24 +03:00
|
|
|
constructor() {
|
2016-12-31 15:21:34 +03:00
|
|
|
super([
|
|
|
|
// webdriver
|
2019-10-10 01:46:02 +03:00
|
|
|
["browserName", getWebDriverBrowserName()],
|
2016-12-31 15:21:34 +03:00
|
|
|
["browserVersion", appinfo.version],
|
2018-06-23 16:31:00 +03:00
|
|
|
["platformName", getWebDriverPlatformName()],
|
2016-12-31 15:21:34 +03:00
|
|
|
["platformVersion", Services.sysinfo.getProperty("version")],
|
|
|
|
["acceptInsecureCerts", false],
|
2018-06-10 15:37:19 +03:00
|
|
|
["pageLoadStrategy", PageLoadStrategy.Normal],
|
2018-06-26 19:15:28 +03:00
|
|
|
["proxy", new Proxy()],
|
2018-07-21 20:30:55 +03:00
|
|
|
["setWindowRect", appinfo.name == "firefox"],
|
2018-06-10 15:37:19 +03:00
|
|
|
["timeouts", new Timeouts()],
|
2018-11-03 19:31:00 +03:00
|
|
|
["strictFileInteractability", false],
|
2018-06-10 15:37:19 +03:00
|
|
|
["unhandledPromptBehavior", UnhandledPromptBehavior.DismissAndNotify],
|
2016-12-31 15:21:34 +03:00
|
|
|
|
|
|
|
// features
|
|
|
|
["rotatable", appinfo.name == "B2G"],
|
|
|
|
|
|
|
|
// proprietary
|
|
|
|
["moz:accessibilityChecks", false],
|
2019-02-13 13:23:30 +03:00
|
|
|
["moz:buildID", Services.appinfo.appBuildID],
|
2017-07-04 01:43:39 +03:00
|
|
|
[
|
|
|
|
"moz:headless",
|
|
|
|
Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo).isHeadless,
|
|
|
|
],
|
2017-09-01 18:11:35 +03:00
|
|
|
["moz:processID", Services.appinfo.processID],
|
|
|
|
["moz:profile", maybeProfile()],
|
2018-10-19 14:52:42 +03:00
|
|
|
[
|
|
|
|
"moz:shutdownTimeout",
|
|
|
|
Services.prefs.getIntPref("toolkit.asyncshutdown.crash_timeout"),
|
|
|
|
],
|
2018-01-23 19:31:06 +03:00
|
|
|
["moz:useNonSpecCompliantPointerOrigin", false],
|
2017-11-02 00:18:39 +03:00
|
|
|
["moz:webdriverClick", true],
|
2016-12-31 15:21:34 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
|
|
|
* @param {string} key
|
2017-08-04 22:04:12 +03:00
|
|
|
* Capability key.
|
2017-07-26 15:11:53 +03:00
|
|
|
* @param {(string|number|boolean)} value
|
|
|
|
* JSON-safe capability value.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
set(key, value) {
|
2018-06-26 19:15:28 +03:00
|
|
|
if (key === "timeouts" && !(value instanceof Timeouts)) {
|
2016-12-31 15:21:34 +03:00
|
|
|
throw new TypeError();
|
2018-06-26 19:15:28 +03:00
|
|
|
} else if (key === "proxy" && !(value instanceof Proxy)) {
|
2016-12-31 15:21:34 +03:00
|
|
|
throw new TypeError();
|
|
|
|
}
|
|
|
|
|
2017-04-19 13:32:20 +03:00
|
|
|
return super.set(key, value);
|
2016-12-31 15:21:34 +03:00
|
|
|
}
|
|
|
|
|
2018-06-26 19:15:28 +03:00
|
|
|
toString() {
|
|
|
|
return "[object Capabilities]";
|
|
|
|
}
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
|
|
|
* JSON serialisation of capabilities object.
|
|
|
|
*
|
|
|
|
* @return {Object.<string, ?>}
|
|
|
|
*/
|
2016-12-31 15:21:34 +03:00
|
|
|
toJSON() {
|
2019-01-07 02:04:41 +03:00
|
|
|
let marshalled = marshal(this);
|
|
|
|
marshalled.timeouts = super.get("timeouts");
|
|
|
|
return marshalled;
|
2016-12-31 15:21:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unmarshal a JSON object representation of WebDriver capabilities.
|
|
|
|
*
|
2017-08-04 22:04:12 +03:00
|
|
|
* @param {Object.<string, *>=} json
|
2016-12-31 15:21:34 +03:00
|
|
|
* WebDriver capabilities.
|
|
|
|
*
|
2018-06-26 19:15:28 +03:00
|
|
|
* @return {Capabilities}
|
2016-12-31 15:21:34 +03:00
|
|
|
* Internal representation of WebDriver capabilities.
|
|
|
|
*/
|
2017-08-04 22:04:12 +03:00
|
|
|
static fromJSON(json) {
|
2016-12-31 15:21:34 +03:00
|
|
|
if (typeof json == "undefined" || json === null) {
|
|
|
|
json = {};
|
|
|
|
}
|
2018-05-31 08:17:15 +03:00
|
|
|
assert.object(
|
|
|
|
json,
|
|
|
|
pprint`Expected "capabilities" to be an object, got ${json}"`
|
|
|
|
);
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2018-06-26 19:15:28 +03:00
|
|
|
return Capabilities.match_(json);
|
2016-12-31 15:21:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Matches capabilities as described by WebDriver.
|
2017-08-04 22:04:12 +03:00
|
|
|
static match_(json = {}) {
|
2018-06-26 19:15:28 +03:00
|
|
|
let matched = new Capabilities();
|
2016-12-31 15:21:34 +03:00
|
|
|
|
2017-08-04 22:04:12 +03:00
|
|
|
for (let [k, v] of Object.entries(json)) {
|
2016-12-31 15:21:34 +03:00
|
|
|
switch (k) {
|
|
|
|
case "acceptInsecureCerts":
|
2018-07-27 16:17:59 +03:00
|
|
|
assert.boolean(v, pprint`Expected ${k} to be a boolean, got ${v}`);
|
2016-12-31 15:21:34 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "pageLoadStrategy":
|
2018-07-27 16:17:59 +03:00
|
|
|
assert.string(v, pprint`Expected ${k} to be a string, got ${v}`);
|
2018-07-21 20:29:39 +03:00
|
|
|
if (!Object.values(PageLoadStrategy).includes(v)) {
|
2018-07-27 16:17:59 +03:00
|
|
|
throw new InvalidArgumentError("Unknown page load strategy: " + v);
|
2016-12-31 15:21:34 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "proxy":
|
2018-07-21 20:29:39 +03:00
|
|
|
v = Proxy.fromJSON(v);
|
2016-12-31 15:21:34 +03:00
|
|
|
break;
|
|
|
|
|
2018-07-21 20:30:55 +03:00
|
|
|
case "setWindowRect":
|
|
|
|
assert.boolean(v, pprint`Expected ${k} to be boolean, got ${v}`);
|
|
|
|
if (appinfo.name == "firefox" && !v) {
|
|
|
|
throw new InvalidArgumentError("setWindowRect cannot be disabled");
|
|
|
|
} else if (appinfo.name != "firefox" && v) {
|
|
|
|
throw new InvalidArgumentError(
|
|
|
|
"setWindowRect is only supported in Firefox desktop"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-12-31 15:21:34 +03:00
|
|
|
case "timeouts":
|
2018-07-21 20:29:39 +03:00
|
|
|
v = Timeouts.fromJSON(v);
|
2016-12-31 15:21:34 +03:00
|
|
|
break;
|
|
|
|
|
2018-11-03 19:31:00 +03:00
|
|
|
case "strictFileInteractability":
|
|
|
|
v = assert.boolean(v);
|
|
|
|
break;
|
|
|
|
|
2018-06-10 15:37:19 +03:00
|
|
|
case "unhandledPromptBehavior":
|
2018-07-27 16:17:59 +03:00
|
|
|
assert.string(v, pprint`Expected ${k} to be a string, got ${v}`);
|
2018-07-21 20:29:39 +03:00
|
|
|
if (!Object.values(UnhandledPromptBehavior).includes(v)) {
|
2018-06-10 15:37:19 +03:00
|
|
|
throw new InvalidArgumentError(
|
|
|
|
`Unknown unhandled prompt behavior: ${v}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-01-23 19:31:06 +03:00
|
|
|
case "moz:accessibilityChecks":
|
2018-07-21 20:29:39 +03:00
|
|
|
assert.boolean(v, pprint`Expected ${k} to be boolean, got ${v}`);
|
2016-12-31 15:21:34 +03:00
|
|
|
break;
|
|
|
|
|
2018-01-23 19:31:06 +03:00
|
|
|
case "moz:useNonSpecCompliantPointerOrigin":
|
2018-07-21 20:29:39 +03:00
|
|
|
assert.boolean(v, pprint`Expected ${k} to be boolean, got ${v}`);
|
2018-01-23 19:31:06 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "moz:webdriverClick":
|
2018-07-21 20:29:39 +03:00
|
|
|
assert.boolean(v, pprint`Expected ${k} to be boolean, got ${v}`);
|
2016-12-31 15:21:34 +03:00
|
|
|
break;
|
|
|
|
}
|
2018-07-21 20:29:39 +03:00
|
|
|
|
|
|
|
matched.set(k, v);
|
2016-12-31 15:21:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return matched;
|
|
|
|
}
|
2018-06-26 19:15:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this.Capabilities = Capabilities;
|
|
|
|
this.PageLoadStrategy = PageLoadStrategy;
|
|
|
|
this.Proxy = Proxy;
|
|
|
|
this.Timeouts = Timeouts;
|
2018-06-10 15:37:19 +03:00
|
|
|
this.UnhandledPromptBehavior = UnhandledPromptBehavior;
|
2018-06-23 16:31:00 +03:00
|
|
|
|
2019-09-26 02:00:24 +03:00
|
|
|
function getWebDriverBrowserName() {
|
|
|
|
// Similar to chromedriver which reports "chrome" as browser name for all
|
|
|
|
// WebView apps, we will report "firefox" for all GeckoView apps.
|
|
|
|
if (Services.androidBridge) {
|
|
|
|
return "firefox";
|
|
|
|
}
|
|
|
|
|
|
|
|
return appinfo.name;
|
|
|
|
}
|
|
|
|
|
2018-06-23 16:31:00 +03:00
|
|
|
function getWebDriverPlatformName() {
|
|
|
|
let name = Services.sysinfo.getProperty("name");
|
|
|
|
|
2019-09-26 02:00:24 +03:00
|
|
|
if (Services.androidBridge) {
|
|
|
|
return "android";
|
|
|
|
}
|
|
|
|
|
2018-06-23 16:31:00 +03:00
|
|
|
switch (name) {
|
|
|
|
case "Windows_NT":
|
|
|
|
return "windows";
|
|
|
|
|
|
|
|
case "Darwin":
|
|
|
|
return "mac";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return name.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 15:21:34 +03:00
|
|
|
// Specialisation of |JSON.stringify| that produces JSON-safe object
|
|
|
|
// literals, dropping empty objects and entries which values are undefined
|
2019-01-07 02:04:41 +03:00
|
|
|
// or null. Objects are allowed to produce their own JSON representations
|
2016-12-31 15:21:34 +03:00
|
|
|
// by implementing a |toJSON| function.
|
|
|
|
function marshal(obj) {
|
|
|
|
let rv = Object.create(null);
|
|
|
|
|
|
|
|
function* iter(mapOrObject) {
|
|
|
|
if (mapOrObject instanceof Map) {
|
2017-06-30 02:40:24 +03:00
|
|
|
for (const [k, v] of mapOrObject) {
|
|
|
|
yield [k, v];
|
2016-12-31 15:21:34 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (const k of Object.keys(mapOrObject)) {
|
|
|
|
yield [k, mapOrObject[k]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
for (let [k, v] of iter(obj)) {
|
2016-12-31 15:21:34 +03:00
|
|
|
// Skip empty values when serialising to JSON.
|
|
|
|
if (typeof v == "undefined" || v === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively marshal objects that are able to produce their own
|
|
|
|
// JSON representation.
|
|
|
|
if (typeof v.toJSON == "function") {
|
|
|
|
v = marshal(v.toJSON());
|
|
|
|
|
|
|
|
// Or do the same for object literals.
|
2017-06-30 02:40:24 +03:00
|
|
|
} else if (isObject(v)) {
|
2016-12-31 15:21:34 +03:00
|
|
|
v = marshal(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And finally drop (possibly marshaled) objects which have no
|
|
|
|
// entries.
|
|
|
|
if (!isObjectEmpty(v)) {
|
|
|
|
rv[k] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isObject(obj) {
|
|
|
|
return Object.prototype.toString.call(obj) == "[object Object]";
|
|
|
|
}
|
|
|
|
|
|
|
|
function isObjectEmpty(obj) {
|
|
|
|
return isObject(obj) && Object.keys(obj).length === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Services.dirsvc is not accessible from content frame scripts,
|
|
|
|
// but we should not panic about that.
|
|
|
|
function maybeProfile() {
|
|
|
|
try {
|
|
|
|
return Services.dirsvc.get("ProfD", Ci.nsIFile).path;
|
|
|
|
} catch (e) {
|
|
|
|
return "<protected>";
|
|
|
|
}
|
|
|
|
}
|