Skip to content

Commit 8107209

Browse files
committed
[Feature/Add_Relative_Locators] Refactor code
1 parent 02c4f38 commit 8107209

File tree

2 files changed

+57
-56
lines changed

2 files changed

+57
-56
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
public class ChallengingDomForm extends Form {
88

99
public static final String LOCATOR_CHALLENGING_DOM_FORM = "//h3[contains(text(),'Challenging DOM')]";
10+
public static final String ELEMENT_NAME_ROW3_COLUMN5 = "Cell in row 3 column 5";
11+
public static final String ELEMENT_NAME_ROW5_COLUMN5 = "Cell in row 5 column 5";
12+
public static final String ELEMENT_NAME_ROW7_COLUMN5 = "Cell in row 7 column 5";
13+
public static final String ELEMENT_NAME_ROW5_COLUMN7 = "Cell in row 5 column 7";
14+
public static final String ELEMENT_NAME_ROW5_COLUMN3 = "Cell in row 5 column 3";
15+
public static final String ELEMENT_NAME_ROW1_COLUMN1 = "Cell in row 1 column 1";
16+
public static final String ELEMENT_NAME_ROW2_COLUMN1 = "Cell in row 2 column 1";
17+
public static final String ELEMENT_NAME_HEADER_CHALLENGING_DOM = "Header of Challenging Dom";
18+
1019
private final String locatorCellRow5Column5 = "//tr[5]/td[5]";
1120
private final String locatorCellRow3Column5 = "//tr[3]/td[5]";
1221
private final String locatorCellRow7Column5 = "//tr[7]/td[5]";
@@ -15,14 +24,14 @@ public class ChallengingDomForm extends Form {
1524
private final String locatorCellRow1Column1 = "//tr[1]/td[1]";
1625
private final String locatorCellRow2Column1 = "//tr[2]/td[1]";
1726

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");
27+
private ILabel headerName = getElementFactory().getLabel(By.xpath(LOCATOR_CHALLENGING_DOM_FORM), ELEMENT_NAME_HEADER_CHALLENGING_DOM);
28+
private ILabel cellInRow3Column5 = getElementFactory().getLabel(By.xpath(locatorCellRow3Column5), ELEMENT_NAME_ROW3_COLUMN5);
29+
private ILabel cellInRow5Column5 = getElementFactory().getLabel(By.xpath(locatorCellRow5Column5), ELEMENT_NAME_ROW5_COLUMN5);
30+
private ILabel cellInRow7Column5 = getElementFactory().getLabel(By.xpath(locatorCellRow7Column5), ELEMENT_NAME_ROW7_COLUMN5);
31+
private ILabel cellInRow5Column7 = getElementFactory().getLabel(By.xpath(locatorCellRow5Column7), ELEMENT_NAME_ROW5_COLUMN7);
32+
private ILabel cellInRow5Column3 = getElementFactory().getLabel(By.xpath(locatorCellRow5Column3), ELEMENT_NAME_ROW5_COLUMN3);
33+
private ILabel cellInRow1Column1 = getElementFactory().getLabel(By.xpath(locatorCellRow1Column1), ELEMENT_NAME_ROW1_COLUMN1);
34+
private ILabel cellInRow2Column1 = getElementFactory().getLabel(By.xpath(locatorCellRow2Column1), ELEMENT_NAME_ROW2_COLUMN1);
2635

2736
public ChallengingDomForm() {
2837
super(By.xpath(LOCATOR_CHALLENGING_DOM_FORM), "Challenging DOM");

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

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,10 @@
1616
public class LocatorTests extends BaseTest {
1717
private final ChallengingDomForm challengingDomForm = new ChallengingDomForm();
1818
private final String labelLocatorCell = "//td";
19-
private final String nameElementRow3Column5 = "expectedRow3Column5";
20-
private final String nameElementRow7Column5 = "expectedRow7Column5";
21-
private final String nameElementRow5Column3 = "expectedRow5Column3";
22-
private final String nameElementRow5Column7 = "expectedRow5Column7";
23-
private final String nameElementRow5Column5 = "expectedRow5Column5";
24-
private final String nameElementRow2Column1 = "expectedRow2Column1";
25-
private final String nameElementHeaderOfPage = "Challenging Dom";
2619
private final String friendlyMessageEquallingText = "Actual cell text is not equal expected";
2720
private final String friendlyMessageElementFound = "Element with not reachable distance is exist";
28-
private final int positiveDistance = 300;
29-
private final int negativeDistance = 100;
21+
private final int distanceToFindElementWithPositiveResult = 300;
22+
private final int distanceToFindElementWithNegativeResult = 100;
3023

3124
@BeforeMethod
3225
public void beforeMethod() {
@@ -39,15 +32,15 @@ public void testAboveLocatorWithDifferentAboveParametersType() {
3932

4033
ILabel actualCellRaw3Column5GotWithByXpath =
4134
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).above(By.xpath(challengingDomForm.getLocatorCellRow5Column5())),
42-
nameElementRow3Column5);
35+
ChallengingDomForm.ELEMENT_NAME_ROW3_COLUMN5);
4336

4437
ILabel actualCellRaw3Column5GotWithWebElement =
4538
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).above(cellInRow5Column5.getElement()),
46-
nameElementRow3Column5);
39+
ChallengingDomForm.ELEMENT_NAME_ROW3_COLUMN5);
4740

