Skip to content

Commit 9a2412b

Browse files
committed
Refactoring expected conditions to use findElements instead of findElement to check presence of an element
1 parent 48c746d commit 9a2412b

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ public static ExpectedCondition<Boolean> elementSelectionStateToBe(final By loca
799799
@Override
800800
public Boolean apply(WebDriver driver) {
801801
try {
802-
WebElement element = driver.findElement(locator);
802+
WebElement element = findElement(locator, driver);
803803
return element.isSelected() == selected;
804804
} catch (StaleElementReferenceException e) {
805805
return null;
@@ -892,9 +892,8 @@ public String toString() {
892892
*/
893893
private static WebElement findElement(By by, WebDriver driver) {
894894
try {
895-
return driver.findElement(by);
896-
} catch (NoSuchElementException e) {
897-
throw e;
895+
return driver.findElements(by).stream().findFirst().orElseThrow(
896+
() -> new NoSuchElementException("Cannot locate an element using " + by));
898897
} catch (WebDriverException e) {
899898
log.log(Level.WARNING,
900899
String.format("WebDriverException thrown by findElement(%s)", by), e);
@@ -913,7 +912,7 @@ private static List<WebElement> findElements(By by, WebDriver driver) {
913912
return driver.findElements(by);
914913
} catch (WebDriverException e) {
915914
log.log(Level.WARNING,
916-
String.format("WebDriverException thrown by findElement(%s)", by), e);
915+
String.format("WebDriverException thrown by findElements(%s)", by), e);
917916
throw e;
918917
}
919918
}
@@ -934,7 +933,7 @@ public static ExpectedCondition<Boolean> attributeToBe(final By locator, final S
934933

935934
@Override
936935
public Boolean apply(WebDriver driver) {
937-
WebElement element = driver.findElement(locator);
936+
WebElement element = findElement(locator, driver);
938937
currentValue = element.getAttribute(attribute);
939938
if (currentValue == null||currentValue.isEmpty()) {
940939
currentValue = element.getCssValue(attribute);
@@ -963,7 +962,7 @@ public static ExpectedCondition<Boolean> textToBe(final By locator, final String
963962
@Override
964963
public Boolean apply(WebDriver driver) {
965964
try {
966-
currentValue = driver.findElement(locator).getText();
965+
currentValue = findElement(locator, driver).getText();
967966
return currentValue.equals(value);
968967
} catch (Exception e) {
969968
return false;
@@ -992,7 +991,7 @@ public static ExpectedCondition<Boolean> textMatches(final By locator, final Pat
992991
@Override
993992
public Boolean apply(WebDriver driver) {
994993
try {
995-
currentValue = driver.findElement(locator).getText();
994+
currentValue = findElement(locator, driver).getText();
996995
return pattern.matcher(currentValue).find();
997996
} catch (Exception e) {
998997
return false;
@@ -1022,7 +1021,7 @@ public static ExpectedCondition<List<WebElement>> numberOfElementsToBeMoreThan(f
10221021

10231022
@Override
10241023
public List<WebElement> apply(WebDriver webDriver) {
1025-
List<WebElement> elements = webDriver.findElements(locator);
1024+
List<WebElement> elements = findElements(locator, webDriver);
10261025
currentNumber = elements.size();
10271026
return currentNumber > number ? elements : null;
10281027
}
@@ -1050,7 +1049,7 @@ public static ExpectedCondition<List<WebElement>> numberOfElementsToBeLessThan(f
10501049

10511050
@Override
10521051
public List<WebElement> apply(WebDriver webDriver) {
1053-
List<WebElement> elements = webDriver.findElements(locator);
1052+
List<WebElement> elements = findElements(locator, webDriver);
10541053
currentNumber = elements.size();
10551054
return currentNumber < number ? elements : null;
10561055
}
@@ -1077,7 +1076,7 @@ public static ExpectedCondition<List<WebElement>> numberOfElementsToBe(final By
10771076

10781077
@Override
10791078
public List<WebElement> apply(WebDriver webDriver) {
1080-
List<WebElement> elements = webDriver.findElements(locator);
1079+
List<WebElement> elements = findElements(locator, webDriver);
10811080
currentNumber = elements.size();
10821081
return currentNumber.equals(number) ? elements : null;
10831082
}
@@ -1175,9 +1174,9 @@ public static ExpectedCondition<Boolean> attributeContains(final By locator,
11751174
public Boolean apply(WebDriver driver) {
11761175
Boolean contains = false;
11771176
try {
1178-
currentValue = driver.findElement(locator).getAttribute(attribute);
1177+
currentValue = findElement(locator, driver).getAttribute(attribute);
11791178
if (currentValue == null || currentValue.isEmpty()) {
1180-
currentValue = driver.findElement(locator).getCssValue(attribute);
1179+
currentValue = findElement(locator, driver).getCssValue(attribute);
11811180
}
11821181
contains = currentValue.contains(value);
11831182
} catch (Exception e) {/**/}
@@ -1237,14 +1236,14 @@ public List<WebElement> apply(WebDriver webDriver) {
12371236
Boolean displayed = false;
12381237
Boolean exists = false;
12391238
try {
1240-
exists = webDriver.findElement(locator).findElements(sub_locator).size() > 0;
1239+
exists = findElement(locator, webDriver).findElements(sub_locator).size() > 0;
12411240
displayed =
1242-
webDriver.findElement(locator).findElement(sub_locator).isDisplayed();
1241+
findElement(locator, webDriver).findElement(sub_locator).isDisplayed();
12431242
} catch (Exception e) {
12441243
int i = 0;
12451244
}
12461245
return (exists && displayed) ?
1247-
webDriver.findElement(locator).findElements(sub_locator) :
1246+
findElement(locator, webDriver).findElements(sub_locator) :
12481247
null;
12491248
}
12501249

@@ -1273,11 +1272,11 @@ public List<WebElement> apply(WebDriver webDriver) {
12731272
Boolean exists = false;
12741273
try {
12751274
exists =
1276-
element.findElements(sub_locator).size()
1277-
> 0; //duplicating search is to avoid dom rebuilding problems
1278-
displayed = element.findElement(sub_locator).isDisplayed();
1275+
findElements(sub_locator, webDriver).size() > 0;
1276+
//duplicating search is to avoid dom rebuilding problems
1277+
displayed = findElement(sub_locator, webDriver).isDisplayed();
12791278
} catch (Exception e) {/**/}
1280-
return (exists && displayed) ? element.findElements(sub_locator) : null;
1279+
return (exists && displayed) ? findElements(sub_locator, webDriver) : null;
12811280
}
12821281

12831282
@Override

0 commit comments

Comments
 (0)