From b792f7310c42641222e2983695493ff42727b6b5 Mon Sep 17 00:00:00 2001 From: Sergey Ruzin Date: Wed, 1 Dec 2021 14:20:54 +0300 Subject: [PATCH 1/2] Updated packages (including Selenium to 4 version) --- .../Behavioral.Automation.DemoBindings.csproj | 6 +++--- ...Behavioral.Automation.DemoScenarios.csproj | 10 +++++----- .../Behavioral.Automation.UnitTests.csproj | 8 ++++---- .../Behavioral.Automation.csproj | 20 +++++++++---------- .../Bindings/ListBinding.cs | 7 ------- .../FluentAssertions/Assert.cs | 2 +- .../Services/BrowserRunner.cs | 7 ++++--- .../Services/DriverService.cs | 4 ++-- .../Services/IDriverService.cs | 4 ++-- 9 files changed, 31 insertions(+), 37 deletions(-) diff --git a/Behavioral.Automation.DemoBindings/Behavioral.Automation.DemoBindings.csproj b/Behavioral.Automation.DemoBindings/Behavioral.Automation.DemoBindings.csproj index 50ad947d..de05ae0b 100644 --- a/Behavioral.Automation.DemoBindings/Behavioral.Automation.DemoBindings.csproj +++ b/Behavioral.Automation.DemoBindings/Behavioral.Automation.DemoBindings.csproj @@ -9,9 +9,9 @@ - - - + + + diff --git a/Behavioral.Automation.DemoScenarios/Behavioral.Automation.DemoScenarios.csproj b/Behavioral.Automation.DemoScenarios/Behavioral.Automation.DemoScenarios.csproj index acf72b0e..a5480234 100644 --- a/Behavioral.Automation.DemoScenarios/Behavioral.Automation.DemoScenarios.csproj +++ b/Behavioral.Automation.DemoScenarios/Behavioral.Automation.DemoScenarios.csproj @@ -9,11 +9,11 @@ - - - - - + + + + + diff --git a/src/Behavioral.Automation.UnitTests/Behavioral.Automation.UnitTests.csproj b/src/Behavioral.Automation.UnitTests/Behavioral.Automation.UnitTests.csproj index 0f970749..2569eb12 100644 --- a/src/Behavioral.Automation.UnitTests/Behavioral.Automation.UnitTests.csproj +++ b/src/Behavioral.Automation.UnitTests/Behavioral.Automation.UnitTests.csproj @@ -7,10 +7,10 @@ - - - - + + + + diff --git a/src/Behavioral.Automation/Behavioral.Automation.csproj b/src/Behavioral.Automation/Behavioral.Automation.csproj index b5be82ef..e4ff3f22 100644 --- a/src/Behavioral.Automation/Behavioral.Automation.csproj +++ b/src/Behavioral.Automation/Behavioral.Automation.csproj @@ -22,16 +22,16 @@ The whole automation code is divided into the following parts: - - - - - - - - - - + + + + + + + + + + diff --git a/src/Behavioral.Automation/Bindings/ListBinding.cs b/src/Behavioral.Automation/Bindings/ListBinding.cs index f8b24502..7c1e209f 100644 --- a/src/Behavioral.Automation/Bindings/ListBinding.cs +++ b/src/Behavioral.Automation/Bindings/ListBinding.cs @@ -11,13 +11,6 @@ namespace Behavioral.Automation.Bindings [Binding] public class ListBinding { - private readonly IDriverService _driverService; - - public ListBinding([NotNull] IDriverService driverService) - { - _driverService = driverService; - } - [Given("(.+?) (has|does not have) the following items:")] [Then("(.+?) should (have|not have) the following items:")] public void CheckListHaveItems(IListWrapper list, string behavior, Table table) diff --git a/src/Behavioral.Automation/FluentAssertions/Assert.cs b/src/Behavioral.Automation/FluentAssertions/Assert.cs index ae03ca81..5aaec4c2 100644 --- a/src/Behavioral.Automation/FluentAssertions/Assert.cs +++ b/src/Behavioral.Automation/FluentAssertions/Assert.cs @@ -89,7 +89,7 @@ public static void True(bool condition, string message) if (!condition) { TestContext.WriteLine($"\nActual:\n{message}"); - NUnit.Framework.Assert.Fail(); + NUnit.Framework.Assert.Fail($"Test execution failed: \n{message}"); } } diff --git a/src/Behavioral.Automation/Services/BrowserRunner.cs b/src/Behavioral.Automation/Services/BrowserRunner.cs index 792812fb..769ef928 100644 --- a/src/Behavioral.Automation/Services/BrowserRunner.cs +++ b/src/Behavioral.Automation/Services/BrowserRunner.cs @@ -2,15 +2,16 @@ using System.Collections.Generic; using JetBrains.Annotations; using OpenQA.Selenium.Chrome; +using OpenQA.Selenium.Chromium; using OpenQA.Selenium.Remote; namespace Behavioral.Automation.Services { public class BrowserRunner { - public RemoteWebDriver Driver; + public ChromiumDriver Driver; - public void OpenBrowser([NotNull] RemoteWebDriver driver) + public void OpenBrowser([NotNull] ChromiumDriver driver) { Driver = driver; } @@ -51,7 +52,7 @@ public void OpenChrome(ChromeOptions options = null) {"behavior", "allow"}, {"downloadPath", downloadPath} }; - driver.ExecuteChromeCommand("Page.setDownloadBehavior", param); + driver.ExecuteCdpCommand("Page.setDownloadBehavior", param); OpenBrowser(driver); } diff --git a/src/Behavioral.Automation/Services/DriverService.cs b/src/Behavioral.Automation/Services/DriverService.cs index 4eb27d94..20123c6b 100644 --- a/src/Behavioral.Automation/Services/DriverService.cs +++ b/src/Behavioral.Automation/Services/DriverService.cs @@ -8,8 +8,8 @@ using JetBrains.Annotations; using NUnit.Framework; using OpenQA.Selenium; +using OpenQA.Selenium.Chromium; using OpenQA.Selenium.Interactions; -using OpenQA.Selenium.Remote; namespace Behavioral.Automation.Services { @@ -26,7 +26,7 @@ public DriverService([NotNull] IScopeContextManager scopeContextManager, Browser _browserRunner = browserRunner; } - public RemoteWebDriver Driver => _browserRunner.Driver; + public ChromiumDriver Driver => _browserRunner.Driver; private readonly string SearchAttribute = ConfigServiceBase.SearchAttribute; diff --git a/src/Behavioral.Automation/Services/IDriverService.cs b/src/Behavioral.Automation/Services/IDriverService.cs index b280374d..0d6bc96b 100644 --- a/src/Behavioral.Automation/Services/IDriverService.cs +++ b/src/Behavioral.Automation/Services/IDriverService.cs @@ -1,13 +1,13 @@ using System.Collections.ObjectModel; using JetBrains.Annotations; using OpenQA.Selenium; -using OpenQA.Selenium.Remote; +using OpenQA.Selenium.Chromium; namespace Behavioral.Automation.Services { public interface IDriverService { - RemoteWebDriver Driver { get; } + ChromiumDriver Driver { get; } string CurrentUrl { [NotNull] get; } From 12203538bb19facb862002b788090b2cec508a7e Mon Sep 17 00:00:00 2001 From: Sergey Ruzin Date: Wed, 1 Dec 2021 16:23:40 +0300 Subject: [PATCH 2/2] Changed RemoteWebDriver to IWebDriver --- .../Services/BrowserRunner.cs | 7 ++++--- .../Services/DriverService.cs | 21 ++++++++++++------- .../Services/IDriverService.cs | 6 ++++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Behavioral.Automation/Services/BrowserRunner.cs b/src/Behavioral.Automation/Services/BrowserRunner.cs index 769ef928..30f22fec 100644 --- a/src/Behavioral.Automation/Services/BrowserRunner.cs +++ b/src/Behavioral.Automation/Services/BrowserRunner.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using JetBrains.Annotations; +using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Chromium; using OpenQA.Selenium.Remote; @@ -9,9 +10,9 @@ namespace Behavioral.Automation.Services { public class BrowserRunner { - public ChromiumDriver Driver; - - public void OpenBrowser([NotNull] ChromiumDriver driver) + public IWebDriver Driver; + + public void OpenBrowser([NotNull] IWebDriver driver) { Driver = driver; } diff --git a/src/Behavioral.Automation/Services/DriverService.cs b/src/Behavioral.Automation/Services/DriverService.cs index 20123c6b..32564c2d 100644 --- a/src/Behavioral.Automation/Services/DriverService.cs +++ b/src/Behavioral.Automation/Services/DriverService.cs @@ -8,8 +8,8 @@ using JetBrains.Annotations; using NUnit.Framework; using OpenQA.Selenium; -using OpenQA.Selenium.Chromium; using OpenQA.Selenium.Interactions; +using OpenQA.Selenium.Remote; namespace Behavioral.Automation.Services { @@ -26,7 +26,7 @@ public DriverService([NotNull] IScopeContextManager scopeContextManager, Browser _browserRunner = browserRunner; } - public ChromiumDriver Driver => _browserRunner.Driver; + public IWebDriver Driver => _browserRunner.Driver; private readonly string SearchAttribute = ConfigServiceBase.SearchAttribute; @@ -80,13 +80,19 @@ public ReadOnlyCollection FindElementsByXpath(string path) return Driver.FindElements(By.XPath(path)); } + public object ExecuteScript(string script, params object[] args) + { + IJavaScriptExecutor js = (IJavaScriptExecutor)Driver; + return js.ExecuteScript(script, args); + } + 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));"; - Driver.ExecuteScript(scrollElementIntoMiddle, element); + ExecuteScript(scrollElementIntoMiddle, element); Actions actions = new Actions(Driver); actions.MoveToElement(element); actions.Perform(); @@ -130,8 +136,8 @@ private Uri GetUriFromRelativePath(string url) } public void RemoveFocusFromActiveElement() - { - Driver.ExecuteScript("document.activeElement.blur()"); + { + ExecuteScript("document.activeElement.blur()"); } public void DebugDumpPage() @@ -165,13 +171,14 @@ public void ResizeWindow(int Height, int Width) public string MakeScreenShot() { var fileName = Regex.Replace(TestContext.CurrentContext.Test.Name, @"(\\|\"")", string.Empty) + ".png"; - Driver.GetScreenshot().SaveAsFile(fileName, ScreenshotImageFormat.Png); + Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot(); + screenshot.SaveAsFile(fileName, ScreenshotImageFormat.Png); return fileName; } public void ScrollElementTo(IWebElement element, int offset) { - Driver.ExecuteScript(@" + ExecuteScript(@" var element = arguments[0]; var offset = arguments[1]; element.scrollTo({top: offset, behavior: 'smooth'});", element, offset); diff --git a/src/Behavioral.Automation/Services/IDriverService.cs b/src/Behavioral.Automation/Services/IDriverService.cs index 0d6bc96b..9ef2b21c 100644 --- a/src/Behavioral.Automation/Services/IDriverService.cs +++ b/src/Behavioral.Automation/Services/IDriverService.cs @@ -1,13 +1,13 @@ using System.Collections.ObjectModel; using JetBrains.Annotations; using OpenQA.Selenium; -using OpenQA.Selenium.Chromium; +using OpenQA.Selenium.Remote; namespace Behavioral.Automation.Services { public interface IDriverService { - ChromiumDriver Driver { get; } + IWebDriver Driver { get; } string CurrentUrl { [NotNull] get; } @@ -26,6 +26,8 @@ public interface IDriverService ReadOnlyCollection FindElementsByXpath([NotNull] string path); + object ExecuteScript(string script, params object[] args); + public void Refresh(); void Navigate([NotNull] string url);