Skip to content

Commit

Permalink
Making .NET common tests run with the dotnet test command
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Feb 11, 2018
1 parent b1a7d4d commit d13c060
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 22 deletions.
76 changes: 76 additions & 0 deletions dotnet/test/common/Environment/DriverFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using NUnit.Framework;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace OpenQA.Selenium.Environment
{
public class DriverFactory
{
string driverPath;

public DriverFactory(string driverPath)
{
if (string.IsNullOrEmpty(driverPath))
{
this.driverPath = TestContext.CurrentContext.TestDirectory;
}
else
{
this.driverPath = driverPath;
}
}

public string DriverServicePath
{
get { return this.driverPath; }
}

public IWebDriver CreateDriver(Type driverType)
{
List<Type> constructorArgTypeList = new List<Type>();
IWebDriver driver = null;
if (typeof(ChromeDriver).IsAssignableFrom(driverType))
{
ChromeDriverService service = ChromeDriverService.CreateDefaultService(this.driverPath);
constructorArgTypeList.Add(typeof(ChromeDriverService));
ConstructorInfo ctorInfo = driverType.GetConstructor(constructorArgTypeList.ToArray());
return (IWebDriver)ctorInfo.Invoke(new object[] { service });
}

if (typeof(InternetExplorerDriver).IsAssignableFrom(driverType))
{
InternetExplorerDriverService service = InternetExplorerDriverService.CreateDefaultService(this.driverPath);
constructorArgTypeList.Add(typeof(InternetExplorerDriverService));
ConstructorInfo ctorInfo = driverType.GetConstructor(constructorArgTypeList.ToArray());
return (IWebDriver)ctorInfo.Invoke(new object[] { service });
}

if (typeof(EdgeDriver).IsAssignableFrom(driverType))
{
EdgeDriverService service = EdgeDriverService.CreateDefaultService(this.driverPath);
constructorArgTypeList.Add(typeof(EdgeDriverService));
ConstructorInfo ctorInfo = driverType.GetConstructor(constructorArgTypeList.ToArray());
return (IWebDriver)ctorInfo.Invoke(new object[] { service });
}

if (typeof(FirefoxDriver).IsAssignableFrom(driverType))
{
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(this.driverPath);
constructorArgTypeList.Add(typeof(FirefoxDriverService));
ConstructorInfo ctorInfo = driverType.GetConstructor(constructorArgTypeList.ToArray());
return (IWebDriver)ctorInfo.Invoke(new object[] { service });
}

driver = (IWebDriver)Activator.CreateInstance(driverType);
return driver;
}
}
}
11 changes: 9 additions & 2 deletions dotnet/test/common/Environment/EnvironmentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class EnvironmentManager
private IWebDriver driver;
private UrlBuilder urlBuilder;
private TestWebServer webServer;
RemoteSeleniumServer remoteServer;
private DriverFactory driverFactory;
private RemoteSeleniumServer remoteServer;
private string remoteCapabilities;

private EnvironmentManager()
Expand All @@ -26,6 +27,7 @@ private EnvironmentManager()
string activeWebsiteConfig = TestContext.Parameters.Get("ActiveWebsiteConfig", env.ActiveWebsiteConfig);
DriverConfig driverConfig = env.DriverConfigs[activeDriverConfig];
WebsiteConfig websiteConfig = env.WebSiteConfigs[activeWebsiteConfig];
this.driverFactory = new DriverFactory(env.DriverServiceLocation);

Assembly driverAssembly = Assembly.Load(driverConfig.AssemblyName);
driverType = driverAssembly.GetType(driverConfig.DriverTypeName);
Expand Down Expand Up @@ -66,6 +68,11 @@ public Browser Browser
get { return browser; }
}

public string DriverServiceDirectory
{
get { return this.driverFactory.DriverServicePath; }
}

