Bug 1165449: Add the ability to set a proxy via capabilities on Marionette session start; r=jgriffin

By passing an object to capabilities like

  {
    "requiredCapabilities": {
        "proxy": {... }
    }
  }

Specification detail at http://w3c.github.io/webdriver/webdriver-spec.html#dfn-set-the-proxy
This commit is contained in:
David Burns 2015-07-14 00:51:58 +01:00
parent 9148ae4112
commit 6826f183e8
2 changed files with 303 additions and 0 deletions

View File

@ -0,0 +1,250 @@
# 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/.
from marionette import MarionetteTestCase
from marionette_driver.errors import InvalidArgumentException
class TestProxy(MarionetteTestCase):
def setUp(self):
super(TestProxy, self).setUp()
self.marionette.delete_session()
def test_that_we_can_set_a_autodetect_proxy(self):
capabilities = {"requiredCapabilities":
{
"proxy":{
"proxyType": "autodetect",
}
}
}
self.marionette.start_session(capabilities)
result = None
with self.marionette.using_context('chrome'):
result = self.marionette.execute_script("""return {
"proxyType" : Services.prefs.getIntPref('network.proxy.type'),
}
""")
self.assertEqual(result["proxyType"], 4)
def test_that_capabilities_returned_have_proxy_details(self):
capabilities = {"requiredCapabilities":
{
"proxy":{
"proxyType": "autodetect",
}
}
}
self.marionette.start_session(capabilities)
result = self.marionette.session_capabilities
self.assertEqual(result["proxy"]["proxyType"], "autodetect")
def test_that_we_can_set_a_system_proxy(self):
capabilities = {"requiredCapabilities":
{
"proxy":{
"proxyType": "system",
}
}
}
self.marionette.start_session(capabilities)
result = None
with self.marionette.using_context('chrome'):
result = self.marionette.execute_script("""return {
"proxyType" : Services.prefs.getIntPref('network.proxy.type'),
}
""")
self.assertEqual(result["proxyType"], 5)
def test_we_can_set_a_pac_proxy(self):
url = "http://marionette.test"
capabilities = {"requiredCapabilities":
{
"proxy":{
"proxyType": "pac",
"pacUrl": url,
}
}
}
self.marionette.start_session(capabilities)
result = None
with self.marionette.using_context('chrome'):
result = self.marionette.execute_script("""return {
"proxyType" : Services.prefs.getIntPref('network.proxy.type'),
"pacUrl" : Services.prefs.getCharPref('network.proxy.autoconfig_url'),
}
""")
self.assertEqual(result["proxyType"], 2)
self.assertEqual(result["pacUrl"], url, 'pacUrl was not set')
def test_that_we_can_set_a_manual_proxy(self):
port = 4444
url = "http://marionette.test"
capabilities = {"requiredCapabilities":
{
"proxy":{
"proxyType": "manual",
"ftpProxy": url,
"ftpProxyPort": port,
"httpProxy": url,
"httpProxyPort": port,
"sslProxy": url,
"sslProxyPort": port,
}
}
}
self.marionette.start_session(capabilities)
result = None
with self.marionette.using_context('chrome'):
result = self.marionette.execute_script("""return {
"proxyType" : Services.prefs.getIntPref('network.proxy.type'),
"httpProxy" : Services.prefs.getCharPref('network.proxy.http'),
"httpProxyPort": Services.prefs.getIntPref('network.proxy.http_port'),
"sslProxy": Services.prefs.getCharPref('network.proxy.ssl'),
"sslProxyPort": Services.prefs.getIntPref('network.proxy.ssl_port'),
"ftpProxy": Services.prefs.getCharPref('network.proxy.ftp'),
"ftpProxyPort": Services.prefs.getIntPref('network.proxy.ftp_port'),
}
""")
self.assertEqual(result["proxyType"], 1)
self.assertEqual(result["httpProxy"], url, 'httpProxy was not set')
self.assertEqual(result["httpProxyPort"], port, 'httpProxyPort was not set')
self.assertEqual(result["sslProxy"], url, 'sslProxy url was not set')
self.assertEqual(result["sslProxyPort"], port, 'sslProxyPort was not set')
self.assertEqual(result["ftpProxy"], url, 'ftpProxy was not set')
self.assertEqual(result["ftpProxyPort"], port, 'ftpProxyPort was not set')
def test_we_can_set_a_manual_proxy_with_a_socks_proxy_with_socks_version(self):
port = 4444
url = "http://marionette.test"
capabilities = {"requiredCapabilities":
{
"proxy":{
"proxyType": "manual",
"socksProxy": url,
"socksProxyPort": port,
"socksVersion": 4,
"socksUsername": "cake",
"socksPassword": "made with cake"
}
}
}
self.marionette.start_session(capabilities)
result = None
with self.marionette.using_context('chrome'):
result = self.marionette.execute_script("""return {
"proxyType" : Services.prefs.getIntPref('network.proxy.type'),
"socksProxy" : Services.prefs.getCharPref('network.proxy.socks'),
"socksProxyPort": Services.prefs.getIntPref('network.proxy.socks_port'),
"socksVersion": Services.prefs.getIntPref('network.proxy.socks_version'),
}
""")
self.assertEqual(result["socksProxy"], url, 'socksProxy was not set')
self.assertEqual(result["socksProxyPort"], port, 'socksProxyPort was not set')
self.assertEqual(result["socksVersion"], 4, 'socksVersion was not set to 4')
def test_we_can_set_a_manual_proxy_with_a_socks_proxy_with_no_socks_version(self):
port = 4444
url = "http://marionette.test"
capabilities = {"requiredCapabilities":
{
"proxy":{
"proxyType": "manual",
"socksProxy": url,
"socksProxyPort": port,
"socksUsername": "cake",
"socksPassword": "made with cake"
}
}
}
self.marionette.start_session(capabilities)
result = None
with self.marionette.using_context('chrome'):
result = self.marionette.execute_script("""return {
"proxyType" : Services.prefs.getIntPref('network.proxy.type'),
"socksProxy" : Services.prefs.getCharPref('network.proxy.socks'),
"socksProxyPort": Services.prefs.getIntPref('network.proxy.socks_port'),
"socksVersion": Services.prefs.getIntPref('network.proxy.socks_version'),
}
""")
self.assertEqual(result["socksProxy"], url, 'socksProxy was not set')
self.assertEqual(result["socksProxyPort"], port, 'socksProxyPort was not set')
self.assertEqual(result["socksVersion"], 5, 'socksVersion was not set to 5')
def test_when_not_all_manual_proxy_details_are_in_capabilities(self):
port = 4444
url = "http://marionette.test"
capabilities = {"requiredCapabilities":
{
"proxy":{
"proxyType": "manual",
"ftpProxy": url,
"ftpProxyPort": port,
}
}
}
self.marionette.start_session(capabilities)
result = None
with self.marionette.using_context('chrome'):
result = self.marionette.execute_script("""return {
"proxyType" : Services.prefs.getIntPref('network.proxy.type'),
"httpProxy" : Services.prefs.getCharPref('network.proxy.http'),
"httpProxyPort": Services.prefs.getIntPref('network.proxy.http_port'),
"sslProxy": Services.prefs.getCharPref('network.proxy.ssl'),
"sslProxyPort": Services.prefs.getIntPref('network.proxy.ssl_port'),
"ftpProxy": Services.prefs.getCharPref('network.proxy.ftp'),
"ftpProxyPort": Services.prefs.getIntPref('network.proxy.ftp_port'),
}
""")
self.assertEqual(result["proxyType"], 1)
self.assertNotEqual(result["httpProxy"], url,
'httpProxy was set. %s' % result["httpProxy"])
self.assertNotEqual(result["httpProxyPort"], port, 'httpProxyPort was set')
self.assertNotEqual(result["sslProxy"], url, 'sslProxy url was set')
self.assertNotEqual(result["sslProxyPort"], port, 'sslProxyPort was set')
self.assertEqual(result["ftpProxy"], url, 'ftpProxy was set')
self.assertEqual(result["ftpProxyPort"], port, 'ftpProxyPort was set')
def test_proxy_is_a_string_should_throw_invalid_argument(self):
capabilities = {"requiredCapabilities":
{
"proxy":"I really should be a dictionary"
}
}
try:
self.marionette.start_session(capabilities)
self.fail("We should have started a session because proxy should be a dict")
except InvalidArgumentException as e:
assert e.msg == "the value of 'proxy' should be an object"
def test_proxy_is_passed_in_with_no_proxy_doesnt_set_it(self):
capabilities = {"requiredCapabilities":
{
"proxy": {"proxyType": "NOPROXY"},
}
}
self.marionette.start_session(capabilities)
result = None
with self.marionette.using_context('chrome'):
result = self.marionette.execute_script("""return {
"proxyType": Services.prefs.getIntPref('network.proxy.type'),
};
""")
self.assertEqual(result["proxyType"], 0)
def tearDown(self):
if not self.marionette.session:
self.marionette.start_session()
else:
self.marionette.restart(clean=True)

