From d69369433250e9fd1e0efdc143f44520f347f6a9 Mon Sep 17 00:00:00 2001 From: Rob Moore Date: Wed, 18 Feb 2015 17:25:45 +0800 Subject: [PATCH] Fixed a problem with running IE on a retina screen (http://stackoverflow.com/questions/12034969/internetexplorerdriver-zoom-level-error) Also fixed a problem where an error after resolving a driver means the driver and browser didn't get tied to the parent process --- .../Configuration/ProcessLifecycleTests.cs | 13 +++--- .../Configuration/BrowserFactory.cs | 12 +++--- .../Configuration/SelenoHost.cs | 11 +++-- .../Configuration/WebDriverBuilder.cs | 42 +++++++++++-------- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/TestStack.Seleno.AcceptanceTests/Configuration/ProcessLifecycleTests.cs b/src/TestStack.Seleno.AcceptanceTests/Configuration/ProcessLifecycleTests.cs index 289353c7..5cbe4257 100644 --- a/src/TestStack.Seleno.AcceptanceTests/Configuration/ProcessLifecycleTests.cs +++ b/src/TestStack.Seleno.AcceptanceTests/Configuration/ProcessLifecycleTests.cs @@ -24,13 +24,14 @@ class ProcessLifecycleTests public void Closing_SelenoHost_should_close_child_browser(string driverName) { Process.GetProcessesByName(driverName).ForEach(StopProcess); - var selenoHost = new SelenoHost(); - Func driver = GetBrowserFactory(driverName); - selenoHost.Run("TestStack.Seleno.AcceptanceTests.Web", 12346, - c => c.WithRemoteWebDriver(driver)); - Process.GetProcessesByName(driverName).Length.Should().Be(1); - selenoHost.Dispose(); + using (var selenoHost = new SelenoHost()) + { + Func driver = GetBrowserFactory(driverName); + selenoHost.Run("TestStack.Seleno.AcceptanceTests.Web", 12346, + c => c.WithRemoteWebDriver(driver)); + Process.GetProcessesByName(driverName).Length.Should().Be(1); + } Process.GetProcessesByName(driverName).Should().BeEmpty(); } diff --git a/src/TestStack.Seleno/Configuration/BrowserFactory.cs b/src/TestStack.Seleno/Configuration/BrowserFactory.cs index a92cb8cd..37448b5b 100644 --- a/src/TestStack.Seleno/Configuration/BrowserFactory.cs +++ b/src/TestStack.Seleno/Configuration/BrowserFactory.cs @@ -71,7 +71,7 @@ public static ChromeDriver Chrome(ChromeOptions options) public static FirefoxDriver FireFox() { return new WebDriverBuilder(() => new FirefoxDriver()) - .WithProcessName("firefox"); + .WithProcessNames("firefox"); } /// @@ -83,7 +83,7 @@ public static FirefoxDriver FireFox() public static FirefoxDriver FireFox(FirefoxProfile profile) { return new WebDriverBuilder(() => new FirefoxDriver(profile)) - .WithProcessName("firefox"); + .WithProcessNames("firefox"); } /// @@ -128,9 +128,10 @@ public static SafariDriver Safari(SafariOptions options) /// Initialised IE driver public static InternetExplorerDriver InternetExplorer() { - var options = new InternetExplorerOptions { IntroduceInstabilityByIgnoringProtectedModeSettings = true }; + var options = new InternetExplorerOptions { IntroduceInstabilityByIgnoringProtectedModeSettings = true, IgnoreZoomLevel = true}; return new WebDriverBuilder(() => new InternetExplorerDriver(options)) - .WithFileName("IEDriverServer.exe"); + .WithFileName("IEDriverServer.exe") + .WithProcessNames("IEDriverServer", "iexplore"); } /// @@ -142,7 +143,8 @@ public static InternetExplorerDriver InternetExplorer() public static InternetExplorerDriver InternetExplorer(InternetExplorerOptions options) { return new WebDriverBuilder(() => new InternetExplorerDriver(options)) - .WithFileName("IEDriverServer.exe"); + .WithFileName("IEDriverServer.exe") + .WithProcessNames("IEDriverServer", "iexplore"); } } diff --git a/src/TestStack.Seleno/Configuration/SelenoHost.cs b/src/TestStack.Seleno/Configuration/SelenoHost.cs index d4506f2b..c711477f 100644 --- a/src/TestStack.Seleno/Configuration/SelenoHost.cs +++ b/src/TestStack.Seleno/Configuration/SelenoHost.cs @@ -159,12 +159,17 @@ private void ThrowIfHostNotInitialised() public void Dispose() { - Application.Logger.Info("Starting SelenoHost Dispose"); + if (Application != null) + Application.Logger.Info("Starting SelenoHost Dispose"); // removing unload handler as it's being handled here AppDomain.CurrentDomain.DomainUnload -= CurrentDomainDomainUnload; - Application.Dispose(); - Application.Logger.Info("SelenoHost Disposed"); + + if (Application != null) + { + Application.Dispose(); + Application.Logger.Info("SelenoHost Disposed"); + } } } } \ No newline at end of file diff --git a/src/TestStack.Seleno/Configuration/WebDriverBuilder.cs b/src/TestStack.Seleno/Configuration/WebDriverBuilder.cs index 9efceeab..f574828e 100644 --- a/src/TestStack.Seleno/Configuration/WebDriverBuilder.cs +++ b/src/TestStack.Seleno/Configuration/WebDriverBuilder.cs @@ -1,9 +1,9 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; +using System.Reflection.Emit; using Holf.AllForOne; using OpenQA.Selenium; @@ -12,7 +12,7 @@ namespace TestStack.Seleno.Configuration internal class WebDriverBuilder where T : IWebDriver { private readonly Func _factory; - private string _processName; + private string[] _processNames; private string _fileName; public WebDriverBuilder(Func factory) @@ -26,9 +26,9 @@ public WebDriverBuilder WithFileName(string fileName) return this; } - public WebDriverBuilder WithProcessName(string processName) + public WebDriverBuilder WithProcessNames(params string[] processNames) { - _processName = processName; + _processNames = processNames; return this; } @@ -55,7 +55,7 @@ private static void EnsureFileExists(string resourceFileName) // Find any assembly with the desired executable embedded in it // http://bloggingabout.net/blogs/vagif/archive/2010/07/02/net-4-0-and-notsupportedexception-complaining-about-dynamic-assemblies.aspx var assembly = AppDomain.CurrentDomain.GetAssemblies() - .Where(a => !(a is System.Reflection.Emit.AssemblyBuilder)) + .Where(a => !(a is AssemblyBuilder)) .Where(a => a.GetType().FullName != "System.Reflection.Emit.InternalAssemblyBuilder") .Where(a => !a.GlobalAssemblyCache) .FirstOrDefault(a => a @@ -79,22 +79,28 @@ private static void EnsureFileExists(string resourceFileName) private T CreateWebDriver() { - var processName = _processName ?? _fileName.Replace(@".exe", ""); + var processNames = _processNames ?? new [] {_fileName.Replace(@".exe", "")}; - IEnumerable pidsBefore = Process - .GetProcessesByName(processName) - .Select(p => p.Id); + var pidsBefore = processNames + .SelectMany(Process.GetProcessesByName) + .Select(p => p.Id) + .ToArray(); - var driver = _factory(); - - IEnumerable pidsAfter = Process - .GetProcessesByName(processName) - .Select(p => p.Id); - - IEnumerable newPids = pidsAfter.Except(pidsBefore); - foreach (int pid in newPids) + IWebDriver driver; + try + { + driver = _factory(); + } + finally { - Process.GetProcessById(pid).TieLifecycleToParentProcess(); + var pidsAfter = processNames + .SelectMany(Process.GetProcessesByName) + .Select(p => p.Id) + .ToArray(); + + var newPids = pidsAfter.Except(pidsBefore); + foreach (var pid in newPids) + Process.GetProcessById(pid).TieLifecycleToParentProcess(); } return (T)driver;