Skip to content

Commit

Permalink
update selenium wrapper to avoid usage of App.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Zheng authored and Victor Zheng committed Dec 14, 2023
1 parent 8680bde commit bb65ae9
Showing 1 changed file with 42 additions and 43 deletions.
85 changes: 42 additions & 43 deletions TestingDriver/src/SeleniumDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/// <summary>
Expand All @@ -64,7 +68,10 @@ public class SeleniumDriver : ITestingDriver
/// <param name="loadingSpinner">The xpath for any loading spinners.</param>
/// <param name="errorContainer">The xpath for any error containers.</param>
/// <param name="remoteHost">The address of the remote host.</param>
/// <param name="headless">Indicate whether to run the browser in headless mode.</param>
/// <param name="incogMode">Indicate whether to run the browser in incognito mode.</param>
/// <param name="webDriver">Any Web driver to be passed in.</param>
/// <param name="browserSize">The execution type which indicates how the test will be executed in.</param>
public SeleniumDriver(
string browser = "chrome",
int timeOut = 5,
Expand All @@ -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);
Expand All @@ -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";
Expand Down Expand Up @@ -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<string> 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;


}

/// <inheritdoc/>
Expand Down Expand Up @@ -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)
Expand All @@ -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");
}
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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,

Expand Down

0 comments on commit bb65ae9

Please sign in to comment.