From 2f8f600adcf4340a74e4019ad39541e03e5c11ba Mon Sep 17 00:00:00 2001 From: Andrey Brovko Date: Fri, 17 Sep 2021 10:52:52 +0300 Subject: [PATCH 1/3] Move RemoteWebDriver to context --- .../Bootstrapper.cs | 20 +++--- .../{BrowserRunner.cs => BrowserContext.cs} | 8 ++- .../Services/DriverService.cs | 71 +++++++++---------- 3 files changed, 46 insertions(+), 53 deletions(-) rename src/Behavioral.Automation/Services/{BrowserRunner.cs => BrowserContext.cs} (95%) diff --git a/Behavioral.Automation.DemoBindings/Bootstrapper.cs b/Behavioral.Automation.DemoBindings/Bootstrapper.cs index 5f99f5fc..6a560fc5 100644 --- a/Behavioral.Automation.DemoBindings/Bootstrapper.cs +++ b/Behavioral.Automation.DemoBindings/Bootstrapper.cs @@ -10,26 +10,21 @@ public class Bootstrapper { private readonly IObjectContainer _objectContainer; private readonly ITestRunner _runner; - private static DemoTestServicesBuilder _servicesBuilder; - private static readonly BrowserRunner BrowserRunner = new BrowserRunner(); + private readonly DemoTestServicesBuilder _servicesBuilder; + private readonly BrowserContext _browserContext; - public Bootstrapper(IObjectContainer objectContainer, ITestRunner runner) + public Bootstrapper(IObjectContainer objectContainer, ITestRunner runner, BrowserContext browserContext) { _objectContainer = objectContainer; _runner = runner; + _browserContext = browserContext; _servicesBuilder = new DemoTestServicesBuilder(objectContainer, new TestServicesBuilder(_objectContainer)); } - [BeforeTestRun] - public static void OpenBrowser() + [AfterScenario] + public void CloseBrowser() { - BrowserRunner.OpenChrome(); - } - - [AfterTestRun] - public static void CloseBrowser() - { - BrowserRunner.CloseBrowser(); + _browserContext.CloseBrowser(); } [BeforeScenario(Order = 0)] @@ -39,6 +34,7 @@ public void Bootstrap() _objectContainer.RegisterTypeAs(); _servicesBuilder.Build(); Assert.SetConsumer(_objectContainer.Resolve()); + _browserContext.OpenChrome(); } } } diff --git a/src/Behavioral.Automation/Services/BrowserRunner.cs b/src/Behavioral.Automation/Services/BrowserContext.cs similarity index 95% rename from src/Behavioral.Automation/Services/BrowserRunner.cs rename to src/Behavioral.Automation/Services/BrowserContext.cs index 2a72fd9c..020b7d1d 100644 --- a/src/Behavioral.Automation/Services/BrowserRunner.cs +++ b/src/Behavioral.Automation/Services/BrowserContext.cs @@ -7,16 +7,18 @@ namespace Behavioral.Automation.Services { - public class BrowserRunner + public class BrowserContext { + public RemoteWebDriver Driver; + public void OpenBrowser([NotNull] RemoteWebDriver driver) { - DriverService.Driver = driver; + Driver = driver; } public void CloseBrowser() { - DriverService.Driver.Dispose(); + Driver.Dispose(); } public void OpenChrome(ChromeOptions options = null) diff --git a/src/Behavioral.Automation/Services/DriverService.cs b/src/Behavioral.Automation/Services/DriverService.cs index d3736d53..996ae801 100644 --- a/src/Behavioral.Automation/Services/DriverService.cs +++ b/src/Behavioral.Automation/Services/DriverService.cs @@ -1,16 +1,14 @@ using System; using System.Collections.ObjectModel; -using System.Linq; using System.Drawing; +using System.Linq; using System.Text.RegularExpressions; +using System.Threading; using Behavioral.Automation.Services.Mapping.Contract; using JetBrains.Annotations; +using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Interactions; -using OpenQA.Selenium.Remote; -using OpenQA.Selenium.Support.UI; -using System.Threading; -using NUnit.Framework; namespace Behavioral.Automation.Services { @@ -19,28 +17,25 @@ public sealed class DriverService : IDriverService { [NotNull] private readonly IScopeContextManager _scopeContextManager; - - public static RemoteWebDriver Driver; - - public DriverService([NotNull] IScopeContextManager scopeContextManager) + private readonly BrowserContext _browserContext; + + public DriverService([NotNull] IScopeContextManager scopeContextManager, BrowserContext browserContext) { _scopeContextManager = scopeContextManager; + _browserContext = browserContext; } - private WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(4)); - private ReadOnlyCollection WindowHandles => Driver.WindowHandles; - private string SearchAttribute = ConfigServiceBase.SearchAttribute; - public string Title => Driver.Title; + public string Title => _browserContext.Driver.Title; - public string CurrentUrl => Driver.Url; + public string CurrentUrl => _browserContext.Driver.Url; public IWebElement FindElement(string id) { try { - return Driver.FindElement(By.XPath($"//*[@{SearchAttribute}='{id}']")); + return _browserContext.Driver.FindElement(By.XPath($"//*[@{SearchAttribute}='{id}']")); } catch (NoSuchElementException) { @@ -64,7 +59,7 @@ public IWebElement FindElementByXpath(string path) { try { - return Driver.FindElement(By.XPath(path)); + return _browserContext.Driver.FindElement(By.XPath(path)); } catch (NoSuchElementException) { @@ -74,56 +69,56 @@ public IWebElement FindElementByXpath(string path) public ReadOnlyCollection FindElements(string id) { - return Driver.FindElements(By.XPath($"//*[@{SearchAttribute}='{id}']")); + return _browserContext.Driver.FindElements(By.XPath($"//*[@{SearchAttribute}='{id}']")); } public ReadOnlyCollection FindElementsByXpath(string path) { - return Driver.FindElements(By.XPath(path)); + return _browserContext.Driver.FindElements(By.XPath(path)); } public void ScrollTo(IWebElement element) { var scrollElementIntoMiddle = "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);" - + "var elementTop = arguments[0].getBoundingClientRect().top;" - + "window.scrollBy(0, elementTop-(viewPortHeight/2));"; + + "var elementTop = arguments[0].getBoundingClientRect().top;" + + "window.scrollBy(0, elementTop-(viewPortHeight/2));"; - Driver.ExecuteScript(scrollElementIntoMiddle, element); - Actions actions = new Actions(Driver); + _browserContext.Driver.ExecuteScript(scrollElementIntoMiddle, element); + Actions actions = new Actions(_browserContext.Driver); actions.MoveToElement(element); actions.Perform(); } public void MouseClick() { - Actions actions = new Actions(Driver); + Actions actions = new Actions(_browserContext.Driver); actions.Click(); actions.Perform(); } public void CloseActiveElement() { - var element = Driver.SwitchTo().ActiveElement(); + var element = _browserContext.Driver.SwitchTo().ActiveElement(); element.SendKeys(Keys.Escape); } public void NavigateToRelativeUrl(string url) { var uri = GetUriFromRelativePath(url); - Driver.Navigate().GoToUrl(uri); + _browserContext.Driver.Navigate().GoToUrl(uri); _scopeContextManager.SwitchPage(uri); } public void SwitchToTheLastWindow() { - var handle = Driver.WindowHandles.Last(); - Driver.SwitchTo().Window(handle); + var handle = _browserContext.Driver.WindowHandles.Last(); + _browserContext.Driver.SwitchTo().Window(handle); } public void SwitchToTheFirstWindow() { - var handle = Driver.WindowHandles.First(); - Driver.SwitchTo().Window(handle); + var handle = _browserContext.Driver.WindowHandles.First(); + _browserContext.Driver.SwitchTo().Window(handle); } private Uri GetUriFromRelativePath(string url) @@ -133,51 +128,51 @@ private Uri GetUriFromRelativePath(string url) public void RemoveFocusFromActiveElement() { - Driver.ExecuteScript("document.activeElement.blur()"); + _browserContext.Driver.ExecuteScript("document.activeElement.blur()"); } public void DebugDumpPage() { - var page = Driver.PageSource; + var page = _browserContext.Driver.PageSource; Console.WriteLine(page); } public void Refresh() { - Driver.Navigate().Refresh(); + _browserContext.Driver.Navigate().Refresh(); } public void Navigate(string url) { - Driver.Navigate().GoToUrl(url); + _browserContext.Driver.Navigate().GoToUrl(url); var uri = new Uri(url); _scopeContextManager.SwitchPage(uri); } public void SwitchToLastWindow() { - Driver.SwitchTo().Window(Driver.WindowHandles.Last()); + _browserContext.Driver.SwitchTo().Window(_browserContext.Driver.WindowHandles.Last()); } public void ResizeWindow(int Height, int Width) { - Driver.Manage().Window.Size = new Size(Width, Height); + _browserContext.Driver.Manage().Window.Size = new Size(Width, Height); } public string MakeScreenShot() { var fileName = Regex.Replace(TestContext.CurrentContext.Test.Name, @"(\\|\"")", string.Empty) + ".png"; - Driver.GetScreenshot().SaveAsFile(fileName, ScreenshotImageFormat.Png); + _browserContext.Driver.GetScreenshot().SaveAsFile(fileName, ScreenshotImageFormat.Png); return fileName; } public void ScrollElementTo(IWebElement element, int offset) { - Driver.ExecuteScript(@" + _browserContext.Driver.ExecuteScript(@" var element = arguments[0]; var offset = arguments[1]; element.scrollTo({top: offset, behavior: 'smooth'});", element, offset); Thread.Sleep(1000); } } -} +} \ No newline at end of file From 3cfa7ec9b4f81575d91f91d34c97cba6111d10b7 Mon Sep 17 00:00:00 2001 From: Sergey Ruzin Date: Fri, 17 Sep 2021 13:16:40 +0300 Subject: [PATCH 2/3] added driver field to driver service --- .../Properties/AssemblyInfo.cs | 6 +++ .../Services/BrowserContext.cs | 1 - .../Services/DriverService.cs | 51 ++++++++++--------- .../Services/IDriverService.cs | 3 ++ 4 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 Behavioral.Automation.DemoScenarios/Properties/AssemblyInfo.cs diff --git a/Behavioral.Automation.DemoScenarios/Properties/AssemblyInfo.cs b/Behavioral.Automation.DemoScenarios/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..98c5f084 --- /dev/null +++ b/Behavioral.Automation.DemoScenarios/Properties/AssemblyInfo.cs @@ -0,0 +1,6 @@ +using NUnit.Framework; + +[assembly: Parallelizable(ParallelScope.Fixtures)] + +//edit if more threads are needed +[assembly: LevelOfParallelism(1)] \ No newline at end of file diff --git a/src/Behavioral.Automation/Services/BrowserContext.cs b/src/Behavioral.Automation/Services/BrowserContext.cs index 020b7d1d..144ad10a 100644 --- a/src/Behavioral.Automation/Services/BrowserContext.cs +++ b/src/Behavioral.Automation/Services/BrowserContext.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using JetBrains.Annotations; -using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Remote; diff --git a/src/Behavioral.Automation/Services/DriverService.cs b/src/Behavioral.Automation/Services/DriverService.cs index 996ae801..7902045e 100644 --- a/src/Behavioral.Automation/Services/DriverService.cs +++ b/src/Behavioral.Automation/Services/DriverService.cs @@ -9,6 +9,7 @@ using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Interactions; +using OpenQA.Selenium.Remote; namespace Behavioral.Automation.Services { @@ -18,24 +19,26 @@ public sealed class DriverService : IDriverService [NotNull] private readonly IScopeContextManager _scopeContextManager; private readonly BrowserContext _browserContext; - + public DriverService([NotNull] IScopeContextManager scopeContextManager, BrowserContext browserContext) { _scopeContextManager = scopeContextManager; _browserContext = browserContext; } - private string SearchAttribute = ConfigServiceBase.SearchAttribute; + public RemoteWebDriver Driver => _browserContext.Driver; + + private readonly string SearchAttribute = ConfigServiceBase.SearchAttribute; - public string Title => _browserContext.Driver.Title; + public string Title => Driver.Title; - public string CurrentUrl => _browserContext.Driver.Url; + public string CurrentUrl => Driver.Url; public IWebElement FindElement(string id) { try { - return _browserContext.Driver.FindElement(By.XPath($"//*[@{SearchAttribute}='{id}']")); + return Driver.FindElement(By.XPath($"//*[@{SearchAttribute}='{id}']")); } catch (NoSuchElementException) { @@ -59,7 +62,7 @@ public IWebElement FindElementByXpath(string path) { try { - return _browserContext.Driver.FindElement(By.XPath(path)); + return Driver.FindElement(By.XPath(path)); } catch (NoSuchElementException) { @@ -69,12 +72,12 @@ public IWebElement FindElementByXpath(string path) public ReadOnlyCollection FindElements(string id) { - return _browserContext.Driver.FindElements(By.XPath($"//*[@{SearchAttribute}='{id}']")); + return Driver.FindElements(By.XPath($"//*[@{SearchAttribute}='{id}']")); } public ReadOnlyCollection FindElementsByXpath(string path) { - return _browserContext.Driver.FindElements(By.XPath(path)); + return Driver.FindElements(By.XPath(path)); } public void ScrollTo(IWebElement element) @@ -83,42 +86,42 @@ public void ScrollTo(IWebElement element) + "var elementTop = arguments[0].getBoundingClientRect().top;" + "window.scrollBy(0, elementTop-(viewPortHeight/2));"; - _browserContext.Driver.ExecuteScript(scrollElementIntoMiddle, element); - Actions actions = new Actions(_browserContext.Driver); + Driver.ExecuteScript(scrollElementIntoMiddle, element); + Actions actions = new Actions(Driver); actions.MoveToElement(element); actions.Perform(); } public void MouseClick() { - Actions actions = new Actions(_browserContext.Driver); + Actions actions = new Actions(Driver); actions.Click(); actions.Perform(); } public void CloseActiveElement() { - var element = _browserContext.Driver.SwitchTo().ActiveElement(); + var element = Driver.SwitchTo().ActiveElement(); element.SendKeys(Keys.Escape); } public void NavigateToRelativeUrl(string url) { var uri = GetUriFromRelativePath(url); - _browserContext.Driver.Navigate().GoToUrl(uri); + Driver.Navigate().GoToUrl(uri); _scopeContextManager.SwitchPage(uri); } public void SwitchToTheLastWindow() { - var handle = _browserContext.Driver.WindowHandles.Last(); - _browserContext.Driver.SwitchTo().Window(handle); + var handle = Driver.WindowHandles.Last(); + Driver.SwitchTo().Window(handle); } public void SwitchToTheFirstWindow() { - var handle = _browserContext.Driver.WindowHandles.First(); - _browserContext.Driver.SwitchTo().Window(handle); + var handle = Driver.WindowHandles.First(); + Driver.SwitchTo().Window(handle); } private Uri GetUriFromRelativePath(string url) @@ -128,7 +131,7 @@ private Uri GetUriFromRelativePath(string url) public void RemoveFocusFromActiveElement() { - _browserContext.Driver.ExecuteScript("document.activeElement.blur()"); + Driver.ExecuteScript("document.activeElement.blur()"); } public void DebugDumpPage() @@ -139,30 +142,30 @@ public void DebugDumpPage() public void Refresh() { - _browserContext.Driver.Navigate().Refresh(); + Driver.Navigate().Refresh(); } public void Navigate(string url) { - _browserContext.Driver.Navigate().GoToUrl(url); + Driver.Navigate().GoToUrl(url); var uri = new Uri(url); _scopeContextManager.SwitchPage(uri); } public void SwitchToLastWindow() { - _browserContext.Driver.SwitchTo().Window(_browserContext.Driver.WindowHandles.Last()); + Driver.SwitchTo().Window(_browserContext.Driver.WindowHandles.Last()); } public void ResizeWindow(int Height, int Width) - { - _browserContext.Driver.Manage().Window.Size = new Size(Width, Height); + { + Driver.Manage().Window.Size = new Size(Width, Height); } public string MakeScreenShot() { var fileName = Regex.Replace(TestContext.CurrentContext.Test.Name, @"(\\|\"")", string.Empty) + ".png"; - _browserContext.Driver.GetScreenshot().SaveAsFile(fileName, ScreenshotImageFormat.Png); + Driver.GetScreenshot().SaveAsFile(fileName, ScreenshotImageFormat.Png); return fileName; } diff --git a/src/Behavioral.Automation/Services/IDriverService.cs b/src/Behavioral.Automation/Services/IDriverService.cs index 12155aca..b280374d 100644 --- a/src/Behavioral.Automation/Services/IDriverService.cs +++ b/src/Behavioral.Automation/Services/IDriverService.cs @@ -1,11 +1,14 @@ using System.Collections.ObjectModel; using JetBrains.Annotations; using OpenQA.Selenium; +using OpenQA.Selenium.Remote; namespace Behavioral.Automation.Services { public interface IDriverService { + RemoteWebDriver Driver { get; } + string CurrentUrl { [NotNull] get; } string Title { [CanBeNull] get; } From 2c143286204a68e1dc5a36de7cd42ed089f4b361 Mon Sep 17 00:00:00 2001 From: Sergey Ruzin Date: Fri, 17 Sep 2021 13:21:05 +0300 Subject: [PATCH 3/3] Removed extra browser context calls --- src/Behavioral.Automation/Services/DriverService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Behavioral.Automation/Services/DriverService.cs b/src/Behavioral.Automation/Services/DriverService.cs index 7902045e..e11a62bc 100644 --- a/src/Behavioral.Automation/Services/DriverService.cs +++ b/src/Behavioral.Automation/Services/DriverService.cs @@ -136,7 +136,7 @@ public void RemoveFocusFromActiveElement() public void DebugDumpPage() { - var page = _browserContext.Driver.PageSource; + var page = Driver.PageSource; Console.WriteLine(page); } @@ -154,7 +154,7 @@ public void Navigate(string url) public void SwitchToLastWindow() { - Driver.SwitchTo().Window(_browserContext.Driver.WindowHandles.Last()); + Driver.SwitchTo().Window(Driver.WindowHandles.Last()); } public void ResizeWindow(int Height, int Width) @@ -171,7 +171,7 @@ public string MakeScreenShot() public void ScrollElementTo(IWebElement element, int offset) { - _browserContext.Driver.ExecuteScript(@" + Driver.ExecuteScript(@" var element = arguments[0]; var offset = arguments[1]; element.scrollTo({top: offset, behavior: 'smooth'});", element, offset);