4841
ILabel actualCellRaw3Column5GotWithAqualityElement =
4942
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).above(cellInRow5Column5),
50-
nameElementRow3Column5);
43+
ChallengingDomForm.ELEMENT_NAME_ROW3_COLUMN5);
5144

5245
WebElement actualWebElementCellRaw3Column5GotBySeleniumRelative =
5346
getBrowser().getDriver().findElement(RelativeLocator
@@ -68,15 +61,15 @@ public void testBelowLocatorWithDifferentBelowParametersType() {
6861

6962
ILabel actualCellRaw7Column5GotWithByXpath =
7063
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).below(By.xpath(challengingDomForm.getLocatorCellRow5Column5())),
71-
nameElementRow7Column5);
64+
ChallengingDomForm.ELEMENT_NAME_ROW7_COLUMN5);
7265

7366
ILabel actualCellRaw7Column5GotWithWebElement =
7467
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).below(cellInRow5Column5.getElement()),
75-
nameElementRow7Column5);
68+
ChallengingDomForm.ELEMENT_NAME_ROW7_COLUMN5);
7669

7770
ILabel actualCellRaw7Column5GotWithAqualityElement =
7871
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).below(cellInRow5Column5),
79-
nameElementRow7Column5);
72+
ChallengingDomForm.ELEMENT_NAME_ROW7_COLUMN5);
8073

8174
WebElement actualWebElementCellRaw7Column5GotBySeleniumRelative =
8275
getBrowser().getDriver().findElement(RelativeLocator
@@ -91,22 +84,21 @@ public void testBelowLocatorWithDifferentBelowParametersType() {
9184
challengingDomForm.getCellInRow7Column5().getText());
9285
}
9386

94-
9587
@Test
9688
public void testToLeftOfLocatorWithDifferentToLeftOfParametersType() {
9789
ILabel cellInRow5Column5 = challengingDomForm.getCellInRow5Column5();
9890

9991
ILabel actualCellRaw5Column3GotWithByXpath =
10092
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toLeftOf(By.xpath(challengingDomForm.getLocatorCellRow5Column5())),
101-
nameElementRow5Column3);
93+
ChallengingDomForm.ELEMENT_NAME_ROW5_COLUMN3);
10294

10395
ILabel actualCellRaw5Column3GotWithWebElement =
10496
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toLeftOf(cellInRow5Column5.getElement()),
105-
nameElementRow5Column3);
97+
ChallengingDomForm.ELEMENT_NAME_ROW5_COLUMN3);
10698

10799
ILabel actualCellRaw5Column3GotWithAqualityElement =
108100
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toLeftOf(cellInRow5Column5),
109-
nameElementRow5Column3);
101+
ChallengingDomForm.ELEMENT_NAME_ROW5_COLUMN3);
110102

111103
WebElement actualWebElementCellRaw5Column3GotBySeleniumRelative =
112104
getBrowser().getDriver().findElement(RelativeLocator
@@ -127,15 +119,15 @@ public void testToRightOfLocatorWithDifferentToRightOfParametersType() {
127119

128120
ILabel actualCellRaw5Column7GotWithByXpath =
129121
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toRightOf(By.xpath(challengingDomForm.getLocatorCellRow5Column5())),
130-
nameElementRow5Column7);
122+
ChallengingDomForm.ELEMENT_NAME_ROW5_COLUMN7);
131123

132124
ILabel actualCellRaw5Column7GotWithWebElement =
133125
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toRightOf(cellInRow5Column5.getElement()),
134-
nameElementRow5Column3);
126+
ChallengingDomForm.ELEMENT_NAME_ROW5_COLUMN7);
135127

