Skip to content

Commit 0d65c3d

Browse files
valfirstshs96c
authored andcommitted
Add ExpectedCondition checking invisibility of single WebElement
Signed-off-by: Simon Stewart <simon.m.stewart@gmail.com>
1 parent 7f7dc14 commit 0d65c3d

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,16 +1369,7 @@ public static ExpectedCondition<Boolean> invisibilityOfAllElements(
13691369

13701370
@Override
13711371
public Boolean apply(WebDriver webDriver) {
1372-
for (WebElement element : elements) {
1373-
try {
1374-
if (element.isDisplayed()) {
1375-
return false;
1376-
}
1377-
} catch (StaleElementReferenceException ignored) {
1378-
// We can assume a stale element isn't displayed.
1379-
}
1380-
}
1381-
return true;
1372+
return elements.stream().allMatch(ExpectedConditions::isInvisible);
13821373
}
13831374

13841375
@Override
@@ -1388,6 +1379,36 @@ public String toString() {
13881379
};
13891380
}
13901381

1382+
/**
1383+
* An expectation for checking the element to be invisible
1384+
*
1385+
* @param element used to check its invisibility
1386+
* @return Boolean true when elements is not visible anymore
1387+
*/
1388+
public static ExpectedCondition<Boolean> invisibilityOf(final WebElement element) {
1389+
return new ExpectedCondition<Boolean>() {
1390+
1391+
@Override
1392+
public Boolean apply(WebDriver webDriver) {
1393+
return isInvisible(element);
1394+
}
1395+
1396+
@Override
1397+
public String toString() {
1398+
return "invisibility of " + element;
1399+
}
1400+
};
1401+
}
1402+
1403+
private static boolean isInvisible(final WebElement element) {
1404+
try {
1405+
return !element.isDisplayed();
1406+
} catch (StaleElementReferenceException ignored) {
1407+
// We can assume a stale element isn't displayed.
1408+
return true;
1409+
}
1410+
}
1411+
13911412
/**
13921413
* An expectation with the logical or condition of the given list of conditions.
13931414
*

java/client/test/org/openqa/selenium/support/ui/ExpectedConditionsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static org.openqa.selenium.support.ui.ExpectedConditions.attributeToBe;
3232
import static org.openqa.selenium.support.ui.ExpectedConditions.attributeToBeNotEmpty;
3333
import static org.openqa.selenium.support.ui.ExpectedConditions.elementSelectionStateToBe;
34+
import static org.openqa.selenium.support.ui.ExpectedConditions.invisibilityOf;
3435
import static org.openqa.selenium.support.ui.ExpectedConditions.invisibilityOfAllElements;
3536
import static org.openqa.selenium.support.ui.ExpectedConditions.not;
3637
import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfElementsToBe;
@@ -811,6 +812,12 @@ public void waitingForAllElementsInvisibilityWhenElementsAreVisible() {
811812
wait.until(invisibilityOfAllElements(Arrays.asList(mockElement)));
812813
}
813814

815+
@Test(expected = TimeoutException.class)
816+
public void waitingForElementInvisibilityWhenElementIsVisible() {
817+
when(mockElement.isDisplayed()).thenReturn(true);
818+
wait.until(invisibilityOf(mockElement));
819+
}
820+
814821
@Test(expected = TimeoutException.class)
815822
public void waitingForTextToBePresentInElementLocatedThrowsTimeoutExceptionWhenTextNotPresent() {
816823
String testSelector = "testSelector";

0 commit comments

Comments
 (0)