Skip to content

Commit d7df9aa

Browse files
committed
[Feature/Add_Relative_Locators] Add toLeftOf, toRightOf, toBelow locators
1 parent b08eed7 commit d7df9aa

File tree

2 files changed

+182
-2
lines changed

2 files changed

+182
-2
lines changed

src/main/java/aquality/selenium/locators/RelativeBy.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,55 @@ public RelativeBy above(By by) {
3030
return new RelativeBy(this.by);
3131
}
3232

33+
public RelativeBy below(IElement element) {
34+
by = RelativeLocator.with(by).below(element.getElement());
35+
return new RelativeBy(by);
36+
}
37+
38+
public RelativeBy below(WebElement element) {
39+
by = RelativeLocator.with(by).below(element);
40+
return new RelativeBy(by);
41+
}
42+
43+
public RelativeBy below(By by) {
44+
this.by = RelativeLocator.with(this.by).below(by);
45+
return new RelativeBy(this.by);
46+
}
47+
48+
public RelativeBy toLeftOf(IElement element) {
49+
by = RelativeLocator.with(by).toLeftOf(element.getElement());
50+
return new RelativeBy(by);
51+
}
52+
53+
public RelativeBy toLeftOf(WebElement element) {
54+
by = RelativeLocator.with(by).toLeftOf(element);
55+
return new RelativeBy(by);
56+
}
57+
58+
public RelativeBy toLeftOf(By by) {
59+
this.by = RelativeLocator.with(this.by).toLeftOf(by);
60+
return new RelativeBy(this.by);
61+
}
62+
63+
public RelativeBy toRightOf(IElement element) {
64+
by = RelativeLocator.with(by).toRightOf(element.getElement());
65+
return new RelativeBy(by);
66+
}
67+
68+
public RelativeBy toRightOf(WebElement element) {
69+
by = RelativeLocator.with(by).toRightOf(element);
70+
return new RelativeBy(by);
71+
}
72+
73+
public RelativeBy toRightOf(By by) {
74+
this.by = RelativeLocator.with(this.by).toRightOf(by);
75+
return new RelativeBy(this.by);
76+
}
77+
78+
79+
80+
81+
3382
@Override
3483
public List<WebElement> findElements(SearchContext context) {
3584
return context.findElements(by);

src/test/java/tests/integration/LocatorTests.java

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.openqa.selenium.By;
55
import org.openqa.selenium.WebElement;
66
import org.openqa.selenium.support.locators.RelativeLocator;
7+
import org.testng.annotations.BeforeMethod;
78
import org.testng.annotations.Test;
89
import org.testng.asserts.SoftAssert;
910
import tests.BaseTest;
@@ -15,14 +16,25 @@ public class LocatorTests extends BaseTest {
1516
private final String labelLocatorCell = "//td";
1617
private final String labelLocatorCellRow5Column5 = "//tr[5]/td[5]";
1718
private final String labelLocatorCellRow3Column5 = "//tr[3]/td[5]";
19+
private final String labelLocatorCellRow7Column5 = "//tr[7]/td[5]";
20+
private final String labelLocatorCellRow5Column3 = "//tr[5]/td[3]";
21+
private final String labelLocatorCellRow5Column7 = "//tr[5]/td[7]";
1822
private final String nameElementRow3Column5 = "expectedRow3Column5GotWithByXpath";
23+
private final String nameElementRow7Column5 = "expectedRow7Column5GotWithByXpath";
24+
private final String nameElementRow5Column3 = "expectedRow5Column3GotWithByXpath";
25+
private final String nameElementRow5Column7 = "expectedRow5Column7GotWithByXpath";
26+
private final String nameElementRow5Column5 = "expectedRow5Column5";
1927
private final String friendlyMessage = "Actual cell text is not equal expected";
2028

29+
@BeforeMethod
30+
public void beforeMethod() {
31+
navigate(TheInternetPage.CHALLENGING_DOM);
32+
}
33+
2134
@Test
2235
public void testAboveLocatorWithDifferentAboveParametersType() {
23-
navigate(TheInternetPage.CHALLENGING_DOM);
2436

25-
ILabel cellInRow3Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow3Column5), "CellInRow4Column5");
37+
ILabel cellInRow3Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow3Column5), "CellInRow3Column5");
2638
ILabel cellInRow5Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column5), "CellInRow5Column5");
2739

2840
ILabel actualCellRaw3Column5GotWithByXpath =
@@ -48,6 +60,125 @@ public void testAboveLocatorWithDifferentAboveParametersType() {
4860
cellInRow3Column5.getText());
4961
}
5062

