From 7e8d48cd803076dabb11c83b75f00ea3503388e0 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Wed, 6 Sep 2017 11:46:46 +0100 Subject: [PATCH] On AppVeyor, must use Firefox (not Edge) for Selenium tests --- test/Templates.Test/Helpers/AspNetProcess.cs | 5 +-- .../Helpers/TemplateTestBase.cs | 22 ---------- .../Helpers/WebDriverFactory.cs | 42 +++++++++++++++++++ test/Templates.Test/SpaTemplateTest.cs | 2 +- test/Templates.Test/Templates.Test.csproj | 5 ++- 5 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 test/Templates.Test/Helpers/WebDriverFactory.cs diff --git a/test/Templates.Test/Helpers/AspNetProcess.cs b/test/Templates.Test/Helpers/AspNetProcess.cs index 44127dad19..6422453455 100644 --- a/test/Templates.Test/Helpers/AspNetProcess.cs +++ b/test/Templates.Test/Helpers/AspNetProcess.cs @@ -1,12 +1,10 @@ using OpenQA.Selenium; -using OpenQA.Selenium.Edge; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; -using System.Reflection; using Xunit; namespace Templates.Test.Helpers @@ -73,8 +71,7 @@ namespace Templates.Test.Helpers public IWebDriver VisitInBrowser() { - var driver = new EdgeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); - driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1); + var driver = WebDriverFactory.CreateWebDriver(); driver.Navigate().GoToUrl(_listeningUri); return driver; } diff --git a/test/Templates.Test/Helpers/TemplateTestBase.cs b/test/Templates.Test/Helpers/TemplateTestBase.cs index c98c2f8652..9efbce8989 100644 --- a/test/Templates.Test/Helpers/TemplateTestBase.cs +++ b/test/Templates.Test/Helpers/TemplateTestBase.cs @@ -1,8 +1,6 @@ using System; using System.IO; using System.Reflection; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; using System.Threading; using Templates.Test.Helpers; using Xunit; @@ -14,12 +12,6 @@ namespace Templates.Test protected string ProjectName { get; set; } protected string TemplateOutputDir { get; private set; } - // The tests use EdgeDriver because InternetExplorerDriver is flaky and - // ChromeDriver is very slow (plus, Chrome might not be on CI machines). - // This limits us to running the browser automation tests on Windows 10+. - protected bool EnableBrowserAutomationTesting - => OSSupportsEdge(); - static TemplateTestBase() { TemplatePackageInstaller.ReinstallTemplatePackages(); @@ -147,19 +139,5 @@ namespace Templates.Test } } } - - private static int GetWindowsVersion() - { - var osDescription = RuntimeInformation.OSDescription; - var windowsVersion = Regex.Match(osDescription, "^Microsoft Windows (\\d+)\\..*"); - return windowsVersion.Success ? int.Parse(windowsVersion.Groups[1].Value) : -1; - } - - private static bool OSSupportsEdge() - { - var windowsVersion = GetWindowsVersion(); - return (windowsVersion >= 10 && windowsVersion < 2000) - || (windowsVersion >= 2016); - } } } diff --git a/test/Templates.Test/Helpers/WebDriverFactory.cs b/test/Templates.Test/Helpers/WebDriverFactory.cs new file mode 100644 index 0000000000..5ae0ec2145 --- /dev/null +++ b/test/Templates.Test/Helpers/WebDriverFactory.cs @@ -0,0 +1,42 @@ +using OpenQA.Selenium; +using OpenQA.Selenium.Edge; +using OpenQA.Selenium.Firefox; +using System; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; + +namespace Templates.Test.Helpers +{ + public static class WebDriverFactory + { + public static bool HostSupportsBrowserAutomation + => IsAppVeyor || OSSupportsEdge(); + + private static bool IsAppVeyor + => Environment.GetEnvironmentVariables().Contains("APPVEYOR"); + + public static IWebDriver CreateWebDriver() + { + // Where possible, it's preferable to use Edge because it's + // far faster to automate than Chrome/Firefox. But on AppVeyor + // only Firefox is available. + var result = IsAppVeyor ? new FirefoxDriver() : (IWebDriver)new EdgeDriver(); + result.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1); + return result; + } + + private static int GetWindowsVersion() + { + var osDescription = RuntimeInformation.OSDescription; + var windowsVersion = Regex.Match(osDescription, "^Microsoft Windows (\\d+)\\..*"); + return windowsVersion.Success ? int.Parse(windowsVersion.Groups[1].Value) : -1; + } + + private static bool OSSupportsEdge() + { + var windowsVersion = GetWindowsVersion(); + return (windowsVersion >= 10 && windowsVersion < 2000) + || (windowsVersion >= 2016); + } + } +} diff --git a/test/Templates.Test/SpaTemplateTest.cs b/test/Templates.Test/SpaTemplateTest.cs index ba84e043b1..bc12b0ec39 100644 --- a/test/Templates.Test/SpaTemplateTest.cs +++ b/test/Templates.Test/SpaTemplateTest.cs @@ -26,7 +26,7 @@ namespace Templates.Test { aspNetProcess.AssertOk("/"); - if (EnableBrowserAutomationTesting) + if (WebDriverFactory.HostSupportsBrowserAutomation) { Console.WriteLine("Starting browser automation tests..."); diff --git a/test/Templates.Test/Templates.Test.csproj b/test/Templates.Test/Templates.Test.csproj index e6465bafd3..cd8306c6d1 100644 --- a/test/Templates.Test/Templates.Test.csproj +++ b/test/Templates.Test/Templates.Test.csproj @@ -11,9 +11,10 @@ - - + + +