From bb65ae923540879e73e5b4d992db2b85c66d1686 Mon Sep 17 00:00:00 2001 From: Victor Zheng Date: Thu, 14 Dec 2023 17:11:33 -0500 Subject: [PATCH] update selenium wrapper to avoid usage of App.Config --- TestingDriver/src/SeleniumDriver.cs | 85 ++++++++++++++--------------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/TestingDriver/src/SeleniumDriver.cs b/TestingDriver/src/SeleniumDriver.cs index 52efeb3..88eced7 100644 --- a/TestingDriver/src/SeleniumDriver.cs +++ b/TestingDriver/src/SeleniumDriver.cs @@ -6,7 +6,6 @@ namespace TestingDriver { using System; using System.Collections.Generic; - using System.Configuration; using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; @@ -50,6 +49,11 @@ public class SeleniumDriver : ITestingDriver private TimeSpan timeOutThreshold; private TimeSpan actualTimeOut; + private bool incogMode; + private bool headless; + + private string browserSize; + private string remoteHost; /// @@ -64,7 +68,10 @@ public class SeleniumDriver : ITestingDriver /// The xpath for any loading spinners. /// The xpath for any error containers. /// The address of the remote host. + /// Indicate whether to run the browser in headless mode. + /// Indicate whether to run the browser in incognito mode. /// Any Web driver to be passed in. + /// The execution type which indicates how the test will be executed in. public SeleniumDriver( string browser = "chrome", int timeOut = 5, @@ -75,7 +82,10 @@ public SeleniumDriver( string loadingSpinner = "", string errorContainer = "", string remoteHost = "", - IWebDriver webDriver = null) + bool headless = true, + bool incogMode = true, + IWebDriver webDriver = null, + string browserSize = "max") { this.browserType = this.GetBrowserType(browser); this.timeOutThreshold = TimeSpan.FromSeconds(timeOut); @@ -84,6 +94,10 @@ public SeleniumDriver( this.screenshotSaveLocation = screenshotSaveLocation; this.actualTimeOut = TimeSpan.FromMinutes(actualTimeout); + this.incogMode = incogMode; + this.headless = headless; + this.browserSize = browserSize; + if (string.IsNullOrEmpty(loadingSpinner)) { this.LoadingSpinner = "loadingspinner"; @@ -354,48 +368,37 @@ public void Maximize() // we want to determine what the size should be before maximizing. Console.WriteLine("Maximizing browser"); - string exType = ConfigurationManager.AppSettings["ExecutionType"]; - List exTypesList = ConfigurationManager.AppSettings["ListTypeOfExecutions"].Split(",").ToList(); - // maximize the browser first, then do any changes this.WebDriver.Manage().Window.Maximize(); // grab the maximized brwoser size Size windowSize = this.WebDriver.Manage().Window.Size; - if (exTypesList.Contains(exType)) + // browser Width and browser Height + switch (this.browserSize) { - switch (exType) - { - case "mobile": - windowSize.Width = windowSize.Width / 3; - break; - case "tablet": - windowSize.Width = windowSize.Width / 2; - break; - case "desktop": - windowSize.Width = 1024; - windowSize.Height = 768; - break; - case "extended-desktop": - case "max": - // maximize to the max size of the window, which should already be done - break; - default: - Logger.Warn("Not implemented error for size"); - break; - } - } - else - { - Logger.Info("Exeuction type list does not contain size" + exType); - this.WebDriver.Manage().Window.Maximize(); + case "mobile": + windowSize.Width = windowSize.Width / 3; + break; + case "tablet": + windowSize.Width = windowSize.Width / 2; + break; + case "desktop": + windowSize.Width = 1024; + windowSize.Height = 768; + break; + case "extended-desktop": + break; + case "max": + // maximize to the max size of the window, which should already be done + break; + default: + Logger.Warn("Not implemented error for size"); + break; } Console.WriteLine($"Size after maximization: {windowSize.Width} {windowSize.Height}"); this.WebDriver.Manage().Window.Size = windowSize; - - } /// @@ -1102,10 +1105,6 @@ private void InstantiateSeleniumDriver() ChromeOptions chromeOptions; ChromeDriverService service; - // create local var to determine whether to enable incog mode - // bool enableIncog = bool.Parse(ConfigurationManager.AppSettings["INCOGMODE"].ToString()); - // bool enableHeadless = bool.Parse(ConfigurationManager.AppSettings["HEADLESS_MODE"].ToString()); - Logger.Info("Browser type is: " + this.browserType); switch (this.browserType) @@ -1119,7 +1118,7 @@ private void InstantiateSeleniumDriver() }; // check if in incog mode, if it is, then we launch incog mode - if (enableIncog) + if (this.incogMode) { chromeOptions.AddArgument("--incognito"); } @@ -1141,13 +1140,13 @@ private void InstantiateSeleniumDriver() }; // check if in incog mode, if it is, then we launch incog mode - if (enableIncog) + if (this.incogMode) { chromeOptions.AddArgument("--incognito"); } // enable headless mode - if (enableHeadless) + if (this.headless) { Logger.Info("Started a headless session"); chromeOptions.AddArgument("--headless=new"); @@ -1194,7 +1193,7 @@ private void InstantiateSeleniumDriver() }; // check if in incog mode, if it is, then we launch incog mode - if (enableIncog) + if (this.incogMode) { // options.AddArgument("--incognito"); // options.AddAdditionalEdgeOption("InPrivate", true); @@ -1233,7 +1232,7 @@ private void InstantiateSeleniumDriver() FirefoxOptions fireFoxOptions = new FirefoxOptions(); // check if in incog mode, if it is, then we launch incog mode - if (enableIncog) + if (this.incogMode) { fireFoxOptions.AddArgument("-private"); } @@ -1274,7 +1273,7 @@ private void InstantiateSeleniumDriver() IntroduceInstabilityByIgnoringProtectedModeSettings = true, IgnoreZoomLevel = true, EnsureCleanSession = true, - EnableNativeEvents = bool.Parse(ConfigurationManager.AppSettings["IEEnableNativeEvents"].ToString()), + EnableNativeEvents = true, UnhandledPromptBehavior = UnhandledPromptBehavior.Accept, RequireWindowFocus = true,