Skip to content

Commit

Permalink
Adding .NET integration tests for setting a proxy
Browse files Browse the repository at this point in the history
This commit now brings the .NET test suite into alignment with the Java
test suite for this feature. It includes a forked version of FryProxy
modified to remove its dependency on log4net, and a few other minor
modifications. The forked changes can be found at
https://github.com/jimevans/FryProxy. The resulting NuGet package has
been added to the local package repository, but is not available from
NuGet directly.
  • Loading branch information
jimevans committed Apr 9, 2019
1 parent 962c50d commit 32e343f
Show file tree
Hide file tree
Showing 10 changed files with 877 additions and 55 deletions.
62 changes: 27 additions & 35 deletions dotnet/src/webdriver/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using Newtonsoft.Json;

Expand Down Expand Up @@ -99,23 +100,31 @@ public Proxy(Dictionary<string, object> settings)
throw new ArgumentNullException("settings", "settings dictionary cannot be null");
}

if (settings.ContainsKey("proxyType"))
if (settings.ContainsKey("proxyType") && settings["proxyType"] != null)
{
ProxyKind rawType = (ProxyKind)Enum.Parse(typeof(ProxyKind), settings["proxyType"].ToString(), true);
this.Kind = rawType;
// Special-case "PAC" since that is the correct serialization.
if (settings["proxyType"].ToString().ToLowerInvariant() == "pac")
{
this.Kind = ProxyKind.ProxyAutoConfigure;
}
else
{
ProxyKind rawType = (ProxyKind)Enum.Parse(typeof(ProxyKind), settings["proxyType"].ToString(), true);
this.Kind = rawType;
}
}

if (settings.ContainsKey("ftpProxy"))
if (settings.ContainsKey("ftpProxy") && settings["ftpProxy"] != null)
{
this.FtpProxy = settings["ftpProxy"].ToString();
}

if (settings.ContainsKey("httpProxy"))
if (settings.ContainsKey("httpProxy") && settings["httpProxy"] != null)
{
this.HttpProxy = settings["httpProxy"].ToString();
}

if (settings.ContainsKey("noProxy"))
if (settings.ContainsKey("noProxy") && settings["noProxy"] != null)
{
List<string> bypassAddresses = new List<string>();
string addressesAsString = settings["noProxy"] as string;
Expand All @@ -138,37 +147,37 @@ public Proxy(Dictionary<string, object> settings)
this.AddBypassAddresses(bypassAddresses);
}

if (settings.ContainsKey("proxyAutoconfigUrl"))
if (settings.ContainsKey("proxyAutoconfigUrl") && settings["proxyAutoconfigUrl"] != null)
{
this.ProxyAutoConfigUrl = settings["proxyAutoconfigUrl"].ToString();
}

if (settings.ContainsKey("sslProxy"))
if (settings.ContainsKey("sslProxy") && settings["sslProxy"] != null)
{
this.SslProxy = settings["sslProxy"].ToString();
}

if (settings.ContainsKey("socksProxy"))
if (settings.ContainsKey("socksProxy") && settings["socksProxy"] != null)
{
this.SocksProxy = settings["socksProxy"].ToString();
}

if (settings.ContainsKey("socksUsername"))
if (settings.ContainsKey("socksUsername") && settings["socksUsername"] != null)
{
this.SocksUserName = settings["socksUsername"].ToString();
}

if (settings.ContainsKey("socksPassword"))
if (settings.ContainsKey("socksPassword") && settings["socksPassword"] != null)
{
this.SocksPassword = settings["socksPassword"].ToString();
}

if (settings.ContainsKey("socksVersion"))
if (settings.ContainsKey("socksVersion") && settings["socksVersion"] != null)
{
this.SocksVersion = (int)settings["socksVersion"];
}

if (settings.ContainsKey("autodetect"))
if (settings.ContainsKey("autodetect") && settings["autodetect"] != null)
{
this.IsAutoDetect = (bool)settings["autodetect"];
}
Expand Down Expand Up @@ -202,10 +211,10 @@ public string SerializableProxyKind
{
if (this.proxyKind == ProxyKind.ProxyAutoConfigure)
{
return "PAC";
return "pac";
}

return this.proxyKind.ToString().ToUpperInvariant();
return this.proxyKind.ToString().ToLowerInvariant();
}
}

Expand Down Expand Up @@ -272,27 +281,10 @@ public string HttpProxy
}

/// <summary>
/// Gets or sets the value for bypass proxy addresses.
/// </summary>
[Obsolete("Add addresses to bypass with the proxy by using the AddBypassAddress method.")]
public string NoProxy
{
get
{
return this.BypassProxyAddresses;
}

set
{
this.AddBypassAddress(value);
}
}

/// <summary>
/// Gets the semicolon delimited list of address for which to bypass the proxy.
/// Gets the list of address for which to bypass the proxy as an array.
/// </summary>
[JsonProperty("noProxy", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore)]
public string BypassProxyAddresses
public ReadOnlyCollection<string> BypassProxyAddresses
{
get
{
Expand All @@ -301,7 +293,7 @@ public string BypassProxyAddresses
return null;
}

return string.Join(";", this.noProxyAddresses.ToArray());
return this.noProxyAddresses.AsReadOnly();
}
}

