Skip to content

Commit b08eed7

Browse files
committed
[Feature/Add_Relative_Locators] Add layer for work with relative locators
1 parent d2d296e commit b08eed7

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package aquality.selenium.locators;
2+
3+
import aquality.selenium.elements.interfaces.IElement;
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.SearchContext;
6+
import org.openqa.selenium.WebElement;
7+
import org.openqa.selenium.support.locators.RelativeLocator;
8+
9+
import java.util.List;
10+
11+
public class RelativeBy extends By {
12+
private By by;
13+
14+
public RelativeBy(By by) {
15+
this.by = by;
16+
}
17+
18+
public RelativeBy above(IElement element) {
19+
by = RelativeLocator.with(by).above(element.getElement());
20+
return new RelativeBy(by);
21+
}
22+
23+
public RelativeBy above(WebElement element) {
24+
by = RelativeLocator.with(by).above(element);
25+
return new RelativeBy(by);
26+
}
27+
28+
public RelativeBy above(By by) {
29+
this.by = RelativeLocator.with(this.by).above(by);
30+
return new RelativeBy(this.by);
31+
}
32+
33+
@Override
34+
public List<WebElement> findElements(SearchContext context) {
35+
return context.findElements(by);
36+
}
37+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package aquality.selenium.locators;
2+
3+
import org.openqa.selenium.By;
4+
5+
public class RelativeBySupplier {
6+
public static RelativeBy with(By by) {
7+
return new RelativeBy(by);
8+
}
9+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package tests.integration;
2+
3+
import aquality.selenium.elements.interfaces.ILabel;
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.WebElement;
6+
import org.openqa.selenium.support.locators.RelativeLocator;
7+
import org.testng.annotations.Test;
8+
import org.testng.asserts.SoftAssert;
9+
import tests.BaseTest;
10+
import theinternet.TheInternetPage;
11+
12+
import static aquality.selenium.locators.RelativeBySupplier.with;
13+
14+
public class LocatorTests extends BaseTest {
15+
private final String labelLocatorCell = "//td";
16+
private final String labelLocatorCellRow5Column5 = "//tr[5]/td[5]";
17+
private final String labelLocatorCellRow3Column5 = "//tr[3]/td[5]";
18+
private final String nameElementRow3Column5 = "expectedRow3Column5GotWithByXpath";
19+
private final String friendlyMessage = "Actual cell text is not equal expected";
20+
21+
@Test
22+
public void testAboveLocatorWithDifferentAboveParametersType() {
23+
navigate(TheInternetPage.CHALLENGING_DOM);
24+
25+
ILabel cellInRow3Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow3Column5), "CellInRow4Column5");
26+
ILabel cellInRow5Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column5), "CellInRow5Column5");
27+
28+
ILabel actualCellRaw3Column5GotWithByXpath =
29+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).above(By.xpath(labelLocatorCellRow5Column5)),
30+
nameElementRow3Column5);
31+
32+
ILabel actualCellRaw3Column5GotWithWebElement =
33+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).above(cellInRow5Column5.getElement()),
34+
nameElementRow3Column5);
35+
36+
ILabel actualCellRaw3Column5GotWithAqualityElement =
37+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).above(cellInRow5Column5),
38+
nameElementRow3Column5);
39+
40+
WebElement actualWebElementCellRaw3Column5GotBySeleniumRelative =
41+
getBrowser().getDriver().findElement(RelativeLocator.with(By.xpath(labelLocatorCell)).above(By.xpath(labelLocatorCellRow5Column5)));
42+
43+
checkDifferentTypesWithSoftAssert(
44+
actualCellRaw3Column5GotWithAqualityElement.getText(),
45+
actualCellRaw3Column5GotWithWebElement.getText(),
46+
actualCellRaw3Column5GotWithByXpath.getText(),
47+
actualWebElementCellRaw3Column5GotBySeleniumRelative.getText(),
48+
cellInRow3Column5.getText());
49+
}
50+
51+
public void checkDifferentTypesWithSoftAssert(String textAquality, String textWebElement, String textByXpath, String textSelenium, String expectedText) {
52+
SoftAssert softAssert = new SoftAssert();
53+
softAssert.assertEquals(textAquality, expectedText, friendlyMessage);
54+
softAssert.assertEquals(textByXpath, expectedText, friendlyMessage);
55+
softAssert.assertEquals(textWebElement, expectedText, friendlyMessage);
56+
softAssert.assertEquals(textSelenium, expectedText, friendlyMessage);
57+
softAssert.assertAll();
58+
}
59+
}

src/test/java/theinternet/TheInternetPage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public enum TheInternetPage {
1010
LOGIN,
1111
REDIRECTOR,
1212
STATUS_CODES,
13-
TABLES;
13+
TABLES,
14+
CHALLENGING_DOM;
1415

1516
private static final String BASE_URL = "http://the-internet.herokuapp.com/";
1617

0 commit comments

Comments
 (0)