63+
@Test
64+
public void testBelowLocatorWithDifferentBelowParametersType () {
65+
66+
ILabel cellInRow7Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow7Column5), "CellInRow7Column5");
67+
ILabel cellInRow5Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column5), "CellInRow5Column5");
68+
69+
ILabel actualCellRaw7Column5GotWithByXpath =
70+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).below(By.xpath(labelLocatorCellRow5Column5)),
71+
nameElementRow7Column5);
72+
73+
ILabel actualCellRaw7Column5GotWithWebElement =
74+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).below(cellInRow5Column5.getElement()),
75+
nameElementRow7Column5);
76+
77+
ILabel actualCellRaw7Column5GotWithAqualityElement =
78+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).below(cellInRow5Column5),
79+
nameElementRow7Column5);
80+
81+
WebElement actualWebElementCellRaw7Column5GotBySeleniumRelative =
82+
getBrowser().getDriver().findElement(RelativeLocator.with(By.xpath(labelLocatorCell)).below(By.xpath(labelLocatorCellRow5Column5)));
83+
84+
checkDifferentTypesWithSoftAssert(
85+
actualCellRaw7Column5GotWithAqualityElement.getText(),
86+
actualCellRaw7Column5GotWithWebElement.getText(),
87+
actualCellRaw7Column5GotWithByXpath.getText(),
88+
actualWebElementCellRaw7Column5GotBySeleniumRelative.getText(),
89+
cellInRow7Column5.getText());
90+
}
91+
92+
93+
@Test
94+
public void testToLeftOfLocatorWithDifferentToLeftOfParametersType () {
95+
96+
ILabel cellInRow5Column3 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column3), "CellInRow5Column3");
97+
ILabel cellInRow5Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column5), "CellInRow5Column5");
98+
99+
ILabel actualCellRaw5Column3GotWithByXpath =
100+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toLeftOf(By.xpath(labelLocatorCellRow5Column5)),
101+
nameElementRow5Column3);
102+
103+
ILabel actualCellRaw5Column3GotWithWebElement =
104+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toLeftOf(cellInRow5Column5.getElement()),
105+
nameElementRow5Column3);
106+
107+
ILabel actualCellRaw5Column3GotWithAqualityElement =
108+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toLeftOf(cellInRow5Column5),
109+
nameElementRow5Column3);
110+
111+
WebElement actualWebElementCellRaw5Column3GotBySeleniumRelative =
112+
getBrowser().getDriver().findElement(RelativeLocator.with(By.xpath(labelLocatorCell)).toLeftOf(By.xpath(labelLocatorCellRow5Column5)));
113+
114+
checkDifferentTypesWithSoftAssert(
115+
actualCellRaw5Column3GotWithAqualityElement.getText(),
116+
actualCellRaw5Column3GotWithWebElement.getText(),
117+
actualCellRaw5Column3GotWithByXpath.getText(),
118+
actualWebElementCellRaw5Column3GotBySeleniumRelative.getText(),
119+
cellInRow5Column3.getText());
120+
}
121+
122+
@Test
123+
public void testToRightOfLocatorWithDifferentToRightOfParametersType () {
124+
125+
ILabel cellInRow5Column7 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column7), "CellInRow5Column7");
126+
ILabel cellInRow5Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column5), "CellInRow5Column5");
127+
128+
ILabel actualCellRaw5Column7GotWithByXpath =
129+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toRightOf(By.xpath(labelLocatorCellRow5Column5)),
130+
nameElementRow5Column7);
131+
132+
ILabel actualCellRaw5Column7GotWithWebElement =
133+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toRightOf(cellInRow5Column5.getElement()),
134+
nameElementRow5Column3);
135+
136+
ILabel actualCellRaw5Column7GotWithAqualityElement =
137+
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toRightOf(cellInRow5Column5),
138+
nameElementRow5Column3);
139+
140+
WebElement actualWebElementCellRaw5Column7GotBySeleniumRelative =
141+
getBrowser().getDriver().findElement(RelativeLocator.with(By.xpath(labelLocatorCell)).toRightOf(By.xpath(labelLocatorCellRow5Column5)));
142+
143+
checkDifferentTypesWithSoftAssert(
144+
actualCellRaw5Column7GotWithAqualityElement.getText(),
145+
actualCellRaw5Column7GotWithWebElement.getText(),
146+
actualCellRaw5Column7GotWithByXpath.getText(),
147+
actualWebElementCellRaw5Column7GotBySeleniumRelative.getText(),
148+
cellInRow5Column7.getText());
149+
}
150+
151+
@Test
152+
public void testAboveBelowLeftRight() {
153+
154+
ILabel cellInRow3Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow3Column5), "CellInRow3Column5");
155+
ILabel cellInRow5Column7 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column7), "CellInRow5Column7");
156+
ILabel cellInRow5Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column5), "CellInRow5Column5");
157+
ILabel cellInRow5Column3 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column3), "CellInRow5Column3");
158+
ILabel cellInRow7Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow7Column5), "CellInRow7Column5");
159+
160+
161+
162+
ILabel actualCellRaw5Column5GotWithAqualityElement =
163+
elementFactory.getLabel(with(By.xpath(labelLocatorCell))
164+
.above(cellInRow7Column5)
165+
.below(cellInRow3Column5)
166+
.toRightOf(cellInRow5Column3)
167+
.toLeftOf(cellInRow5Column7)
168+
.above(cellInRow7Column5), nameElementRow5Column5);
169+
170+
171+
String a = actualCellRaw5Column5GotWithAqualityElement.getText();
172+
int ss=2;
173+
174+
175+
}
176+
177+
178+
179+
180+
181+
51182
public void checkDifferentTypesWithSoftAssert(String textAquality, String textWebElement, String textByXpath, String textSelenium, String expectedText) {
52183
SoftAssert softAssert = new SoftAssert();
53184
softAssert.assertEquals(textAquality, expectedText, friendlyMessage);

0 commit comments

Comments
 (0)