Expand Down
19 changes: 17 additions & 2 deletions dotnet/test/common/Environment/DriverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ private void PopulateServiceTypes()
this.serviceTypes[Browser.Safari] = typeof(SafariDriverService);
}

public event EventHandler<DriverStartingEventArgs> DriverStarting;

public string DriverServicePath
{
get { return this.driverPath; }
Expand All @@ -68,8 +70,8 @@ public IWebDriver CreateDriver(Type driverType)
public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverOptions)
{
Browser browser = Browser.All;
object service = null;
object options = null;
DriverService service = null;
DriverOptions options = null;

List<Type> constructorArgTypeList = new List<Type>();
IWebDriver driver = null;
Expand Down Expand Up @@ -108,6 +110,8 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
service = CreateService<SafariDriverService>(driverType);
}

this.OnDriverLaunching(service, options);

constructorArgTypeList.Add(this.serviceTypes[browser]);
constructorArgTypeList.Add(this.optionsTypes[browser]);
ConstructorInfo ctorInfo = driverType.GetConstructor(constructorArgTypeList.ToArray());
Expand All @@ -120,6 +124,15 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
return driver;
}

protected void OnDriverLaunching(DriverService service, DriverOptions options)
{
if (this.DriverStarting != null)
{
DriverStartingEventArgs args = new DriverStartingEventArgs(service, options);
this.DriverStarting(this, args);
}
}

private T GetDriverOptions<T>(Type driverType, DriverOptions overriddenOptions) where T : DriverOptions, new()
{
T options = new T();
Expand All @@ -135,6 +148,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
{
options.PageLoadStrategy = overriddenOptions.PageLoadStrategy;
options.UnhandledPromptBehavior = overriddenOptions.UnhandledPromptBehavior;
options.Proxy = overriddenOptions.Proxy;
}

return options;
Expand All @@ -157,6 +171,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
{
mergedOptions.PageLoadStrategy = overriddenOptions.PageLoadStrategy;
mergedOptions.UnhandledPromptBehavior = overriddenOptions.UnhandledPromptBehavior;
mergedOptions.Proxy = overriddenOptions.Proxy;
}

return mergedOptions;
Expand Down
24 changes: 24 additions & 0 deletions dotnet/test/common/Environment/DriverStartingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenQA.Selenium.Environment
{
public class DriverStartingEventArgs
{
DriverService service;
DriverOptions options;

public DriverStartingEventArgs(DriverService service, DriverOptions options)
{
this.Service = service;
this.Options = options;
}

public DriverService Service { get => service; set => service = value; }

public DriverOptions Options { get => options; set => options = value; }
}
}
44 changes: 27 additions & 17 deletions dotnet/test/common/Environment/EnvironmentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private EnvironmentManager()
DriverConfig driverConfig = env.DriverConfigs[activeDriverConfig];
WebsiteConfig websiteConfig = env.WebSiteConfigs[activeWebsiteConfig];
this.driverFactory = new DriverFactory(driverServiceLocation);
this.driverFactory.DriverStarting += OnDriverStarting;

Assembly driverAssembly = Assembly.Load(driverConfig.AssemblyName);
driverType = driverAssembly.GetType(driverConfig.DriverTypeName);
Expand Down Expand Up @@ -71,6 +72,21 @@ private EnvironmentManager()
}
}

public event EventHandler<DriverStartingEventArgs> DriverStarting;

public static EnvironmentManager Instance
{
get
{
if (instance == null)
{
instance = new EnvironmentManager();
}

return instance;
}
}

public Browser Browser
{
get { return browser; }
Expand Down Expand Up @@ -110,6 +126,14 @@ public string RemoteCapabilities
get { return remoteCapabilities; }
}

public UrlBuilder UrlBuilder
{
get
{
return urlBuilder;
}
}

public IWebDriver GetCurrentDriver()
{
if (driver != null)
Expand Down Expand Up @@ -148,26 +172,12 @@ public void CloseCurrentDriver()
driver = null;
}

public static EnvironmentManager Instance
protected void OnDriverStarting(object sender, DriverStartingEventArgs e)
{
get
if (this.DriverStarting != null)
{
if (instance == null)
{
instance = new EnvironmentManager();
}

return instance;
this.DriverStarting(sender, e);
}
}

public UrlBuilder UrlBuilder
{
get
{
return urlBuilder;
}
}

}
}
19 changes: 19 additions & 0 deletions dotnet/test/common/Environment/UrlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ public string WhereElseIs(string page)
return location;
}

public string WhereIsViaNonLoopbackAddress(string page)
{
string hostNameAsIPAddress = "127.0.0.1";
IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress address in addresses)
{
if (address.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(address))
{
hostNameAsIPAddress = address.ToString();
break;
}
}

string location = string.Empty;
location = "http://" + hostNameAsIPAddress + ":" + port + "/" + path + "/" + page;

return location;
}

public string WhereIsSecure(string page)
{
string location = string.Empty;
Expand Down
Loading

0 comments on commit 32e343f

Please sign in to comment.