From d13c060a4bf46a3f1b4db95f798439734b13e8aa Mon Sep 17 00:00:00 2001 From: Jim Evans Date: Sun, 11 Feb 2018 11:20:19 -0800 Subject: [PATCH] Making .NET common tests run with the `dotnet test` command --- .../test/common/Environment/DriverFactory.cs | 76 +++++++++++++++++++ .../common/Environment/EnvironmentManager.cs | 11 ++- .../common/Environment/TestEnvironment.cs | 3 + .../common/ExecutingAsyncJavascriptTest.cs | 12 ++- dotnet/test/common/GetLogsTest.cs | 16 ++-- .../test/common/WebDriver.Common.Tests.csproj | 1 + dotnet/test/common/appconfig.json | 3 +- .../test/firefox/NightlyFirefoxWebDriver.cs | 8 +- .../test/firefox/ReleaseFirefoxWebDriver.cs | 6 +- 9 files changed, 114 insertions(+), 22 deletions(-) create mode 100644 dotnet/test/common/Environment/DriverFactory.cs diff --git a/dotnet/test/common/Environment/DriverFactory.cs b/dotnet/test/common/Environment/DriverFactory.cs new file mode 100644 index 0000000000000..9b9d475170233 --- /dev/null +++ b/dotnet/test/common/Environment/DriverFactory.cs @@ -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 constructorArgTypeList = new List(); + 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; + } + } +} diff --git a/dotnet/test/common/Environment/EnvironmentManager.cs b/dotnet/test/common/Environment/EnvironmentManager.cs index 286da105e4503..42d5274c40618 100644 --- a/dotnet/test/common/Environment/EnvironmentManager.cs +++ b/dotnet/test/common/Environment/EnvironmentManager.cs @@ -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() @@ -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); @@ -66,6 +68,11 @@ public Browser Browser get { return browser; } } + public string DriverServiceDirectory + { + get { return this.driverFactory.DriverServicePath; } + } + public string CurrentDirectory { get @@ -103,7 +110,7 @@ public IWebDriver GetCurrentDriver() public IWebDriver CreateDriverInstance() { - return (IWebDriver)Activator.CreateInstance(driverType); + return driverFactory.CreateDriver(driverType); } public IWebDriver CreateFreshDriver() diff --git a/dotnet/test/common/Environment/TestEnvironment.cs b/dotnet/test/common/Environment/TestEnvironment.cs index 72c9a590f3b61..27e976bbac7a3 100644 --- a/dotnet/test/common/Environment/TestEnvironment.cs +++ b/dotnet/test/common/Environment/TestEnvironment.cs @@ -10,6 +10,9 @@ namespace OpenQA.Selenium.Environment [JsonObject] class TestEnvironment { + [JsonProperty] + public string DriverServiceLocation { get; set; } + [JsonProperty] public string ActiveDriverConfig { get; set; } diff --git a/dotnet/test/common/ExecutingAsyncJavascriptTest.cs b/dotnet/test/common/ExecutingAsyncJavascriptTest.cs index 7e354e7a25c85..b4e9e686e503b 100644 --- a/dotnet/test/common/ExecutingAsyncJavascriptTest.cs +++ b/dotnet/test/common/ExecutingAsyncJavascriptTest.cs @@ -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() @@ -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] diff --git a/dotnet/test/common/GetLogsTest.cs b/dotnet/test/common/GetLogsTest.cs index 243b292bf3fe0..08942f6ca9cbc 100644 --- a/dotnet/test/common/GetLogsTest.cs +++ b/dotnet/test/common/GetLogsTest.cs @@ -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 { @@ -95,19 +96,12 @@ public void TurningOffLogShouldMeanNoLogMessages() private void CreateWebDriverWithLogging(string logType, LogLevel logLevel) { - if (TestUtilities.IsFirefox(driver)) - { - Dictionary firefoxLogs = new Dictionary(); - 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; diff --git a/dotnet/test/common/WebDriver.Common.Tests.csproj b/dotnet/test/common/WebDriver.Common.Tests.csproj index 4d6eb7ce853cc..1807c07f4283a 100644 --- a/dotnet/test/common/WebDriver.Common.Tests.csproj +++ b/dotnet/test/common/WebDriver.Common.Tests.csproj @@ -25,6 +25,7 @@ + diff --git a/dotnet/test/common/appconfig.json b/dotnet/test/common/appconfig.json index ba1f95b40fb2d..fae1753ea0e89 100644 --- a/dotnet/test/common/appconfig.json +++ b/dotnet/test/common/appconfig.json @@ -1,4 +1,5 @@ { + "DriverServiceLocation": "C:\\Projects\\WebDriverServers", "ActiveDriverConfig": "Chrome", "ActiveWebsiteConfig": "Default", "WebsiteConfigs": { @@ -65,7 +66,7 @@ "AssemblyName": "WebDriver.Remote.Tests", "BrowserValue": "Remote", "RemoteCapabilities": "internet explorer", - "AutoStartRemoteServer": true + "AutoStartRemoteServer": true } } } diff --git a/dotnet/test/firefox/NightlyFirefoxWebDriver.cs b/dotnet/test/firefox/NightlyFirefoxWebDriver.cs index 9a1f845fcbda7..b997b535a9358 100644 --- a/dotnet/test/firefox/NightlyFirefoxWebDriver.cs +++ b/dotnet/test/firefox/NightlyFirefoxWebDriver.cs @@ -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) { } } diff --git a/dotnet/test/firefox/ReleaseFirefoxWebDriver.cs b/dotnet/test/firefox/ReleaseFirefoxWebDriver.cs index b1eb148f0d74b..2bca25ff47e8f 100644 --- a/dotnet/test/firefox/ReleaseFirefoxWebDriver.cs +++ b/dotnet/test/firefox/ReleaseFirefoxWebDriver.cs @@ -1,3 +1,5 @@ +using OpenQA.Selenium.Remote; + namespace OpenQA.Selenium.Firefox { // This is a simple wrapper class to create a FirefoxDriver that @@ -5,8 +7,8 @@ namespace OpenQA.Selenium.Firefox // 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) { } }