Skip to content

Commit dd5025d

Browse files
committed
[Feature/Add_Relative_Locators] Add test to near with distance positive, negative
1 parent 2592e61 commit dd5025d

File tree

2 files changed

+87
-37
lines changed

2 files changed

+87
-37
lines changed

src/test/java/automationpractice/forms/ChallengingDomForm.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66

77
public class ChallengingDomForm extends Form {
88

9+
public static final String LOCATOR_CHALLENGING_DOM_FORM = "//h3[contains(text(),'Challenging DOM')]";
910
private final String locatorCellRow5Column5 = "//tr[5]/td[5]";
1011
private final String locatorCellRow3Column5 = "//tr[3]/td[5]";
1112
private final String locatorCellRow7Column5 = "//tr[7]/td[5]";
1213
private final String locatorCellRow5Column3 = "//tr[5]/td[3]";
1314
private final String locatorCellRow5Column7 = "//tr[5]/td[7]";
14-
private final String locatorCellRow4Column5 = "//tr[4]/td[5]";
1515
private final String locatorCellRow1Column1 = "//tr[1]/td[1]";
1616
private final String locatorCellRow2Column1 = "//tr[2]/td[1]";
1717

18-
private ILabel cellInRow3Column5 = getElementFactory().getLabel(By.xpath(locatorCellRow3Column5), "CellInRow3Column5");
19-
private ILabel cellInRow5Column5 = getElementFactory().getLabel(By.xpath(locatorCellRow5Column5), "CellInRow5Column5");
20-
private ILabel cellInRow7Column5 = getElementFactory().getLabel(By.xpath(locatorCellRow7Column5), "CellInRow7Column5");
21-
private ILabel cellInRow5Column7 = getElementFactory().getLabel(By.xpath(locatorCellRow5Column7), "CellInRow5Column7");
22-
private ILabel cellInRow5Column3 = getElementFactory().getLabel(By.xpath(locatorCellRow5Column3), "CellInRow5Column3");
23-
private ILabel cellInRow4Column5 = getElementFactory().getLabel(By.xpath(locatorCellRow4Column5), "CellInRow4Column5");
24-
private ILabel cellInRow1Column1 = getElementFactory().getLabel(By.xpath(locatorCellRow1Column1), "CellInRow1Column1");
25-
private ILabel cellInRow2Column1 = getElementFactory().getLabel(By.xpath(locatorCellRow2Column1), "CellInRow2Column1");
18+
private ILabel headerName = getElementFactory().getLabel(By.xpath(LOCATOR_CHALLENGING_DOM_FORM), "Header of Challenging Dom Form page");
19+
private ILabel cellInRow3Column5 = getElementFactory().getLabel(By.xpath(locatorCellRow3Column5), "Cell in row 3 column 5");
20+
private ILabel cellInRow5Column5 = getElementFactory().getLabel(By.xpath(locatorCellRow5Column5), "Cell in row 5 column 5");
21+
private ILabel cellInRow7Column5 = getElementFactory().getLabel(By.xpath(locatorCellRow7Column5), "Cell in row 7 column 5");
22+
private ILabel cellInRow5Column7 = getElementFactory().getLabel(By.xpath(locatorCellRow5Column7), "Cell in row 5 column 7");
23+
private ILabel cellInRow5Column3 = getElementFactory().getLabel(By.xpath(locatorCellRow5Column3), "Cell in row 5 column 3");
24+
private ILabel cellInRow1Column1 = getElementFactory().getLabel(By.xpath(locatorCellRow1Column1), "Cell in row 1 column 1");
25+
private ILabel cellInRow2Column1 = getElementFactory().getLabel(By.xpath(locatorCellRow2Column1), "Cell in row 2 column 1");
2626

2727
public ChallengingDomForm() {
28-
super(By.xpath("//h3[contains(text(),'Challenging DOM')]"), "Challenging DOM");
28+
super(By.xpath(LOCATOR_CHALLENGING_DOM_FORM), "Challenging DOM");
2929
}
3030

3131
public ILabel getCellInRow3Column5() {
@@ -48,10 +48,6 @@ public ILabel getCellInRow5Column3() {
4848
return cellInRow5Column3;
4949
}
5050

51-
public ILabel getCellInRow4Column5() {
52-
return cellInRow4Column5;
53-
}
54-
5551
public ILabel getCellInRow1Column1() {
5652
return cellInRow1Column1;
5753
}
@@ -60,18 +56,14 @@ public ILabel getCellInRow2Column1() {
6056
return cellInRow2Column1;
6157
}
6258

63-
public String getLocatorCellRow4Column5() {
64-
return locatorCellRow4Column5;
59+
public ILabel getHeaderName() {
60+
return headerName;
6561
}
6662

6763
public String getLocatorCellRow1Column1() {
6864
return locatorCellRow1Column1;
6965
}
7066

71-
public String getLocatorCellRow2Column1() {
72-
return locatorCellRow2Column1;
73-
}
74-
7567
public String getLocatorCellRow5Column5() {
7668
return locatorCellRow5Column5;
7769
}

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

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.testng.asserts.SoftAssert;
1111
import tests.BaseTest;
1212
import theinternet.TheInternetPage;
13-
13+
import java.util.List;
1414
import static aquality.selenium.locators.RelativeBySupplier.with;
1515

1616
public class LocatorTests extends BaseTest {
@@ -22,7 +22,11 @@ public class LocatorTests extends BaseTest {
2222
private final String nameElementRow5Column7 = "expectedRow5Column7";
2323
private final String nameElementRow5Column5 = "expectedRow5Column5";
2424
private final String nameElementRow2Column1 = "expectedRow2Column1";
25-
private final String friendlyMessage = "Actual cell text is not equal expected";
25+
private final String nameElementHeaderOfPage = "Challenging Dom";
26+
private final String friendlyMessageEquallingText = "Actual cell text is not equal expected";
27+
private final String friendlyMessageElementFound = "Element with not reachable distance is exist";
28+
private final int positiveDistance = 300;
29+
private final int negativeDistance = 100;
2630

2731
@BeforeMethod
2832
public void beforeMethod() {
@@ -31,7 +35,6 @@ public void beforeMethod() {
3135

3236
@Test
3337
public void testAboveLocatorWithDifferentAboveParametersType() {
34-
ILabel cellInRow3Column5 = challengingDomForm.getCellInRow3Column5();
3538
ILabel cellInRow5Column5 = challengingDomForm.getCellInRow5Column5();
3639

3740
ILabel actualCellRaw3Column5GotWithByXpath =
@@ -56,13 +59,11 @@ public void testAboveLocatorWithDifferentAboveParametersType() {
5659
actualCellRaw3Column5GotWithWebElement.getText(),
5760
actualCellRaw3Column5GotWithByXpath.getText(),
5861
actualWebElementCellRaw3Column5GotBySeleniumRelative.getText(),
59-
cellInRow3Column5.getText());
62+
challengingDomForm.getCellInRow3Column5().getText());
6063
}
6164

6265
@Test
6366
public void testBelowLocatorWithDifferentBelowParametersType() {
64-
65-
ILabel cellInRow7Column5 = challengingDomForm.getCellInRow7Column5();
6667
ILabel cellInRow5Column5 = challengingDomForm.getCellInRow5Column5();
6768

6869
ILabel actualCellRaw7Column5GotWithByXpath =
@@ -87,14 +88,12 @@ public void testBelowLocatorWithDifferentBelowParametersType() {
8788
actualCellRaw7Column5GotWithWebElement.getText(),
8889
actualCellRaw7Column5GotWithByXpath.getText(),
8990
actualWebElementCellRaw7Column5GotBySeleniumRelative.getText(),
90-
cellInRow7Column5.getText());
91+
challengingDomForm.getCellInRow7Column5().getText());
9192
}
9293

9394

9495
@Test
9596
public void testToLeftOfLocatorWithDifferentToLeftOfParametersType() {
96-
97-
ILabel cellInRow5Column3 = challengingDomForm.getCellInRow5Column3();
9897
ILabel cellInRow5Column5 = challengingDomForm.getCellInRow5Column5();
9998

10099
ILabel actualCellRaw5Column3GotWithByXpath =
@@ -119,12 +118,11 @@ public void testToLeftOfLocatorWithDifferentToLeftOfParametersType() {
119118
actualCellRaw5Column3GotWithWebElement.getText(),
120119
actualCellRaw5Column3GotWithByXpath.getText(),
121120
actualWebElementCellRaw5Column3GotBySeleniumRelative.getText(),
122-
cellInRow5Column3.getText());
121+
challengingDomForm.getCellInRow5Column3().getText());
123122
}
124123

125124
@Test
126125
public void testToRightOfLocatorWithDifferentToRightOfParametersType() {
127-
ILabel cellInRow5Column7 = challengingDomForm.getCellInRow5Column7();
128126
ILabel cellInRow5Column5 = challengingDomForm.getCellInRow5Column5();
129127

130128
ILabel actualCellRaw5Column7GotWithByXpath =
@@ -149,7 +147,7 @@ public void testToRightOfLocatorWithDifferentToRightOfParametersType() {
149147
actualCellRaw5Column7GotWithWebElement.getText(),
150148
actualCellRaw5Column7GotWithByXpath.getText(),
151149
actualWebElementCellRaw5Column7GotBySeleniumRelative.getText(),
152-
cellInRow5Column7.getText());
150+
challengingDomForm.getCellInRow5Column7().getText());
153151
}
154152

155153
@Test
@@ -197,8 +195,8 @@ public void testAboveBelowLeftRight() {
197195

198196
checkDifferentTypesWithSoftAssert(
199197
actualCellRaw5Column5GotWithAqualityElement.getText(),
200-
actualCellRaw5Column5GotWithXpath.getText(),
201198
actualCellRaw5Column5GotWithWebElement.getText(),
199+
actualCellRaw5Column5GotWithXpath.getText(),
202200
actualWebElementCellRaw5Column5GotBySeleniumRelative.getText(),
203201
cellInRow5Column5.getText());
204202
}
@@ -230,12 +228,72 @@ public void testNear() {
230228
challengingDomForm.getCellInRow2Column1().getText());
231229
}
232230

231+
@Test
232+
public void testNearWithDistance() {
233+
ILabel actualHeaderNameGotWithAqualityElement =
234+
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
235+
.near(challengingDomForm.getCellInRow1Column1(), positiveDistance),
236+
nameElementHeaderOfPage);
237+
238+
ILabel actualHeaderNameGotWithWebElement =
239+
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
240+
.near(challengingDomForm.getCellInRow1Column1().getElement(), positiveDistance),
241+
nameElementHeaderOfPage);
242+
243+
ILabel actualHeaderNameGotWithXpath =
244+
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
245+
.near(By.xpath(challengingDomForm.getLocatorCellRow1Column1()), positiveDistance),
246+
nameElementHeaderOfPage);
247+
248+
249+
WebElement actualWebElementHeaderNameGotBySeleniumRelative =
250+
getBrowser().getDriver().findElement(RelativeLocator.with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
251+
.near(getBrowser().getDriver().findElement(By.xpath(challengingDomForm.getLocatorCellRow1Column1())),positiveDistance));
252+
253+
checkDifferentTypesWithSoftAssert(
254+
actualHeaderNameGotWithAqualityElement.getText(),
255+
actualHeaderNameGotWithWebElement.getText(),
256+
actualHeaderNameGotWithXpath.getText(),
257+
actualWebElementHeaderNameGotBySeleniumRelative.getText(),
258+
challengingDomForm.getHeaderName().getText());
259+
}
260+
261+
@Test
262+
public void testNearWithDistanceNegative() {
263+
ILabel actualHeaderNameGotWithAqualityElement =
264+
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
265+
.near(challengingDomForm.getCellInRow1Column1(), negativeDistance),
266+
nameElementHeaderOfPage);
267+
268+
ILabel actualHeaderNameGotWithWebElement =
269+
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
270+
.near(challengingDomForm.getCellInRow1Column1().getElement(), negativeDistance),
271+
nameElementHeaderOfPage);
272+
273+
ILabel actualHeaderNameGotWithXpath =
274+
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
275+
.near(By.xpath(challengingDomForm.getLocatorCellRow1Column1()), negativeDistance),
276+
nameElementHeaderOfPage);
277+
278+
279+
List<WebElement> actualsWebElementsHeaderNameGotBySeleniumRelative =
280+
getBrowser().getDriver().findElements(RelativeLocator.with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
281+
.near(getBrowser().getDriver().findElement(By.xpath(challengingDomForm.getLocatorCellRow1Column1())), negativeDistance));
282+
283+
SoftAssert softAssert = new SoftAssert();
284+
softAssert.assertFalse(actualHeaderNameGotWithAqualityElement.state().isExist(), friendlyMessageElementFound);
285+
softAssert.assertFalse(actualHeaderNameGotWithWebElement.state().isExist(), friendlyMessageElementFound);
286+
softAssert.assertFalse(actualHeaderNameGotWithXpath.state().isExist(), friendlyMessageElementFound);
287+
softAssert.assertEquals(actualsWebElementsHeaderNameGotBySeleniumRelative.size(),0, "Elements with not reachable distance was found");
288+
softAssert.assertAll();
289+
}
290+
233291
public void checkDifferentTypesWithSoftAssert(String textAquality, String textWebElement, String textByXpath, String textSelenium, String expectedText) {
234292
SoftAssert softAssert = new SoftAssert();
235-
softAssert.assertEquals(textAquality, expectedText, friendlyMessage);
236-
softAssert.assertEquals(textByXpath, expectedText, friendlyMessage);
237-
softAssert.assertEquals(textWebElement, expectedText, friendlyMessage);
238-
softAssert.assertEquals(textSelenium, expectedText, friendlyMessage);
293+
softAssert.assertEquals(textAquality, expectedText, friendlyMessageEquallingText);
294+
softAssert.assertEquals(textByXpath, expectedText, friendlyMessageEquallingText);
295+
softAssert.assertEquals(textWebElement, expectedText, friendlyMessageEquallingText);
296+
softAssert.assertEquals(textSelenium, expectedText, friendlyMessageEquallingText);
239297
softAssert.assertAll();
240298
}
241299
}

0 commit comments

Comments
 (0)