136128
ILabel actualCellRaw5Column7GotWithAqualityElement =
137129
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).toRightOf(cellInRow5Column5),
138-
nameElementRow5Column3);
130+
ChallengingDomForm.ELEMENT_NAME_ROW5_COLUMN7);
139131

140132
WebElement actualWebElementCellRaw5Column7GotBySeleniumRelative =
141133
getBrowser().getDriver().findElement(RelativeLocator
@@ -160,12 +152,12 @@ public void testAboveBelowLeftRight() {
160152

161153
ILabel actualCellRaw5Column5GotWithAqualityElement =
162154
elementFactory.getLabel(with(By.xpath(labelLocatorCell))
163-
.above(cellInRow7Column5)
164-
.below(cellInRow3Column5)
165-
.toRightOf(cellInRow5Column3)
166-
.toLeftOf(cellInRow5Column7)
167-
.above(cellInRow7Column5)
168-
, nameElementRow5Column5);
155+
.above(cellInRow7Column5)
156+
.below(cellInRow3Column5)
157+
.toRightOf(cellInRow5Column3)
158+
.toLeftOf(cellInRow5Column7)
159+
.above(cellInRow7Column5)
160+
, ChallengingDomForm.ELEMENT_NAME_ROW5_COLUMN5);
169161

170162
ILabel actualCellRaw5Column5GotWithWebElement =
171163
elementFactory.getLabel(with(By.xpath(labelLocatorCell))
@@ -174,7 +166,7 @@ public void testAboveBelowLeftRight() {
174166
.toRightOf(cellInRow5Column3.getElement())
175167
.toLeftOf(cellInRow5Column7.getElement())
176168
.above(cellInRow7Column5.getElement())
177-
, nameElementRow5Column5);
169+
, ChallengingDomForm.ELEMENT_NAME_ROW5_COLUMN5);
178170

179171
ILabel actualCellRaw5Column5GotWithXpath =
180172
elementFactory.getLabel(with(By.xpath(labelLocatorCell))
@@ -183,7 +175,7 @@ public void testAboveBelowLeftRight() {
183175
.toRightOf(By.xpath(challengingDomForm.getLocatorCellRow5Column3()))
184176
.toLeftOf(By.xpath(challengingDomForm.getLocatorCellRow5Column7()))
185177
.above(By.xpath(challengingDomForm.getLocatorCellRow7Column5()))
186-
, nameElementRow5Column5);
178+
, ChallengingDomForm.ELEMENT_NAME_ROW5_COLUMN5);
187179

188180
WebElement actualWebElementCellRaw5Column5GotBySeleniumRelative =
189181
getBrowser().getDriver().findElement(RelativeLocator.with(By.xpath(labelLocatorCell))
@@ -205,15 +197,15 @@ public void testAboveBelowLeftRight() {
205197
public void testNear() {
206198
ILabel actualCellRaw2Column1GotWithAqualityElement =
207199
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).near(challengingDomForm.getCellInRow1Column1()),
208-
nameElementRow2Column1);
200+
ChallengingDomForm.ELEMENT_NAME_ROW2_COLUMN1);
209201

210202
ILabel actualCellRaw2Column1GotWithWebElement =
211203
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).near(challengingDomForm.getCellInRow1Column1().getElement()),
212-
nameElementRow2Column1);
204+
ChallengingDomForm.ELEMENT_NAME_ROW2_COLUMN1);
213205

214206
ILabel actualCellRaw2Column1GotWithXpath =
215207
elementFactory.getLabel(with(By.xpath(labelLocatorCell)).near(By.xpath(challengingDomForm.getLocatorCellRow1Column1())),
216-
nameElementRow2Column1);
208+
ChallengingDomForm.ELEMENT_NAME_ROW2_COLUMN1);
217209

218210

219211
WebElement actualWebElementCellRaw2Column1GotBySeleniumRelative =
@@ -232,23 +224,23 @@ public void testNear() {
232224
public void testNearWithDistance() {
233225
ILabel actualHeaderNameGotWithAqualityElement =
234226
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
235-
.near(challengingDomForm.getCellInRow1Column1(), positiveDistance),
236-
nameElementHeaderOfPage);
227+
.near(challengingDomForm.getCellInRow1Column1(), distanceToFindElementWithPositiveResult),
228+
ChallengingDomForm.ELEMENT_NAME_HEADER_CHALLENGING_DOM);
237229

