diff --git a/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj b/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj index 2e0677d5..df62a7cb 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj +++ b/Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj @@ -83,7 +83,7 @@ - + diff --git a/Aquality.Selenium/src/Aquality.Selenium/Locators/Enums/RelativeAqualityLocators.cs b/Aquality.Selenium/src/Aquality.Selenium/Locators/Enums/RelativeAqualityLocators.cs new file mode 100644 index 00000000..e1f1ef35 --- /dev/null +++ b/Aquality.Selenium/src/Aquality.Selenium/Locators/Enums/RelativeAqualityLocators.cs @@ -0,0 +1,11 @@ +namespace Aquality.Selenium.Locators.Enums +{ + public enum RelativeAqualityLocators + { + Above, + Below, + Left, + Right, + Near, + } +} diff --git a/Aquality.Selenium/src/Aquality.Selenium/Locators/IRelativeAquality.cs b/Aquality.Selenium/src/Aquality.Selenium/Locators/IRelativeAquality.cs new file mode 100644 index 00000000..032aaa7f --- /dev/null +++ b/Aquality.Selenium/src/Aquality.Selenium/Locators/IRelativeAquality.cs @@ -0,0 +1,29 @@ +using OpenQA.Selenium; +using Aquality.Selenium.Core.Elements.Interfaces; + +namespace Aquality.Selenium.Locators +{ + internal interface IRelativeAquality + { + RelativeAqualityBy Above(By by); + RelativeAqualityBy Below(By by); + RelativeAqualityBy Left(By by); + RelativeAqualityBy Right(By by); + RelativeAqualityBy Near(By by); + RelativeAqualityBy Near(By by, int atMostDistanceInPixels); + + RelativeAqualityBy Above(IElement element); + RelativeAqualityBy Below(IElement element); + RelativeAqualityBy Left(IElement element); + RelativeAqualityBy Right(IElement elementy); + RelativeAqualityBy Near(IElement element); + RelativeAqualityBy Near(IElement element, int atMostDistanceInPixels); + + RelativeAqualityBy Above(WebElement webElement); + RelativeAqualityBy Below(WebElement webElement); + RelativeAqualityBy Left(WebElement webElement); + RelativeAqualityBy Right(WebElement webElement); + RelativeAqualityBy Near(WebElement webElement); + RelativeAqualityBy Near(WebElement webElement, int atMostDistanceInPixels); + } +} diff --git a/Aquality.Selenium/src/Aquality.Selenium/Locators/RelativeAqualityBy.cs b/Aquality.Selenium/src/Aquality.Selenium/Locators/RelativeAqualityBy.cs new file mode 100644 index 00000000..f27f8cb2 --- /dev/null +++ b/Aquality.Selenium/src/Aquality.Selenium/Locators/RelativeAqualityBy.cs @@ -0,0 +1,284 @@ +using OpenQA.Selenium; +using RelativeSeleniumBy = OpenQA.Selenium.RelativeBy; +using Aquality.Selenium.Core.Elements.Interfaces; +using System.Collections.ObjectModel; +using System; +using System.Collections.Generic; +using System.Linq; +using Aquality.Selenium.Locators.Enums; + +namespace Aquality.Selenium.Locators +{ + public class RelativeAqualityBy : RelativeSeleniumBy, IRelativeAquality + { + private readonly By by; + private readonly List> relativePairs = new List>(); + + private const int DEFAULT_VALUE_IN_PIXELS_FOR_THE_NEAR_LOCATOR = 50; + + private RelativeAqualityBy(By by) + { + this.by = by; + } + + public static RelativeAqualityBy WithLocator(By by) + { + return new RelativeAqualityBy(by); + } + + public RelativeAqualityBy Above(By by) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Above, new[] { by })); + return this; + } + + public RelativeAqualityBy Above(WebElement webElement) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Above, new[] { webElement })); + return this; + } + + public RelativeAqualityBy Above(IElement element) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Above, new[] { element.Locator })); + return this; + } + + public RelativeAqualityBy Below(By by) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Below, new[] { by })); + return this; + } + + public RelativeAqualityBy Below(WebElement webElement) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Below, new[] { webElement })); + return this; + } + + public RelativeAqualityBy Below(IElement element) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Below, new[] { element.Locator })); + return this; + } + + public RelativeAqualityBy Left(By by) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Left, new[] { by })); + return this; + } + + public RelativeAqualityBy Left(WebElement webElement) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Left, new[] { webElement })); + return this; + } + + public RelativeAqualityBy Left(IElement element) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Left, new[] { element.Locator })); + return this; + } + + public RelativeAqualityBy Right(By by) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Right, new[] { by })); + return this; + } + + public RelativeAqualityBy Right(WebElement webElement) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Right, new[] { webElement })); + return this; + } + + public RelativeAqualityBy Right(IElement element) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Right, new[] { element.Locator })); + return this; + } + + public RelativeAqualityBy Near(By by) + { + return Near(by, DEFAULT_VALUE_IN_PIXELS_FOR_THE_NEAR_LOCATOR); + } + + public RelativeAqualityBy Near(WebElement webElement) + { + return Near(webElement, DEFAULT_VALUE_IN_PIXELS_FOR_THE_NEAR_LOCATOR); + } + + public RelativeAqualityBy Near(IElement element) + { + return Near(element, DEFAULT_VALUE_IN_PIXELS_FOR_THE_NEAR_LOCATOR); + } + + public RelativeAqualityBy Near(By by, int atMostDistanceInPixels) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Near, new object[] { by, atMostDistanceInPixels })); + return this; + } + + public RelativeAqualityBy Near(WebElement webElement, int atMostDistanceInPixels) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Near, new object[] { webElement, atMostDistanceInPixels })); + return this; + } + + public RelativeAqualityBy Near(IElement element, int atMostDistanceInPixels) + { + relativePairs.Add(new KeyValuePair(RelativeAqualityLocators.Near, new object[] { element.Locator, atMostDistanceInPixels })); + return this; + } + //TODO! + public override IWebElement FindElement(ISearchContext context) + { + return FindElements(context).First(); + } + //TODO! + public override ReadOnlyCollection FindElements(ISearchContext context) + { + RelativeSeleniumBy formedBy = RelativeSeleniumBy.WithLocator(by); + + relativePairs.ForEach(relativePair => + { + var firstArgument = relativePair.Value[0]; + switch (relativePair.Key) + { + case RelativeAqualityLocators.Above: + formedBy = GetRelativeWithAbove(formedBy, firstArgument); + break; + + case RelativeAqualityLocators.Below: + formedBy = GetRelativeWithBelow(formedBy, firstArgument); + break; + + case RelativeAqualityLocators.Left: + formedBy = GetRelativeWithLeft(formedBy, firstArgument); + break; + + case RelativeAqualityLocators.Right: + formedBy = GetRelativeWithRight(formedBy, firstArgument); + break; + + case RelativeAqualityLocators.Near: + formedBy = GetRelativeWitNear(formedBy, relativePair.Value[0], (int)relativePair.Value[1]); + break; + + default: + throw new ArgumentException($"There is no realisation for [{relativePair.Key}] function"); + } + }); + return context.FindElements(formedBy); + } + + private RelativeSeleniumBy GetRelativeWithAbove(RelativeSeleniumBy formedBy, object savedArgument) + { + var typeArgument = savedArgument.GetType(); + + if (typeArgument == typeof(WebElement)) + { + return formedBy.Above((WebElement)savedArgument); + } + + if (typeArgument == typeof(By)) + { + return formedBy.Above((By)savedArgument); + } + + if (typeArgument == typeof(IElement)) + { + return formedBy.Above(((IElement)savedArgument).Locator); + } + + throw new ArgumentException(ErrorMessageForType(typeArgument)); + } + + private RelativeSeleniumBy GetRelativeWithBelow(RelativeSeleniumBy formedBy, object savedArgument) + { + var typeArgument = savedArgument.GetType(); + + if (typeArgument == typeof(WebElement)) + { + return formedBy.Below((WebElement)savedArgument); + } + + if (typeArgument == typeof(By)) + { + return formedBy.Below((By)savedArgument); + } + + if (typeArgument == typeof(IElement)) + { + return formedBy.Below(((IElement)savedArgument).Locator); + } + + throw new ArgumentException(ErrorMessageForType(typeArgument)); + } + + private RelativeSeleniumBy GetRelativeWithRight(RelativeSeleniumBy formedBy, object savedArgument) + { + var typeArgument = savedArgument.GetType(); + + if (typeArgument == typeof(WebElement)) + { + return formedBy.RightOf((WebElement)savedArgument); + } + + if (typeArgument == typeof(By)) + { + return formedBy.RightOf((By)savedArgument); + } + + if (typeArgument == typeof(IElement)) + { + return formedBy.RightOf(((IElement)savedArgument).Locator); + } + + throw new ArgumentException(ErrorMessageForType(typeArgument)); + } + + private RelativeSeleniumBy GetRelativeWithLeft(RelativeSeleniumBy formedBy, object savedArgument) + { + var typeArgument = savedArgument.GetType(); + + if (typeArgument == typeof(WebElement)) + { + return formedBy.LeftOf((WebElement)savedArgument); + } + + if (typeArgument == typeof(By)) + { + return formedBy.LeftOf((By)savedArgument); + } + + if (typeArgument == typeof(IElement)) + { + return formedBy.LeftOf(((IElement)savedArgument).Locator); + } + + throw new ArgumentException(ErrorMessageForType(typeArgument)); + } + + private RelativeSeleniumBy GetRelativeWitNear(RelativeSeleniumBy formedBy, object locator, int valueInPixelsForTheNearLocator) + { + var typeArgument = locator.GetType(); + if (typeArgument == typeof(WebElement)) + { + return formedBy.Near((WebElement)locator, valueInPixelsForTheNearLocator); + } + if (typeArgument == typeof(By)) + { + return formedBy.Near((By)locator, valueInPixelsForTheNearLocator); + } + if (typeArgument == typeof(IElement)) + { + return formedBy.Near(((IElement)locator).Locator, valueInPixelsForTheNearLocator); + } + + throw new ArgumentException(ErrorMessageForType(typeArgument)); + } + + private string ErrorMessageForType(Type typeArgument) => $"There is no realisation for [{typeArgument}] type"; + } +} diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Aquality.Selenium.Tests.csproj b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Aquality.Selenium.Tests.csproj index ba28e051..d1ad05fd 100644 --- a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Aquality.Selenium.Tests.csproj +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Aquality.Selenium.Tests.csproj @@ -45,6 +45,9 @@ Always + + Always + Always diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/RelativeLocatorsForCustomFormTests.cs b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/RelativeLocatorsForCustomFormTests.cs new file mode 100644 index 00000000..a1561c38 --- /dev/null +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/RelativeLocatorsForCustomFormTests.cs @@ -0,0 +1,196 @@ +using Aquality.Selenium.Browsers; +using NUnit.Framework; +using Aquality.Selenium.Elements.Interfaces; +using Aquality.Selenium.Locators; +using By = OpenQA.Selenium.By; +using Aquality.Selenium.Tests.Integration.TestApp.LoginFormForRelativeLocators.Forms; +using OpenQA.Selenium; + +namespace Aquality.Selenium.Tests.Integration +{ + internal class RelativeLocatorsForCustomFormTests : UITest + { + private readonly LoginForm loginForm = new(); + private const string buttonLocator = "//button"; + private const string inputLocator = "//input"; + + private static IElementFactory ElementFactory => AqualityServices.Get(); + private const string Above = "above"; + private const string Below = "below"; + private const string Left = "left"; + private const string Right = "right"; + private const string Near = "near"; + private const string SeleniumRelative = "selenium relative"; + private const string Xpath = "xpath"; + private const string WebElement = "web element"; + private const string AqualityElement = "aquality element"; + + private static string MessageError(string gotWith, string gotBy) => $"Text of element got with [{gotWith}] by [{gotBy}] is not expected"; + private static string МessageForInputField(string logicKey) => $"Message text with key - [{logicKey}]"; + + [SetUp] + public void Before() + { + loginForm.Open(); + } + + [Test] + public void Should_BePossibleTo_AboveWithDifferentParametersType() + { + var userNameTextBox = loginForm.UserNameTextBox; + userNameTextBox.SendKeys(МessageForInputField("UserName")); + var universityTextBox = loginForm.UniversityTextBox; + + var actualUniversityTextBoxGotWithByXpath = + ElementFactory.GetTextBox(RelativeAqualityBy + .WithLocator(By.XPath(inputLocator)) + .Above(By.XPath(LoginForm.IdLocatorUniversityTextBox)), + LoginForm.ElementNameUserNameTextBox); + + var actualUniversityTextBoxGotWithWebElement = + ElementFactory.GetTextBox(RelativeAqualityBy + .WithLocator(By.XPath(inputLocator)) + .Above(universityTextBox.GetElement()), + LoginForm.ElementNameUserNameTextBox); + + var actualUniversityTextBoxGotWithAqualityElement = + ElementFactory.GetTextBox(RelativeAqualityBy.WithLocator(By.XPath(inputLocator)).Above(universityTextBox), + LoginForm.ElementNameUserNameTextBox); + + var actualWebElementUniversityTextBoxGotBySeleniumRelative = + AqualityServices.Browser.Driver.FindElement(RelativeBy + .WithLocator(By.XPath(inputLocator)) + .Above(By.XPath(LoginForm.IdLocatorUniversityTextBox))); + + var expectedText = userNameTextBox.Value; + var _expectedText = actualWebElementUniversityTextBoxGotBySeleniumRelative.GetAttribute("value"); + + Assert.Multiple(() => + { + Assert.AreEqual(expectedText, actualUniversityTextBoxGotWithByXpath.Value, MessageError(Above, SeleniumRelative)); + Assert.AreEqual(expectedText, actualUniversityTextBoxGotWithWebElement.Value, MessageError(Above, Xpath)); + Assert.AreEqual(expectedText, actualUniversityTextBoxGotWithAqualityElement.Value, MessageError(Above, WebElement)); + Assert.AreEqual(expectedText, actualWebElementUniversityTextBoxGotBySeleniumRelative.GetAttribute("value"), MessageError(Above, AqualityElement)); + }); + } + + [Test] + public void Should_BePossibleTo_BelowWithDifferentParametersType() + { + var userNameTextBox = loginForm.UserNameTextBox; + var universityTextBox = loginForm.UniversityTextBox; + universityTextBox.SendKeys(МessageForInputField("University")); + + var actualUniversityTextBoxGotWithByXpath = + ElementFactory.GetTextBox(RelativeAqualityBy + .WithLocator(By.XPath(inputLocator)) + .Below(By.XPath(LoginForm.IdLocatorUserNameTextBox)), + LoginForm.ElementNameUniversityTextBox); + + var actualUniversityTextBoxGotWithWebElement = + ElementFactory.GetTextBox(RelativeAqualityBy + .WithLocator(By.XPath(inputLocator)) + .Below(userNameTextBox.GetElement()), + LoginForm.ElementNameUniversityTextBox); + + var actualUniversityTextBoxGotWithAqualityElement = + ElementFactory.GetTextBox(RelativeAqualityBy.WithLocator(By.XPath(inputLocator)).Below(userNameTextBox), + LoginForm.ElementNameUniversityTextBox); + + var actualWebElementUniversityTextBoxGotBySeleniumRelative = + AqualityServices.Browser.Driver.FindElement(RelativeBy + .WithLocator(By.XPath(inputLocator)) + .Below(By.XPath(LoginForm.IdLocatorUserNameTextBox))); + + var expectedText = universityTextBox.Value; + + Assert.Multiple(() => + { + Assert.AreEqual(expectedText, actualUniversityTextBoxGotWithByXpath.Value, MessageError(Below, Xpath )); + Assert.AreEqual(expectedText, actualUniversityTextBoxGotWithWebElement.Value, MessageError(Below, WebElement)); + Assert.AreEqual(expectedText, actualUniversityTextBoxGotWithAqualityElement.Value, MessageError(Below, AqualityElement)); + Assert.AreEqual(expectedText, actualWebElementUniversityTextBoxGotBySeleniumRelative.GetAttribute("value"), MessageError(Below, SeleniumRelative)); + }); + } + + [Test] + public void Should_BePossibleTo_LeftWithDifferentParametersType() + { + var loginButton = loginForm.LoginButton; + var cancelButton = loginForm.CancelButton; + + var actualLoginButtonGotWithByXpath = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(buttonLocator)) + .Left(By.XPath(LoginForm.IdLocatorCancelButton)), "actualLoginButtonGotWithByXpath"); + + var actualLoginButtonGotWithWebElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(buttonLocator)) + .Left(cancelButton.GetElement()), "actualLoginButtonGotWithWebElemen"); + + var actualLoginButtonGotWithAqualityElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(buttonLocator)) + .Left(cancelButton), "actualLoginButtonGotWithAqualityElement"); + + var actualWebElementLoginButtonGotBySeleniumRelative = + AqualityServices.Browser.Driver.FindElement(RelativeBy + .WithLocator(By.XPath(buttonLocator)) + .LeftOf(By.XPath(LoginForm.IdLocatorCancelButton))); + + var expectedText = loginButton.Text; + + Assert.Multiple(() => + { + Assert.AreEqual(expectedText, actualLoginButtonGotWithByXpath.Text, MessageError(Left, SeleniumRelative)); + Assert.AreEqual(expectedText, actualLoginButtonGotWithWebElement.Text, MessageError(Left, Xpath)); + Assert.AreEqual(expectedText, actualLoginButtonGotWithAqualityElement.Text, MessageError(Left, WebElement)); + Assert.AreEqual(expectedText, actualWebElementLoginButtonGotBySeleniumRelative.Text, MessageError(Left, AqualityElement)); + }); + } + + [Test] + public void Should_BePossibleTo_RightWithDifferentParametersType() + { + var loginButton = loginForm.LoginButton; + var cancelButton = loginForm.CancelButton; + + var actualLoginButtonGotWithByXpath = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(buttonLocator)) + .Right(By.XPath(LoginForm.IdLocatorLoginButton)), "actualCancelButtonGotWithByXpath"); + + var actualLoginButtonGotWithWebElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(buttonLocator)) + .Right(loginButton.GetElement()), "actualCancelButtonGotWithWebElemen"); + + var actualLoginButtonGotWithAqualityElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(buttonLocator)) + .Right(loginButton), "actualCancelButtonGotWithAqualityElement"); + + var actualWebElementLoginButtonGotBySeleniumRelative = + AqualityServices.Browser.Driver.FindElement(RelativeBy + .WithLocator(By.XPath(buttonLocator)) + .RightOf(By.XPath(LoginForm.IdLocatorLoginButton))); + + var expectedText = cancelButton.Text; + + Assert.Multiple(() => + { + Assert.AreEqual(expectedText, actualLoginButtonGotWithByXpath.Text, MessageError(Right, SeleniumRelative)); + Assert.AreEqual(expectedText, actualLoginButtonGotWithWebElement.Text, MessageError(Right, Xpath)); + Assert.AreEqual(expectedText, actualLoginButtonGotWithAqualityElement.Text, MessageError(Right, WebElement)); + Assert.AreEqual(expectedText, actualWebElementLoginButtonGotBySeleniumRelative.Text, MessageError(Right, AqualityElement)); + }); + } + + [Test] + public void Should_BePossibleTo_NearWithDifferentParametersType() + { + //TODO! + } + + [Test] + public void Should_BePossibleTo_AboveBelowLeftWrightAboveWithDifferentParametersType() + { + //TODO! + } + } +} diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/RelativeLocatorsTests.cs b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/RelativeLocatorsTests.cs new file mode 100644 index 00000000..8339e367 --- /dev/null +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/RelativeLocatorsTests.cs @@ -0,0 +1,230 @@ +using Aquality.Selenium.Browsers; +using Aquality.Selenium.Tests.Integration.TestApp.TheInternet.Forms; +using NUnit.Framework; +using Aquality.Selenium.Elements.Interfaces; +using Aquality.Selenium.Locators; +using By = OpenQA.Selenium.By; +using OpenQA.Selenium; + +namespace Aquality.Selenium.Tests.Integration +{ + internal class RelativeLocatorsTests : UITest //TODO! This code should be removed during refactoring + { + private readonly ChallengingDomForm challengingDomForm = new ChallengingDomForm(); + private const string labelLocatorCell = "//td"; + private static IElementFactory ElementFactory => AqualityServices.Get(); + private const string Above = "above"; + private const string Below = "below"; + private const string Left = "left"; + private const string Right = "right"; + private const string SeleniumRelative = "selenium relative"; + private const string Xpath = "xpath"; + private const string WebElement = "web element"; + private const string AqualityElement = "aquality element"; + + private static string MessageError(string gotWith, string gotBy) => $"Text of element got with [{gotWith}] by [{gotBy}] is not expected"; + + [SetUp] + public void Before() + { + challengingDomForm.Open(); + } + + [Test] + public void Should_BePossibleTo_AboveWithDifferentParametersType() + { + var cellInRow5Column5 = challengingDomForm.CellInRow5Column5; + var cellInRow3Column5 = challengingDomForm.CellInRow3Column5; + + var actualCellRaw3Column5GotWithByXpath = + ElementFactory.GetLabel(RelativeAqualityBy + .WithLocator(By.XPath(labelLocatorCell)) + .Above(By.XPath(ChallengingDomForm.LocatorCellRow5Column5)), + ChallengingDomForm.ElementNameRow5Column5); + + var actualCellRaw3Column5GotWithWebElement = + ElementFactory.GetLabel(RelativeAqualityBy + .WithLocator(By.XPath(labelLocatorCell)) + .Above(cellInRow5Column5.GetElement()), + ChallengingDomForm.ElementNameRow3Column5); + + var actualCellRaw3Column5GotWithAqualityElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)).Above(cellInRow5Column5), + ChallengingDomForm.ElementNameRow3Column5); + + var actualWebElementCellRaw3Column5GotBySeleniumRelative = + AqualityServices.Browser.Driver.FindElement(RelativeBy + .WithLocator(By.XPath(labelLocatorCell)) + .Above(By.XPath(ChallengingDomForm.LocatorCellRow5Column5))); + + var expectedText = cellInRow3Column5.Text; + + Assert.Multiple(() => + { + Assert.AreEqual(expectedText, actualWebElementCellRaw3Column5GotBySeleniumRelative.Text, MessageError(Above, SeleniumRelative)); + Assert.AreEqual(expectedText, actualCellRaw3Column5GotWithByXpath.Text, MessageError(Above, Xpath)); + Assert.AreEqual(expectedText, actualCellRaw3Column5GotWithWebElement.Text, MessageError(Above, WebElement)); + Assert.AreEqual(expectedText, actualCellRaw3Column5GotWithAqualityElement.Text, MessageError(Above, AqualityElement)); + }); + } + + [Test] + public void Should_BePossibleTo_BelowWithDifferentParametersType() + { + var cellInRow5Column5 = challengingDomForm.CellInRow5Column5; + var cellInRow7Column5 = challengingDomForm.CellInRow7Column5; + + var actualCellRaw7Column5GotWithByXpath = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Below(By.XPath(ChallengingDomForm.LocatorCellRow5Column5)), ChallengingDomForm.ElementNameRow7Column5); + + var actualCellRaw7Column5GotWithWebElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Below(cellInRow5Column5.GetElement()), ChallengingDomForm.ElementNameRow7Column5); + + var actualCellRaw7Column5GotWithAqualityElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Below(cellInRow5Column5), ChallengingDomForm.ElementNameRow7Column5); + + var actualWebElementCellRaw7Column5GotBySeleniumRelative = + AqualityServices.Browser.Driver.FindElement(RelativeBy + .WithLocator(By.XPath(labelLocatorCell)) + .Below(By.XPath(ChallengingDomForm.LocatorCellRow5Column5))); + + var expectedText = cellInRow7Column5.Text; + + Assert.Multiple(() => + { + Assert.AreEqual(expectedText, actualWebElementCellRaw7Column5GotBySeleniumRelative.Text, MessageError(Below, SeleniumRelative)); + Assert.AreEqual(expectedText, actualCellRaw7Column5GotWithByXpath.Text, MessageError(Below, Xpath)); + Assert.AreEqual(expectedText, actualCellRaw7Column5GotWithWebElement.Text, MessageError(Below, WebElement)); + Assert.AreEqual(expectedText, actualCellRaw7Column5GotWithAqualityElement.Text, MessageError(Below, AqualityElement)); + }); + } + + [Test] + public void Should_BePossibleTo_LeftWithDifferentParametersType() + { + var cellInRow5Column5 = challengingDomForm.CellInRow5Column5; + var cellInRow5Column3 = challengingDomForm.CellInRow5Column3; + + var actualCellRaw5Column3GotWithByXpath = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Left(By.XPath(ChallengingDomForm.LocatorCellRow5Column5)), ChallengingDomForm.ElementNameRow5Column3); + + var actualCellRaw5Column3GotWithWebElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Left(cellInRow5Column5.GetElement()), ChallengingDomForm.ElementNameRow5Column3); + + var actualCellRaw5Column3GotWithAqualityElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Left(cellInRow5Column5), ChallengingDomForm.ElementNameRow5Column3); + + var actualWebElementCellRaw5Column3GotBySeleniumRelative = + AqualityServices.Browser.Driver.FindElement(RelativeBy + .WithLocator(By.XPath(labelLocatorCell)) + .LeftOf(By.XPath(ChallengingDomForm.LocatorCellRow5Column5))); + + var expectedText = cellInRow5Column3.Text; + + Assert.Multiple(() => + { + Assert.AreEqual(expectedText, actualWebElementCellRaw5Column3GotBySeleniumRelative.Text, MessageError(Left, SeleniumRelative)); + Assert.AreEqual(expectedText, actualCellRaw5Column3GotWithByXpath.Text, MessageError(Left, Xpath)); + Assert.AreEqual(expectedText, actualCellRaw5Column3GotWithWebElement.Text, MessageError(Left, WebElement)); + Assert.AreEqual(expectedText, actualCellRaw5Column3GotWithAqualityElement.Text, MessageError(Left, AqualityElement)); + }); + } + + [Test] + public void Should_BePossibleTo_RightWithDifferentParametersType() + { + var cellInRow5Column5 = challengingDomForm.CellInRow5Column5; + var cellInRow5Column7 = challengingDomForm.CellInRow5Column7; + + var actualCellRaw5Column7GotWithByXpath = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Right(By.XPath(ChallengingDomForm.LocatorCellRow5Column5)), ChallengingDomForm.ElementNameRow5Column7); + + var actualCellRaw5Column7GotWithWebElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Right(cellInRow5Column5.GetElement()), ChallengingDomForm.ElementNameRow5Column7); + + var actualCellRaw5Column7GotWithAqualityElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Right(cellInRow5Column5), ChallengingDomForm.ElementNameRow5Column7); + + var actualWebElementCellRaw5Column7GotBySeleniumRelative = + AqualityServices.Browser.Driver.FindElement(RelativeBy + .WithLocator(By.XPath(labelLocatorCell)) + .RightOf(By.XPath(ChallengingDomForm.LocatorCellRow5Column5))); + + var expectedText = cellInRow5Column7.Text; + + Assert.Multiple(() => + { + Assert.AreEqual(expectedText, actualWebElementCellRaw5Column7GotBySeleniumRelative.Text, MessageError(Right, SeleniumRelative)); + Assert.AreEqual(expectedText, actualCellRaw5Column7GotWithByXpath.Text, MessageError(Right, Xpath)); + Assert.AreEqual(expectedText, actualCellRaw5Column7GotWithWebElement.Text, MessageError(Right, WebElement)); + Assert.AreEqual(expectedText, actualCellRaw5Column7GotWithAqualityElement.Text, MessageError(Right, AqualityElement)); + }); + } + + [Test] + public void Should_BePossibleTo_AboveBelowLeftWrightAboveWithDifferentParametersType() + { + var cellInRow5Column3 = challengingDomForm.CellInRow5Column3; + var cellInRow5Column5 = challengingDomForm.CellInRow5Column5; + var cellInRow5Column7 = challengingDomForm.CellInRow5Column7; + var cellInRow3Column5 = challengingDomForm.CellInRow3Column5; + var cellInRow7Column5 = challengingDomForm.CellInRow7Column5; + + var actualCellRaw3Column5GotWithByXpath = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Above(By.XPath(ChallengingDomForm.LocatorCellRow7Column5)) + .Below(By.XPath(ChallengingDomForm.LocatorCellRow3Column5)) + .Left(By.XPath(ChallengingDomForm.LocatorCellRow5Column7)) + .Right(By.XPath(ChallengingDomForm.LocatorCellRow5Column3)) + .Above(By.XPath(ChallengingDomForm.LocatorCellRow7Column5)) + , ChallengingDomForm.ElementNameRow3Column5); + + var actualCellRaw3Column5GotWithWebElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Above(cellInRow7Column5.GetElement()) + .Below(cellInRow3Column5.GetElement()) + .Left(cellInRow5Column7.GetElement()) + .Right(cellInRow5Column3.GetElement()) + .Above(cellInRow7Column5.GetElement()) + , ChallengingDomForm.ElementNameRow3Column5); + + var actualCellRaw3Column5GotWithAqualityElement = + ElementFactory.GetLabel(RelativeAqualityBy.WithLocator(By.XPath(labelLocatorCell)) + .Above(cellInRow7Column5) + .Below(cellInRow3Column5) + .Left(cellInRow5Column7) + .Right(cellInRow5Column3) + .Above(cellInRow7Column5) + , ChallengingDomForm.ElementNameRow3Column5); + + var actualWebElementCellRaw3Column5GotBySeleniumRelative = + AqualityServices.Browser.Driver.FindElement(RelativeBy + .WithLocator(By.XPath(labelLocatorCell)) + .Above(By.XPath(ChallengingDomForm.LocatorCellRow7Column5)) + .Below(By.XPath(ChallengingDomForm.LocatorCellRow3Column5)) + .LeftOf(By.XPath(ChallengingDomForm.LocatorCellRow5Column7)) + .RightOf(By.XPath(ChallengingDomForm.LocatorCellRow5Column3)) + .Above(By.XPath(ChallengingDomForm.LocatorCellRow7Column5))); + + var expectedText = cellInRow5Column5.Text; + var gotWith = $"{Above} {Below} {Left} {Right} {Above}"; + + Assert.Multiple(() => + { + Assert.AreEqual(expectedText, actualWebElementCellRaw3Column5GotBySeleniumRelative.Text, MessageError(gotWith, SeleniumRelative)); + Assert.AreEqual(expectedText, actualCellRaw3Column5GotWithByXpath.Text, MessageError(gotWith, Xpath)); + Assert.AreEqual(expectedText, actualCellRaw3Column5GotWithWebElement.Text, MessageError(gotWith, WebElement)); + Assert.AreEqual(expectedText, actualCellRaw3Column5GotWithAqualityElement.Text, MessageError(gotWith, AqualityElement)); + }); + } + } +} diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/LoginFormForRelativeLocators/Forms/LoginForm.cs b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/LoginFormForRelativeLocators/Forms/LoginForm.cs new file mode 100644 index 00000000..8afd949a --- /dev/null +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/LoginFormForRelativeLocators/Forms/LoginForm.cs @@ -0,0 +1,59 @@ +using Aquality.Selenium.Browsers; +using Aquality.Selenium.Elements.Interfaces; +using Aquality.Selenium.Forms; +using OpenQA.Selenium; +using System; +using System.IO; + +namespace Aquality.Selenium.Tests.Integration.TestApp.LoginFormForRelativeLocators.Forms +{ + internal class LoginForm : Form + { + private static string BaseUrl => Path.Combine($"{AppDomain.CurrentDomain.BaseDirectory}", "Resources", "LoginFormForRelativeLocators.html"); + + public LoginForm() : base(By.XPath("//h1[contains(text(),'Student Login Form')]"), "Login form") + { + } + + public void Open() + { + AqualityServices.Browser.GoTo($"file://{BaseUrl}"); + AqualityServices.Browser.WaitForPageToLoad(); + AqualityServices.Browser.Maximize(); + } + + public static readonly string IdLocatorUserNameTextBox = "//input[@id='user-name']"; + public static readonly string IdLocatorSurnameTextBox = "//input[@id='user-name']"; + public static readonly string IdLocatorPasswordTextBox = "//input[@id='password']"; + + public static readonly string IdLocatorUniversityTextBox = "//input[@id='university']"; + public static readonly string IdLocatorPhoneTextBox = "//input[@id='phone']"; + public static readonly string IdLocatorAdressTextBox = "//input[@id='adress']"; + + public static readonly string IdLocatorFacultyTextBox = "//input[@id='faculty']"; + public static readonly string IdLocatorLoginButton = "//button[@id='submit-btn']"; + public static readonly string IdLocatorCancelButton = "//button[@id='cancel-btn']"; + + public static readonly string ElementNameUserNameTextBox = "User Name Text Box"; + public static readonly string ElementNameSurnameTextBox = "Surname Name Text Box"; + public static readonly string ElementNamePasswordTextBox = "Password Text Box"; + + public static readonly string ElementNameUniversityTextBox = "University Text Box"; + public static readonly string ElementNamePhoneTextBox = "Phone Text Box"; + public static readonly string ElementNameAdressTextBox = "Adress Text Box"; + + public static readonly string ElementNameFacultyTextBox = "Faculty Text Box"; + public static readonly string ElementNameLoginButton = "Login Button"; + public static readonly string ElementNameCancelButton = "Cancel Button"; + + public ITextBox UserNameTextBox => ElementFactory.GetTextBox(By.XPath(IdLocatorUserNameTextBox), ElementNameUserNameTextBox); + public ITextBox SurnameTextBox => ElementFactory.GetTextBox(By.XPath(IdLocatorSurnameTextBox), ElementNameSurnameTextBox); + public ITextBox PasswordTextBox => ElementFactory.GetTextBox(By.XPath(IdLocatorPasswordTextBox), ElementNamePasswordTextBox); + public ITextBox UniversityTextBox => ElementFactory.GetTextBox(By.XPath(IdLocatorUniversityTextBox), ElementNameUniversityTextBox); + public ITextBox PhoneTextBox => ElementFactory.GetTextBox(By.XPath(IdLocatorPhoneTextBox), ElementNamePhoneTextBox); + public ITextBox AdressTextBox => ElementFactory.GetTextBox(By.XPath(IdLocatorAdressTextBox), ElementNameAdressTextBox); + public ITextBox FacultyTextBox => ElementFactory.GetTextBox(By.XPath(IdLocatorFacultyTextBox), ElementNameFacultyTextBox); + public IButton LoginButton => ElementFactory.GetButton(By.XPath(IdLocatorLoginButton), ElementNameLoginButton); + public IButton CancelButton => ElementFactory.GetButton(By.XPath(IdLocatorCancelButton), ElementNameCancelButton); + } +} diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheInternet/Forms/ChallengingDomForm.cs b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheInternet/Forms/ChallengingDomForm.cs new file mode 100644 index 00000000..0e564ce9 --- /dev/null +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheInternet/Forms/ChallengingDomForm.cs @@ -0,0 +1,40 @@ +using Aquality.Selenium.Elements.Interfaces; +using OpenQA.Selenium; + +namespace Aquality.Selenium.Tests.Integration.TestApp.TheInternet.Forms +{ + internal class ChallengingDomForm : TheInternetForm //TODO! This code should be removed during refactoring + { + public ChallengingDomForm() : base(By.XPath("//h3[contains(text(),'Challenging DOM')]"), "Challenging dom form") + { + } + + protected override string UrlPart => "challenging_dom"; + + public static readonly string ElementNameRow3Column5 = "Cell in row 3 column 5"; + public static readonly string ElementNameRow5Column5 = "Cell in row 5 column 5"; + public static readonly string ElementNameRow7Column5 = "Cell in row 7 column 5"; + public static readonly string ElementNameRow5Column7 = "Cell in row 5 column 7"; + public static readonly string ElementNameRow5Column3 = "Cell in row 5 column 3"; + public static readonly string ElementNameRow1Column1 = "Cell in row 1 column 1"; + public static readonly string ElementNameRow2Column1 = "Cell in row 2 column 1"; + + public static readonly string LocatorCellRow5Column5 = "//tr[5]/td[5]"; + public static readonly string LocatorCellRow1Column5 = "//tr[1]/td[5]"; + public static readonly string LocatorCellRow3Column5 = "//tr[3]/td[5]"; + public static readonly string LocatorCellRow7Column5 = "//tr[7]/td[5]"; + public static readonly string LocatorCellRow5Column3 = "//tr[5]/td[3]"; + public static readonly string LocatorCellRow5Column7 = "//tr[5]/td[7]"; + public static readonly string LocatorCellRow1Column1 = "//tr[1]/td[1]"; + public static readonly string LocatorCellRow2Column1 = "//tr[2]/td[1]"; + + + public ILabel CellInRow3Column5 => ElementFactory.GetLabel(By.XPath(LocatorCellRow3Column5), ElementNameRow3Column5); + public ILabel CellInRow5Column5 => ElementFactory.GetLabel(By.XPath(LocatorCellRow5Column5), ElementNameRow5Column5); + public ILabel CellInRow7Column5 => ElementFactory.GetLabel(By.XPath(LocatorCellRow7Column5), ElementNameRow7Column5); + public ILabel CellInRow5Column7 => ElementFactory.GetLabel(By.XPath(LocatorCellRow5Column7), ElementNameRow5Column7); + public ILabel CellInRow5Column3 => ElementFactory.GetLabel(By.XPath(LocatorCellRow5Column3), ElementNameRow5Column3); + public ILabel CellInRow1Column1 => ElementFactory.GetLabel(By.XPath(LocatorCellRow1Column1), ElementNameRow1Column1); + public ILabel CellInRow2Column1 => ElementFactory.GetLabel(By.XPath(LocatorCellRow2Column1), ElementNameRow2Column1); + } +} diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/LoginFormForRelativeLocators.html b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/LoginFormForRelativeLocators.html new file mode 100644 index 00000000..1d52c86b --- /dev/null +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/LoginFormForRelativeLocators.html @@ -0,0 +1,120 @@ + + + + + Login Page + + + +

Student Login Form

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ Remember me +
+ +
+ + \ No newline at end of file