Skip to content

Commit 2d6ed48

Browse files
committed
[Feature/Add_Relative_Locators] Add interfaces IRelativeBy, IRelativeByAqualityElement, IRelativeByWebElement
1 parent d7df9aa commit 2d6ed48

File tree

5 files changed

+95
-12
lines changed

5 files changed

+95
-12
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package aquality.selenium.locators;
2+
3+
import org.openqa.selenium.By;
4+
5+
public interface IRelativeBy {
6+
RelativeBy above(By by);
7+
RelativeBy below(By by);
8+
RelativeBy toLeftOf(By by);
9+
RelativeBy toRightOf(By by);
10+
RelativeBy near(By by);
11+
RelativeBy near(By by, int atMostDistanceInPixels);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package aquality.selenium.locators;
2+
3+
import aquality.selenium.elements.interfaces.IElement;
4+
5+
public interface IRelativeByAqualityElement {
6+
RelativeBy above(IElement element);
7+
RelativeBy below(IElement element);
8+
RelativeBy toLeftOf(IElement element);
9+
RelativeBy toRightOf(IElement element);
10+
RelativeBy near(IElement element);
11+
RelativeBy near(IElement element, int atMostDistanceInPixels);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package aquality.selenium.locators;
2+
3+
import org.openqa.selenium.WebElement;
4+
5+
public interface IRelativeByWebElement {
6+
RelativeBy above(WebElement element);
7+
RelativeBy below(WebElement element);
8+
RelativeBy toLeftOf(WebElement element);
9+
RelativeBy toRightOf(WebElement element);
10+
RelativeBy near(WebElement element);
11+
RelativeBy near(WebElement element, int atMostDistanceInPixels);
12+
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import java.util.List;
1010

11-
public class RelativeBy extends By {
11+
public class RelativeBy extends By implements IRelativeByAqualityElement, IRelativeByWebElement, IRelativeBy {
1212
private By by;
1313

1414
public RelativeBy(By by) {
@@ -75,9 +75,35 @@ public RelativeBy toRightOf(By by) {
7575
return new RelativeBy(this.by);
7676
}
7777

78+
public RelativeBy near(IElement element) {
79+
by = RelativeLocator.with(by).near(element.getElement());
80+
return new RelativeBy(this.by);
81+
}
82+
83+
public RelativeBy near(WebElement element) {
84+
by = RelativeLocator.with(by).near(element);
85+
return new RelativeBy(this.by);
86+
}
87+
88+
public RelativeBy near(By by) {
89+
this.by = RelativeLocator.with(this.by).near(by);
90+
return new RelativeBy(this.by);
91+
}
7892

93+
public RelativeBy near(IElement element, int atMostDistanceInPixels) {
94+
by = RelativeLocator.with(by).near(element.getElement(), atMostDistanceInPixels);
95+
return new RelativeBy(this.by);
96+
}
7997

98+
public RelativeBy near(WebElement element, int atMostDistanceInPixels) {
99+
by = RelativeLocator.with(by).near(element, atMostDistanceInPixels);
100+
return new RelativeBy(this.by);
101+
}
80102

103+
public RelativeBy near(By by, int atMostDistanceInPixels) {
104+
this.by = RelativeLocator.with(this.by).near(by, atMostDistanceInPixels);
105+
return new RelativeBy(this.by);
106+
}
81107

82108
@Override
83109
public List<WebElement> findElements(SearchContext context) {

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ public void testAboveBelowLeftRight() {
157157
ILabel cellInRow5Column3 = elementFactory.getLabel(By.xpath(labelLocatorCellRow5Column3), "CellInRow5Column3");
158158
ILabel cellInRow7Column5 = elementFactory.getLabel(By.xpath(labelLocatorCellRow7Column5), "CellInRow7Column5");
159159

160-
161-
162160
ILabel actualCellRaw5Column5GotWithAqualityElement =
163161
elementFactory.getLabel(with(By.xpath(labelLocatorCell))
164162
.above(cellInRow7Column5)
@@ -167,18 +165,41 @@ public void testAboveBelowLeftRight() {
167165
.toLeftOf(cellInRow5Column7)
168166
.above(cellInRow7Column5), nameElementRow5Column5);
169167

168+
ILabel actualCellRaw5Column5GotWithWebElement =
169+
elementFactory.getLabel(with(By.xpath(labelLocatorCell))
170+
.above(cellInRow7Column5.getElement())
171+
.below(cellInRow3Column5.getElement())
172+
.toRightOf(cellInRow5Column3.getElement())
173+
.toLeftOf(cellInRow5Column7.getElement())
174+
.above(cellInRow7Column5.getElement())
175+
, nameElementRow5Column5);
176+
177+
ILabel actualCellRaw5Column5GotWithXpath =
178+
elementFactory.getLabel(with(By.xpath(labelLocatorCell))
179+
.above(By.xpath(labelLocatorCellRow7Column5))
180+
.below(By.xpath(labelLocatorCellRow3Column5))
181+
.toRightOf(By.xpath(labelLocatorCellRow5Column3))
182+
.toLeftOf(By.xpath(labelLocatorCellRow5Column7))
183+
.above(By.xpath(labelLocatorCellRow7Column5))
184+
, nameElementRow5Column5);
185+
186+
WebElement actualWebElementCellRaw5Column5GotBySeleniumRelative =
187+
getBrowser().getDriver().findElement(RelativeLocator.with(By.xpath(labelLocatorCell))
188+
.above(By.xpath(labelLocatorCellRow7Column5))
189+
.below(By.xpath(labelLocatorCellRow3Column5))
190+
.toRightOf(By.xpath(labelLocatorCellRow5Column3))
191+
.toLeftOf(By.xpath(labelLocatorCellRow5Column7))
192+
.above(By.xpath(labelLocatorCellRow7Column5))
193+
);
170194

171-
String a = actualCellRaw5Column5GotWithAqualityElement.getText();
172-
int ss=2;
173-
174-
195+
checkDifferentTypesWithSoftAssert(
196+
actualCellRaw5Column5GotWithAqualityElement.getText(),
197+
actualCellRaw5Column5GotWithXpath.getText(),
198+
actualCellRaw5Column5GotWithWebElement.getText(),
199+
actualWebElementCellRaw5Column5GotBySeleniumRelative.getText(),
200+
cellInRow5Column5.getText());
175201
}
176202

177-
178-
179-
180-
181-
182203
public void checkDifferentTypesWithSoftAssert(String textAquality, String textWebElement, String textByXpath, String textSelenium, String expectedText) {
183204
SoftAssert softAssert = new SoftAssert();
184205
softAssert.assertEquals(textAquality, expectedText, friendlyMessage);

0 commit comments

Comments
 (0)