238230
ILabel actualHeaderNameGotWithWebElement =
239231
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
240-
.near(challengingDomForm.getCellInRow1Column1().getElement(), positiveDistance),
241-
nameElementHeaderOfPage);
232+
.near(challengingDomForm.getCellInRow1Column1().getElement(), distanceToFindElementWithPositiveResult),
233+
ChallengingDomForm.ELEMENT_NAME_HEADER_CHALLENGING_DOM);
242234

243235
ILabel actualHeaderNameGotWithXpath =
244236
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
245-
.near(By.xpath(challengingDomForm.getLocatorCellRow1Column1()), positiveDistance),
246-
nameElementHeaderOfPage);
237+
.near(By.xpath(challengingDomForm.getLocatorCellRow1Column1()), distanceToFindElementWithPositiveResult),
238+
ChallengingDomForm.ELEMENT_NAME_HEADER_CHALLENGING_DOM);
247239

248240

249241
WebElement actualWebElementHeaderNameGotBySeleniumRelative =
250242
getBrowser().getDriver().findElement(RelativeLocator.with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
251-
.near(getBrowser().getDriver().findElement(By.xpath(challengingDomForm.getLocatorCellRow1Column1())),positiveDistance));
243+
.near(getBrowser().getDriver().findElement(By.xpath(challengingDomForm.getLocatorCellRow1Column1())), distanceToFindElementWithPositiveResult));
252244

253245
checkDifferentTypesWithSoftAssert(
254246
actualHeaderNameGotWithAqualityElement.getText(),
@@ -262,29 +254,29 @@ public void testNearWithDistance() {
262254
public void testNearWithDistanceNegative() {
263255
ILabel actualHeaderNameGotWithAqualityElement =
264256
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
265-
.near(challengingDomForm.getCellInRow1Column1(), negativeDistance),
266-
nameElementHeaderOfPage);
257+
.near(challengingDomForm.getCellInRow1Column1(), distanceToFindElementWithNegativeResult),
258+
ChallengingDomForm.ELEMENT_NAME_HEADER_CHALLENGING_DOM);
267259

268260
ILabel actualHeaderNameGotWithWebElement =
269261
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
270-
.near(challengingDomForm.getCellInRow1Column1().getElement(), negativeDistance),
271-
nameElementHeaderOfPage);
262+
.near(challengingDomForm.getCellInRow1Column1().getElement(), distanceToFindElementWithNegativeResult),
263+
ChallengingDomForm.ELEMENT_NAME_HEADER_CHALLENGING_DOM);
272264

273265
ILabel actualHeaderNameGotWithXpath =
274266
elementFactory.getLabel(with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
275-
.near(By.xpath(challengingDomForm.getLocatorCellRow1Column1()), negativeDistance),
276-
nameElementHeaderOfPage);
267+
.near(By.xpath(challengingDomForm.getLocatorCellRow1Column1()), distanceToFindElementWithNegativeResult),
268+
ChallengingDomForm.ELEMENT_NAME_HEADER_CHALLENGING_DOM);
277269

278270

279271
List<WebElement> actualsWebElementsHeaderNameGotBySeleniumRelative =
280272
getBrowser().getDriver().findElements(RelativeLocator.with(By.xpath(ChallengingDomForm.LOCATOR_CHALLENGING_DOM_FORM))
281-
.near(getBrowser().getDriver().findElement(By.xpath(challengingDomForm.getLocatorCellRow1Column1())), negativeDistance));
273+
.near(getBrowser().getDriver().findElement(By.xpath(challengingDomForm.getLocatorCellRow1Column1())), distanceToFindElementWithNegativeResult));
282274

283275
SoftAssert softAssert = new SoftAssert();
284276
softAssert.assertFalse(actualHeaderNameGotWithAqualityElement.state().isExist(), friendlyMessageElementFound);
285277
softAssert.assertFalse(actualHeaderNameGotWithWebElement.state().isExist(), friendlyMessageElementFound);
286278
softAssert.assertFalse(actualHeaderNameGotWithXpath.state().isExist(), friendlyMessageElementFound);
287-
softAssert.assertEquals(actualsWebElementsHeaderNameGotBySeleniumRelative.size(),0, "Elements with not reachable distance was found");
279+
softAssert.assertEquals(actualsWebElementsHeaderNameGotBySeleniumRelative.size(), 0, friendlyMessageElementFound);
288280
softAssert.assertAll();
289281
}
290282

0 commit comments

Comments
 (0)