Skip to content

Commit

Permalink
Merge pull request #18 from maciekpazola/Develop
Browse files Browse the repository at this point in the history
Refactor and code cleanup
  • Loading branch information
maciekpazola authored Dec 18, 2024
2 parents 74b840c + 3338b3b commit a052bbb
Show file tree
Hide file tree
Showing 33 changed files with 233 additions and 324 deletions.
8 changes: 3 additions & 5 deletions TestUtilities/TestUtilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
<PackageReference Include="DotNetSeleniumExtras.PageObjects.Core" Version="4.14.1" />
<PackageReference Include="DotNetSeleniumExtras.WaitHelpers" Version="3.11.0" />
<PackageReference Include="ExtentReports" Version="5.0.4" />
<PackageReference Include="FluentAssertions" Version="6.12.2" />
<PackageReference Include="log4net" Version="3.0.3" />
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="MongoDB.Driver" Version="3.1.0" />
<PackageReference Include="NUnit" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="Selenium.Support" Version="4.27.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.27.0" />
<PackageReference Include="SpecFlow.Assist.Dynamic" Version="1.4.2" />
<PackageReference Include="SpecFlow.NUnit" Version="3.9.74" />
<PackageReference Include="WebDriverManager" Version="2.17.4" />
</ItemGroup>
Expand Down
9 changes: 5 additions & 4 deletions UIAutomationTests/AbstractionLayer/Elements/AlertElement.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using NUnit.Framework;
using OpenQA.Selenium;
using SeleniumExtras.WaitHelpers;
using TestUtilities.UITesting.Drivers;
using TestUtilities.UITesting.Helpers;
using TestUtilities.Logs;
using UIAutomationTests.Drivers;
using UIAutomationTests.Helpers;