View File

@ -148,6 +148,7 @@ this.GeckoDriver = function(appName, device, emulator) {
"acceptSslCerts": false,
"takesElementScreenshot": true,
"takesScreenshot": true,
"proxy": {},
// Selenium 2 compat
"platform": Services.appinfo.OS.toUpperCase(),
@ -626,6 +627,11 @@ GeckoDriver.prototype.setSessionCapabilities = function(newCaps) {
to = copy(from[key], to);
break;
case "requiredCapabilities":
if (from[key]["proxy"]) {
this.setUpProxy(from[key]["proxy"]);
to["proxy"] = from[key]["proxy"];
delete from[key]["proxy"];
}
for (let caps in from[key]) {
if (from[key][caps] !== this.sessionCapabilities[caps]) {
errors.push(from[key][caps] + " does not equal " +
@ -652,6 +658,53 @@ GeckoDriver.prototype.setSessionCapabilities = function(newCaps) {
this.sessionCapabilities = caps;
};
GeckoDriver.prototype.setUpProxy = function (proxy) {
logger.debug("Setup Proxy has been entered. Will attempt to setup the following proxy");
logger.debug("Proxy object contains " + JSON.stringify(proxy));
if (typeof proxy == "object" && proxy.hasOwnProperty("proxyType")) {
switch (proxy.proxyType.toUpperCase()) {
case "MANUAL":
Services.prefs.setIntPref("network.proxy.type", 1);
if (proxy.httpProxy && proxy.httpProxyPort){
Services.prefs.setCharPref("network.proxy.http", proxy.httpProxy);
Services.prefs.setIntPref("network.proxy.http_port", proxy.httpProxyPort);
}
if (proxy.sslProxy && proxy.sslProxyPort){
Services.prefs.setCharPref("network.proxy.ssl", proxy.sslProxy);
Services.prefs.setIntPref("network.proxy.ssl_port", proxy.sslProxyPort);
}
if (proxy.ftpProxy && proxy.ftpProxyPort) {
Services.prefs.setCharPref("network.proxy.ftp", proxy.ftpProxy);
Services.prefs.setIntPref("network.proxy.ftp_port", proxy.ftpProxyPort);
}
if (proxy.socksProxy) {
Services.prefs.setCharPref("network.proxy.socks", proxy.socksProxy);
Services.prefs.setIntPref("network.proxy.socks_port", proxy.socksProxyPort);
if (proxy.socksVersion) {
Services.prefs.setIntPref("network.proxy.socks_version", proxy.socksVersion);
}
}
break;
case "PAC":
Services.prefs.setIntPref("network.proxy.type", 2);
Services.prefs.setCharPref("network.proxy.autoconfig_url", proxy.pacUrl);
break;
case "AUTODETECT":
Services.prefs.setIntPref("network.proxy.type", 4);
break;
case "SYSTEM":
Services.prefs.setIntPref("network.proxy.type", 5);
break;
case "NOPROXY":
default:
Services.prefs.setIntPref("network.proxy.type", 0);
}
} else {
throw new InvalidArgumentError("the value of 'proxy' should be an object");
}
};
/**
* Log message. Accepts user defined log-level.
*