public string CurrentDirectory
{
get
Expand Down Expand Up @@ -103,7 +110,7 @@ public IWebDriver GetCurrentDriver()

public IWebDriver CreateDriverInstance()
{
return (IWebDriver)Activator.CreateInstance(driverType);
return driverFactory.CreateDriver(driverType);
}

public IWebDriver CreateFreshDriver()
Expand Down
3 changes: 3 additions & 0 deletions dotnet/test/common/Environment/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace OpenQA.Selenium.Environment
[JsonObject]
class TestEnvironment
{
[JsonProperty]
public string DriverServiceLocation { get; set; }

[JsonProperty]
public string ActiveDriverConfig { get; set; }

Expand Down
12 changes: 9 additions & 3 deletions dotnet/test/common/ExecutingAsyncJavascriptTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace OpenQA.Selenium
public class ExecutingAsyncJavascriptTest : DriverTestFixture
{
private IJavaScriptExecutor executor;
private TimeSpan originalTimeout;
private TimeSpan originalTimeout = TimeSpan.MinValue;

[SetUp]
public void SetUpEnvironment()
Expand All @@ -18,8 +18,14 @@ public void SetUpEnvironment()
executor = (IJavaScriptExecutor)driver;
}

originalTimeout = driver.Manage().Timeouts().AsynchronousJavaScript;
driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(1);
try
{
originalTimeout = driver.Manage().Timeouts().AsynchronousJavaScript;
driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(1);
}
catch (NotImplementedException)
{
}
}

[TearDown]
Expand Down
16 changes: 5 additions & 11 deletions dotnet/test/common/GetLogsTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.Generic;
using System.Collections.Generic;
using NUnit.Framework;
using System.Collections.ObjectModel;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Environment;

namespace OpenQA.Selenium
{
Expand Down Expand Up @@ -95,19 +96,12 @@ public void TurningOffLogShouldMeanNoLogMessages()

private void CreateWebDriverWithLogging(string logType, LogLevel logLevel)
{
if (TestUtilities.IsFirefox(driver))
{
Dictionary<string, object> firefoxLogs = new Dictionary<string, object>();
firefoxLogs[logType] = logLevel.ToString().ToUpperInvariant();
DesiredCapabilities caps = DesiredCapabilities.Firefox();
caps.SetCapability(CapabilityType.LoggingPreferences, firefoxLogs);
localDriver = new FirefoxDriver(caps);
}
else if (TestUtilities.IsChrome(driver))
if (TestUtilities.IsChrome(driver))
{
ChromeDriverService service = ChromeDriverService.CreateDefaultService(EnvironmentManager.Instance.DriverServiceDirectory);
ChromeOptions options = new ChromeOptions();
options.SetLoggingPreference(logType, logLevel);
localDriver = new ChromeDriver(options);
localDriver = new ChromeDriver(service, options);
}

localDriver.Url = simpleTestPage;
Expand Down
1 change: 1 addition & 0 deletions dotnet/test/common/WebDriver.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</ItemGroup>

<ItemGroup>
<None Remove="BUCK" />
<None Remove="build.desc" />
<None Remove="Settings.StyleCop" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion dotnet/test/common/appconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"DriverServiceLocation": "C:\\Projects\\WebDriverServers",
"ActiveDriverConfig": "Chrome",
"ActiveWebsiteConfig": "Default",
"WebsiteConfigs": {
Expand Down Expand Up @@ -65,7 +66,7 @@
"AssemblyName": "WebDriver.Remote.Tests",
"BrowserValue": "Remote",
"RemoteCapabilities": "internet explorer",
"AutoStartRemoteServer": true
"AutoStartRemoteServer": true
}
}
}
8 changes: 5 additions & 3 deletions dotnet/test/firefox/NightlyFirefoxWebDriver.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
namespace OpenQA.Selenium.Firefox
using OpenQA.Selenium.Remote;

namespace OpenQA.Selenium.Firefox
{
// This is a simple wrapper class to create a FirefoxDriver that
// uses the Marionette implementation and has no parameters in the
// constructor.
public class NightlyFirefoxWebDriver : FirefoxDriver
{
public NightlyFirefoxWebDriver()
: base(new FirefoxOptions() { BrowserExecutableLocation = @"C:\Program Files (x86)\Nightly\firefox.exe" })
public NightlyFirefoxWebDriver(FirefoxDriverService service)
: base(service, new FirefoxOptions() { BrowserExecutableLocation = @"C:\Program Files (x86)\Nightly\firefox.exe" }, RemoteWebDriver.DefaultCommandTimeout)
{
}
}
Expand Down
6 changes: 4 additions & 2 deletions dotnet/test/firefox/ReleaseFirefoxWebDriver.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using OpenQA.Selenium.Remote;

namespace OpenQA.Selenium.Firefox
{
// This is a simple wrapper class to create a FirefoxDriver that
// uses the Marionette implementation and has no parameters in the
// constructor.
public class ReleaseFirefoxWebDriver : FirefoxDriver
{
public ReleaseFirefoxWebDriver()
: base(new FirefoxOptions() { BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\firefox.exe" })
public ReleaseFirefoxWebDriver(FirefoxDriverService service)
: base(service, new FirefoxOptions() { BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\firefox.exe" }, RemoteWebDriver.DefaultCommandTimeout)
{
}
}
Expand Down

0 comments on commit d13c060

Please sign in to comment.