diff --git a/dotnet/src/webdriver/Firefox/FirefoxOptions.cs b/dotnet/src/webdriver/Firefox/FirefoxOptions.cs index 779db0b1b2b1e..193b782cfd2ac 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxOptions.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxOptions.cs @@ -62,6 +62,7 @@ public class FirefoxOptions : DriverOptions private string browserBinaryLocation; private FirefoxDriverLogLevel logLevel = FirefoxDriverLogLevel.Default; private FirefoxProfile profile; + private Proxy proxy; private List firefoxArguments = new List(); private Dictionary profilePreferences = new Dictionary(); private Dictionary additionalCapabilities = new Dictionary(); @@ -120,6 +121,15 @@ public string BrowserExecutableLocation set { this.browserBinaryLocation = value; } } + /// + /// Gets or sets the to be used with Firefox. + /// + public Proxy Proxy + { + get { return this.proxy; } + set { this.proxy = value; } + } + /// /// Gets or sets the logging level of the Firefox driver. /// @@ -270,6 +280,7 @@ public void AddAdditionalCapability(string capabilityName, object capabilityValu if (capabilityName == IsMarionetteCapability || capabilityName == FirefoxProfileCapability || capabilityName == FirefoxBinaryCapability || + capabilityName == CapabilityType.Proxy || capabilityName == FirefoxLegacyProfileCapability || capabilityName == FirefoxLegacyBinaryCapability || capabilityName == FirefoxArgumentsCapability || @@ -307,6 +318,15 @@ public override ICapabilities ToCapabilities() DesiredCapabilities capabilities = DesiredCapabilities.Firefox(); if (this.isMarionette) { + if (this.proxy != null) + { + Dictionary proxyCapabiity = this.proxy.ToCapability(); + if (proxyCapabiity != null) + { + capabilities.SetCapability(CapabilityType.Proxy, proxyCapabiity); + } + } + Dictionary firefoxOptions = this.GenerateFirefoxOptionsDictionary(); capabilities.SetCapability(FirefoxOptionsCapability, firefoxOptions); } @@ -315,6 +335,11 @@ public override ICapabilities ToCapabilities() capabilities.SetCapability(IsMarionetteCapability, this.isMarionette); if (this.profile != null) { + if (this.proxy != null) + { + this.profile.InternalSetProxyPreferences(this.proxy); + } + capabilities.SetCapability(FirefoxProfileCapability, this.profile.ToBase64String()); } diff --git a/dotnet/src/webdriver/Firefox/FirefoxProfile.cs b/dotnet/src/webdriver/Firefox/FirefoxProfile.cs index 596d82e3d175d..3b00c2cd32b84 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxProfile.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxProfile.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information @@ -241,39 +241,10 @@ public void SetPreference(string name, bool value) /// /// The object defining the proxy /// preferences for the profile. + [Obsolete("Use the FirefoxOptions class to set a proxy for Firefox.")] public void SetProxyPreferences(Proxy proxy) { - if (proxy == null) - { - throw new ArgumentNullException("proxy", "proxy must not be null"); - } - - if (proxy.Kind == ProxyKind.Unspecified) - { - return; - } - - this.SetPreference("network.proxy.type", (int)proxy.Kind); - - switch (proxy.Kind) - { - case ProxyKind.Manual: // By default, assume we're proxying the lot - this.SetPreference("network.proxy.no_proxies_on", string.Empty); - - this.SetManualProxyPreference("ftp", proxy.FtpProxy); - this.SetManualProxyPreference("http", proxy.HttpProxy); - this.SetManualProxyPreference("ssl", proxy.SslProxy); - this.SetManualProxyPreference("socks", proxy.SocksProxy); - if (proxy.NoProxy != null) - { - this.SetPreference("network.proxy.no_proxies_on", proxy.NoProxy); - } - - break; - case ProxyKind.ProxyAutoConfigure: - this.SetPreference("network.proxy.autoconfig_url", proxy.ProxyAutoConfigUrl); - break; - } + this.InternalSetProxyPreferences(proxy); } /// @@ -356,6 +327,46 @@ internal void AddWebDriverExtension() } } + /// + /// Internal implementation to set proxy preferences for this profile. + /// + /// The object defining the proxy + /// preferences for the profile. + internal void InternalSetProxyPreferences(Proxy proxy) + { + if (proxy == null) + { + throw new ArgumentNullException("proxy", "proxy must not be null"); + } + + if (proxy.Kind == ProxyKind.Unspecified) + { + return; + } + + this.SetPreference("network.proxy.type", (int)proxy.Kind); + + switch (proxy.Kind) + { + case ProxyKind.Manual: // By default, assume we're proxying the lot + this.SetPreference("network.proxy.no_proxies_on", string.Empty); + + this.SetManualProxyPreference("ftp", proxy.FtpProxy); + this.SetManualProxyPreference("http", proxy.HttpProxy); + this.SetManualProxyPreference("ssl", proxy.SslProxy); + this.SetManualProxyPreference("socks", proxy.SocksProxy); + if (proxy.NoProxy != null) + { + this.SetPreference("network.proxy.no_proxies_on", proxy.NoProxy); + } + + break; + case ProxyKind.ProxyAutoConfigure: + this.SetPreference("network.proxy.autoconfig_url", proxy.ProxyAutoConfigUrl); + break; + } + } + /// /// Generates a random directory name for the profile. /// diff --git a/dotnet/src/webdriver/IE/InternetExplorerOptions.cs b/dotnet/src/webdriver/IE/InternetExplorerOptions.cs index 27ce5d32a47c0..6cbc2bb4478f0 100644 --- a/dotnet/src/webdriver/IE/InternetExplorerOptions.cs +++ b/dotnet/src/webdriver/IE/InternetExplorerOptions.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information @@ -484,7 +484,11 @@ public override ICapabilities ToCapabilities() if (this.proxy != null) { - capabilities.SetCapability(CapabilityType.Proxy, this.proxy); + Dictionary proxyCapability = this.proxy.ToCapability(); + if (proxyCapability != null) + { + capabilities.SetCapability(CapabilityType.Proxy, proxyCapability); + } } Dictionary internetExplorerOptions = this.BuildInternetExplorerOptionsDictionary(); diff --git a/dotnet/src/webdriver/Proxy.cs b/dotnet/src/webdriver/Proxy.cs index 86f956120c7ce..dbfea40124318 100644 --- a/dotnet/src/webdriver/Proxy.cs +++ b/dotnet/src/webdriver/Proxy.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information @@ -79,6 +79,7 @@ public class Proxy private string socksProxyLocation; private string socksUserName; private string socksPassword; + private List noProxyAddresses = new List(); /// /// Initializes a new instance of the class. @@ -250,19 +251,23 @@ public string HttpProxy /// /// Gets or sets the value for bypass proxy addresses. /// + [Obsolete("Add addresses to bypass with the proxy by using the AddBypassAddress method.")] [JsonProperty("noProxy", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore)] public string NoProxy { get { - return this.noProxy; + if (this.noProxyAddresses.Count == 0) + { + return null; + } + + return string.Join(";", this.noProxyAddresses); } set { - this.VerifyProxyTypeCompatilibily(ProxyKind.Manual); - this.proxyKind = ProxyKind.Manual; - this.noProxy = value; + this.AddBypassAddress(value); } } @@ -361,6 +366,104 @@ public string SocksPassword } } + /// + /// Adds a single address to the list of addresses against which the proxy will not be used. + /// + /// The address to add. + public void AddBypassAddress(string address) + { + if (string.IsNullOrEmpty(address)) + { + throw new ArgumentException("address must not be null or empty", "address"); + } + + this.AddBypassAddresses(address); + } + + /// + /// Adds addresses to the list of addresses against which the proxy will not be used. + /// + /// An array of addresses to add. + public void AddBypassAddresses(params string[] addressesToAdd) + { + this.AddBypassAddresses(new List(addressesToAdd)); + } + + /// + /// Adds addresses to the list of addresses against which the proxy will not be used. + /// + /// An object of arguments to add. + public void AddBypassAddresses(IEnumerable addressesToAdd) + { + if (addressesToAdd == null) + { + throw new ArgumentNullException("addressesToAdd", "addressesToAdd must not be null"); + } + + this.VerifyProxyTypeCompatilibily(ProxyKind.Manual); + this.proxyKind = ProxyKind.Manual; + this.noProxyAddresses.AddRange(addressesToAdd); + } + + /// + /// Returns a dictionary suitable for serializing to the W3C Specification + /// dialect of the wire protocol. + /// + /// A dictionary suitable for serializing to the W3C Specification + /// dialect of the wire protocol. + internal Dictionary ToCapability() + { + Dictionary serializedDictionary = null; + if (this.proxyKind != ProxyKind.Unspecified) + { + serializedDictionary = new Dictionary(); + if (this.proxyKind == ProxyKind.ProxyAutoConfigure) + { + serializedDictionary["proxyType"] = "pac"; + } + else + { + serializedDictionary["proxyType"] = this.proxyKind.ToString().ToLowerInvariant(); + } + + if (!string.IsNullOrEmpty(this.httpProxyLocation)) + { + serializedDictionary["httpProxy"] = this.httpProxyLocation; + } + + if (!string.IsNullOrEmpty(this.sslProxyLocation)) + { + serializedDictionary["sslProxy"] = this.sslProxyLocation; + } + + if (!string.IsNullOrEmpty(this.ftpProxyLocation)) + { + serializedDictionary["ftpProxy"] = this.ftpProxyLocation; + } + + if (!string.IsNullOrEmpty(this.socksProxyLocation)) + { + string socksAuth = string.Empty; + if (!string.IsNullOrEmpty(this.socksUserName) && !string.IsNullOrEmpty(this.socksPassword)) + { + // TODO: this is probably inaccurate as to how this is supposed + // to look. + socksAuth = this.socksUserName + ":" + this.socksPassword + "@"; + } + + serializedDictionary["socksProxy"] = socksAuth + this.socksProxyLocation; + } + + if (this.noProxyAddresses.Count > 0) + { + List addressList = new List(this.noProxyAddresses); + serializedDictionary["noProxy"] = addressList; + } + } + + return serializedDictionary; + } + private void VerifyProxyTypeCompatilibily(ProxyKind compatibleProxy) { if (this.proxyKind != ProxyKind.Unspecified && this.proxyKind != compatibleProxy)