1010import org .testng .asserts .SoftAssert ;
1111import tests .BaseTest ;
1212import theinternet .TheInternetPage ;
13-
13+ import java . util . List ;
1414import static aquality .selenium .locators .RelativeBySupplier .with ;
1515
1616public 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