diff --git a/Behavioral.Automation.Selenium/Behavioral.Automation.DemoBindings/AutomationConfig.json b/Behavioral.Automation.Selenium/Behavioral.Automation.DemoBindings/AutomationConfig.json index 21b06b58..44acb972 100644 --- a/Behavioral.Automation.Selenium/Behavioral.Automation.DemoBindings/AutomationConfig.json +++ b/Behavioral.Automation.Selenium/Behavioral.Automation.DemoBindings/AutomationConfig.json @@ -8,5 +8,6 @@ "BAUTH_LOGIN": "", "BAUTH_PASSWORD": "", "BAUTH_IGNORE": "true", - "BROWSER_BINARY_LOCATION" : "" + "BROWSER_BINARY_LOCATION" : "", + "UNHANDLED_PROMPT_BEHAVIOR" : "" } \ No newline at end of file diff --git a/Behavioral.Automation.Selenium/Behavioral.Automation.DemoBindings/Behavioral.Automation.DemoBindings.csproj b/Behavioral.Automation.Selenium/Behavioral.Automation.DemoBindings/Behavioral.Automation.DemoBindings.csproj index 17797381..4041255f 100644 --- a/Behavioral.Automation.Selenium/Behavioral.Automation.DemoBindings/Behavioral.Automation.DemoBindings.csproj +++ b/Behavioral.Automation.Selenium/Behavioral.Automation.DemoBindings/Behavioral.Automation.DemoBindings.csproj @@ -10,8 +10,8 @@ - - + + diff --git a/Behavioral.Automation.Selenium/Behavioral.Automation/Behavioral.Automation.csproj b/Behavioral.Automation.Selenium/Behavioral.Automation/Behavioral.Automation.csproj index 3182f812..a2a522a7 100644 --- a/Behavioral.Automation.Selenium/Behavioral.Automation/Behavioral.Automation.csproj +++ b/Behavioral.Automation.Selenium/Behavioral.Automation/Behavioral.Automation.csproj @@ -16,7 +16,7 @@ The whole automation code is divided into the following parts: - UI structure descriptive code - Supportive code Quantori Inc. - 1.13.0 + 1.14.0 https://github.com/quantori/Behavioral.Automation true true @@ -36,8 +36,8 @@ The whole automation code is divided into the following parts: runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/Behavioral.Automation.Selenium/Behavioral.Automation/Services/BrowserRunner.cs b/Behavioral.Automation.Selenium/Behavioral.Automation/Services/BrowserRunner.cs index 4a4c0225..5dacf3e5 100644 --- a/Behavioral.Automation.Selenium/Behavioral.Automation/Services/BrowserRunner.cs +++ b/Behavioral.Automation.Selenium/Behavioral.Automation/Services/BrowserRunner.cs @@ -59,6 +59,16 @@ public void OpenChrome(ChromeOptions options = null) { options.BinaryLocation = Environment.ExpandEnvironmentVariables(ConfigServiceBase.BrowserBinaryLocation); } + if (!string.IsNullOrWhiteSpace(ConfigServiceBase.UnhandledPromptBehavior)) + { + options.UnhandledPromptBehavior = ConfigServiceBase.UnhandledPromptBehavior switch + { + "Accept" => UnhandledPromptBehavior.Accept, + "Dismiss" => UnhandledPromptBehavior.Dismiss, + "Ignore" => UnhandledPromptBehavior.Ignore, + _ => options.UnhandledPromptBehavior + }; + } } var driver = new ChromeDriver(options); diff --git a/Behavioral.Automation.Selenium/Behavioral.Automation/Services/ConfigServiceBase.cs b/Behavioral.Automation.Selenium/Behavioral.Automation/Services/ConfigServiceBase.cs index f7a05202..ab4e243d 100644 --- a/Behavioral.Automation.Selenium/Behavioral.Automation/Services/ConfigServiceBase.cs +++ b/Behavioral.Automation.Selenium/Behavioral.Automation/Services/ConfigServiceBase.cs @@ -29,7 +29,11 @@ public static class ConfigServiceBase private const string AccessClipboardString = "ACCESS_CLIPBOARD"; private const string BrowserBinaryLocationString = "BROWSER_BINARY_LOCATION"; + + private const string UnhandledPromptBehaviorString = "UNHANDLED_PROMPT_BEHAVIOR"; + public static string UnhandledPromptBehavior => ConfigRoot[UnhandledPromptBehaviorString]; + public static string BaseUrl => ConfigRoot[BaseUrlString]; public static string BrowserParameters => ConfigRoot[BrowserParametersString]; diff --git a/Behavioral.Automation.Selenium/Behavioral.Automation/Services/DriverService.cs b/Behavioral.Automation.Selenium/Behavioral.Automation/Services/DriverService.cs index 712c0305..f5d9863e 100644 --- a/Behavioral.Automation.Selenium/Behavioral.Automation/Services/DriverService.cs +++ b/Behavioral.Automation.Selenium/Behavioral.Automation/Services/DriverService.cs @@ -183,6 +183,26 @@ public void SwitchToTheFirstWindow() var handle = Driver.WindowHandles.First(); Driver.SwitchTo().Window(handle); } + + /// + /// Close inactive windows + /// + public void CloseInactiveWindows() + { + var currentWindowHandle = Driver.CurrentWindowHandle; + var windowHandles = Driver.WindowHandles; + + foreach (var windowHandle in windowHandles) + { + if (!windowHandle.Equals(currentWindowHandle)) + { + Driver.SwitchTo().Window(windowHandle); + Driver.Close(); + } + } + + Driver.SwitchTo().Window(currentWindowHandle); + } /// /// Get URI from relative URL @@ -229,23 +249,15 @@ public void Navigate(string url) var uri = new Uri(url); _scopeContextManager.SwitchPage(uri); } - - /// - /// Switch to the last opened window - /// - public void SwitchToLastWindow() - { - Driver.SwitchTo().Window(Driver.WindowHandles.Last()); - } /// /// Change size of opened browser window /// - /// Desired height - /// Desired width - public void ResizeWindow(int Height, int Width) + /// Desired height + /// Desired width + public void ResizeWindow(int height, int width) { - Driver.Manage().Window.Size = new Size(Width, Height); + Driver.Manage().Window.Size = new Size(width, height); } /// @@ -295,5 +307,11 @@ public string SaveBrowserLog() return fileName; } + + public void ClearCache() + { + Driver.Manage().Cookies.DeleteAllCookies(); + ExecuteScript("javascript: localStorage.clear()"); + } } } \ No newline at end of file diff --git a/Behavioral.Automation.Selenium/Behavioral.Automation/Services/IDriverService.cs b/Behavioral.Automation.Selenium/Behavioral.Automation/Services/IDriverService.cs index 32b6130b..733e0da0 100644 --- a/Behavioral.Automation.Selenium/Behavioral.Automation/Services/IDriverService.cs +++ b/Behavioral.Automation.Selenium/Behavioral.Automation/Services/IDriverService.cs @@ -1,7 +1,6 @@ using System.Collections.ObjectModel; using JetBrains.Annotations; using OpenQA.Selenium; -using OpenQA.Selenium.Remote; namespace Behavioral.Automation.Services { @@ -114,9 +113,9 @@ public interface IDriverService /// /// Change size of opened browser window /// - /// Desired height - /// Desired width - void ResizeWindow(int Height, int Width); + /// Desired height + /// Desired width + void ResizeWindow(int height, int width); /// /// Make screenshot @@ -136,5 +135,15 @@ public interface IDriverService /// /// Path to saved log string SaveBrowserLog(); + + /// + /// Clears browser cache + /// + void ClearCache(); + + /// + /// Close inactive windows + /// + public void CloseInactiveWindows(); } } \ No newline at end of file diff --git a/Behavioral.Automation.Selenium/CHANGELOG.md b/Behavioral.Automation.Selenium/CHANGELOG.md index f719e8e6..f5d31562 100644 --- a/Behavioral.Automation.Selenium/CHANGELOG.md +++ b/Behavioral.Automation.Selenium/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -[1.13.0] - 2022-06-03 +[1.14.0] - 2022-07-27 ### Added -- Changed folder structure to separate Selenium and Playwright versions of the framework \ No newline at end of file +- Added method to clear browser data +- Added method to close inactive windows +- Small fixes \ No newline at end of file