namespace TestUtilities.UITesting.AbstractionLayer.Elements
namespace UIAutomationTests.AbstractionLayer.Elements
{
public class AlertElement
{
Expand All @@ -18,9 +18,10 @@ public AlertElement(ScenarioContext scenarioContext)
{
_waits = new(scenarioContext);
_logger = new(scenarioContext.ScenarioInfo.Title);

try
{
Alert = Driver.GetDriver(scenarioContext.Get<string>("BrowserName")).SwitchTo().Alert();
Alert = Driver.GetDriver(scenarioContext).SwitchTo().Alert();
}
catch (NoAlertPresentException)
{
Expand Down
6 changes: 3 additions & 3 deletions UIAutomationTests/AbstractionLayer/Elements/ButtonElement.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using OpenQA.Selenium;
using TestUtilities.UITesting.Drivers;
using UIAutomationTests.Drivers;


namespace TestUtilities.UITesting.AbstractionLayer.Elements
namespace UIAutomationTests.AbstractionLayer.Elements
{
public class ButtonElement
{
Expand All @@ -11,7 +11,7 @@ public class ButtonElement
public ButtonElement(ScenarioContext scenarioContext, string locator)
{
_scenarioContext = scenarioContext;
Button = Driver.GetDriver(_scenarioContext.Get<string>("BrowserName")).FindElement(By.CssSelector(locator));
Button = Driver.GetDriver(_scenarioContext).FindElement(By.CssSelector(locator));
}

public void Click() => Button.Click();
Expand Down
23 changes: 8 additions & 15 deletions UIAutomationTests/AbstractionLayer/Elements/CheckboxElement.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using TestUtilities.UITesting.Drivers;
using TestUtilities.UITesting.Helpers;
using TestUtilities.Logs;
using NUnit.Framework;
using TestUtilities.Logs;
using OpenQA.Selenium;
using UIAutomationTests.Drivers;
using UIAutomationTests.Helpers;

namespace TestUtilities.UITesting.AbstractionLayer.Elements
namespace UIAutomationTests.AbstractionLayer.Elements
{
public class CheckboxElement
{
public readonly IWebElement ?Checkbox;
public readonly IWebElement? Checkbox;
private readonly ScenarioContext _scenarioContext;
private readonly string _locator;
private readonly StateChecker _stateChecker;
Expand All @@ -22,20 +21,14 @@ public CheckboxElement(ScenarioContext scenarioContext, string locator)
_logger = new(scenarioContext.ScenarioInfo.Title);
try
{
Checkbox = Driver.GetDriver(_scenarioContext.Get<string>("BrowserName")).FindElement(By.CssSelector(locator));
Checkbox = Driver.GetDriver(_scenarioContext).FindElement(By.CssSelector(locator));
}
catch (NoSuchElementException)
{
_logger.WriteInfoLog("Can't find checkbox element");
}
}

public void AssertIfChecked(bool expectedResult)
{
bool isChecked = GetCheckedState();
Assert.That(isChecked, Is.EqualTo(expectedResult));
}

public void CheckAll()
{
int numberOfCheckboxes = _stateChecker.GetNumberOfElements(By.CssSelector(_locator));
Expand All @@ -62,8 +55,8 @@ public void UnCheckAll()
}
}

private void Click() => Checkbox?.Click();
public bool GetCheckedState() => StateChecker.GetPropertyState(Checkbox, Properties.Checked);

Check warning on line 58 in UIAutomationTests/AbstractionLayer/Elements/CheckboxElement.cs

View workflow job for this annotation

GitHub Actions / Build and tests

Possible null reference argument for parameter 'element' in 'bool StateChecker.GetPropertyState(IWebElement element, string property)'.

private bool GetCheckedState() => StateChecker.GetPropertyState(Checkbox, Properties.Checked);
private void Click() => Checkbox?.Click();
}
}
10 changes: 3 additions & 7 deletions UIAutomationTests/AbstractionLayer/Elements/DropdownElement.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using TestUtilities.UITesting.Drivers;
using UIAutomationTests.Drivers;

namespace TestUtilities.UITesting.AbstractionLayer.Elements
namespace UIAutomationTests.AbstractionLayer.Elements
{
public class DropdownElement
{
Expand All @@ -14,7 +14,7 @@ public class DropdownElement
public DropdownElement(ScenarioContext scenarioContext, string cssSelector)
{
_scenarioContext = scenarioContext;
Dropdown = Driver.GetDriver(_scenarioContext.Get<string>("BrowserName")).FindElement(By.CssSelector(cssSelector));
Dropdown = Driver.GetDriver(_scenarioContext).FindElement(By.CssSelector(cssSelector));
_dropdown = new SelectElement(Dropdown);
}

Expand All @@ -23,9 +23,6 @@ public DropdownElement(ScenarioContext scenarioContext, string cssSelector)
public void SelectElementInDropdown(string value)
{
_dropdown.SelectByValue(value);
//Assertion will check if the element is selected
var selectedElement = _dropdown.SelectedOption;
ExpectedConditions.ElementToBeSelected(selectedElement);
}

public void SelectAllElementsInDropdown()
Expand All @@ -34,7 +31,6 @@ public void SelectAllElementsInDropdown()
for (int i = 1; i < numberOfElements; i++)
{
_dropdown.SelectByIndex(i);
//Assertion will check if the element is selected
_dropdown.SelectedOption.Selected.Should().BeTrue();
}
}
Expand Down
2 changes: 1 addition & 1 deletion UIAutomationTests/AbstractionLayer/Properties.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TestUtilities.UITesting.AbstractionLayer
namespace UIAutomationTests.AbstractionLayer
{
public static class Properties
{
Expand Down
5 changes: 3 additions & 2 deletions UIAutomationTests/Drivers/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;

namespace TestUtilities.UITesting.Drivers
namespace UIAutomationTests.Drivers
{
public class Driver
{
private static readonly ThreadLocal<IWebDriver> _threadLocalDriver = new();

public static IWebDriver GetDriver(string browserName)
public static IWebDriver GetDriver(ScenarioContext scenarioContext)
{
var browserName = scenarioContext.Get<string>("BrowserName");
if (!_threadLocalDriver.IsValueCreated || _threadLocalDriver.Value == null)
{
dynamic options = GetBrowserOptions(browserName);
Expand Down
8 changes: 4 additions & 4 deletions UIAutomationTests/Helpers/ActionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using TestUtilities.UITesting.Drivers;
using OpenQA.Selenium;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using UIAutomationTests.Drivers;

namespace TestUtilities.UITesting.Helpers
namespace UIAutomationTests.Helpers
{
public class ActionsBuilder
{
Expand All @@ -11,7 +11,7 @@ public class ActionsBuilder

public IAction RightClickOnContextMenu(IWebElement contextMenu)
{
Actions actions = new(Driver.GetDriver(_scenarioContext.Get<string>("BrowserName")));
Actions actions = new(Driver.GetDriver(_scenarioContext));
return actions.MoveToElement(contextMenu).ContextClick().Build();
}
}
Expand Down
2 changes: 1 addition & 1 deletion UIAutomationTests/Helpers/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using OpenQA.Selenium;
using TechTalk.SpecFlow.Tracing;

namespace TestUtilities.UITesting.Helpers
namespace UIAutomationTests.Helpers
{
public class FileManager(FeatureContext featureContext, ScenarioContext scenarioContext)
{
Expand Down
2 changes: 1 addition & 1 deletion UIAutomationTests/Helpers/Locator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TestUtilities.UITesting.Helpers
namespace UIAutomationTests.Helpers
{
public static class Locator
{
Expand Down
59 changes: 9 additions & 50 deletions UIAutomationTests/Helpers/StateChecker.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using TestUtilities.UITesting.Drivers;
using OpenQA.Selenium;
using OpenQA.Selenium;
using TestUtilities.Logs;
using UIAutomationTests.Drivers;

namespace TestUtilities.UITesting.Helpers
namespace UIAutomationTests.Helpers
{
public class StateChecker
{
Expand All @@ -18,61 +18,20 @@ public StateChecker(ScenarioContext scenarioContext)
}
public static bool GetPropertyState(IWebElement element, string property) => Convert.ToBoolean(element.GetDomProperty(property));

public int GetNumberOfElements(By by) => Driver.GetDriver(_scenarioContext.Get<string>("BrowserName")).FindElements(by).Count;
public int GetNumberOfElements(By by) => Driver.GetDriver(_scenarioContext).FindElements(by).Count;

public bool CheckIfItemIsLoaded(IWebElement clickedButton, IWebElement itemToCheck)
{
var wait = _waits.GetWebDriverWait();
wait.Until(_ => clickedButton.Enabled);

_logger.WriteInfoLog($"Clicked element enable state: {clickedButton.Enabled}");
return CheckIfItemIsEnabled(itemToCheck);
}

public bool CheckIfItemIsEnabled(By locator)
public bool IsElementDisplayed(Func<IWebElement> element)
{
try
{
IWebElement element = Driver.GetDriver(_scenarioContext.Get<string>("BrowserName")).FindElement(locator);
var wait = _waits.GetWebDriverWait();
wait.Until(_ => element.Enabled);

_logger.WriteInfoLog($"Clicked element enable state: {element.Enabled}");
return element.Enabled;
return _waits.WaitUntil(() => element().Displayed);
}
catch (Exception ex)
catch(Exception)
{
if (ex is NoSuchElementException
|| ex is StaleElementReferenceException
|| ex is WebDriverTimeoutException)
{
_logger.WriteInfoLog($"Clicked element enable state: False\nException occurred: {ex.Message}");
return false;
}
throw;
_logger.WriteWarningLog("Exception was found during waiting for element");
return false;
}
}
public bool CheckIfItemIsEnabled(IWebElement element)
{
try
{
var wait = _waits.GetWebDriverWait();
wait.Until(_ => element.Enabled);

_logger.WriteInfoLog($"Clicked element enable state: {element.Enabled}");
return element.Enabled;
}
catch (Exception ex)
{
if (ex is NoSuchElementException
|| ex is StaleElementReferenceException
|| ex is WebDriverTimeoutException)
{
_logger.WriteInfoLog($"Clicked element enable state: False\nException occurred: {ex.Message}");
return false;
}
throw;
}
}
}
}
40 changes: 30 additions & 10 deletions UIAutomationTests/Helpers/Waits.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
using TestUtilities.UITesting.Drivers;
using OpenQA.Selenium;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using UIAutomationTests.Drivers;

namespace TestUtilities.UITesting.Helpers
namespace UIAutomationTests.Helpers
{
public class Waits
{
private readonly ScenarioContext _scenarioContext;
public Waits(ScenarioContext scenarioContext) => _scenarioContext = scenarioContext;

public Waits(ScenarioContext scenarioContext)
{
_scenarioContext = scenarioContext;
}

public DefaultWait<IWebDriver> GetWebDriverWait(int timeoutInSeconds = 5)
{
DefaultWait<IWebDriver> wait = new(Driver.GetDriver(_scenarioContext.Get<string>("BrowserName")));
DefaultWait<IWebDriver> wait = new(Driver.GetDriver(_scenarioContext));
wait.Timeout = TimeSpan.FromSeconds(timeoutInSeconds);

return wait;
}
public DefaultWait<IWebDriver> GetFluentWait(int timeoutInSeconds = 3, int pollingIntervalInMilliseconds = 100)

public bool WaitUntil(Func<bool> condition, int timeoutInSeconds = 5)
{
DefaultWait<IWebDriver> fluentWait = new(Driver.GetDriver(_scenarioContext.Get<string>("BrowserName")));
fluentWait.Timeout = TimeSpan.FromSeconds(timeoutInSeconds);
fluentWait.PollingInterval = TimeSpan.FromMilliseconds(pollingIntervalInMilliseconds);
DateTime startTime = DateTime.Now;

while ((DateTime.Now - startTime).TotalSeconds < timeoutInSeconds)
{
try
{
if (condition())
{
return true;
}
}
catch
{
}

Thread.Sleep(500);
}

return fluentWait;
return false;
}
}
}
4 changes: 2 additions & 2 deletions UIAutomationTests/Hooks/AfterHooks.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using TestUtilities.UITesting.Drivers;
using System.Reflection;
using System.Reflection;
using TestUtilities.Logs;
using NUnit.Framework;
using UIAutomationTests.Drivers;
[assembly: Parallelizable(ParallelScope.Fixtures)]
[assembly: LevelOfParallelism(3)]

Expand Down
Loading

0 comments on commit a052bbb

Please sign in to comment.