Skip to content

Commit 270fcf6

Browse files
alb-i986ddavison
authored andcommitted
java: add ExpectedConditions#numberOfWindowsToBe
Also, clean up unit tests in ExpectedConditionsTest: when an exception is expected to be thrown, there's no need to make an assertion on the return value. Signed-off-by: Daniel Davison <[email protected]>
1 parent 68dbd3e commit 270fcf6

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,24 @@ public String toString() {
847847
};
848848
}
849849

850+
public static ExpectedCondition<Boolean> numberOfwindowsToBe(final int expectedNumberOfWindows) {
851+
return new ExpectedCondition<Boolean>() {
852+
@Override
853+
public Boolean apply(WebDriver driver) {
854+
try {
855+
return driver.getWindowHandles().size() == expectedNumberOfWindows;
856+
} catch (WebDriverException e) {
857+
return null;
858+
}
859+
}
860+
861+
@Override
862+
public String toString() {
863+
return "number of open windows to be " + expectedNumberOfWindows;
864+
}
865+
};
866+
}
867+
850868
/**
851869
* An expectation with the logical opposite condition of the given condition.
852870
*

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;
3535
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfAllElements;
3636
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfAllElementsLocatedBy;
37+
import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfwindowsToBe;
3738

3839
import com.google.common.collect.Lists;
40+
import com.google.common.collect.Sets;
3941

4042
import org.junit.Before;
4143
import org.junit.Test;
@@ -48,9 +50,11 @@
4850
import org.openqa.selenium.StaleElementReferenceException;
4951
import org.openqa.selenium.TimeoutException;
5052
import org.openqa.selenium.WebDriver;
53+
import org.openqa.selenium.WebDriverException;
5154
import org.openqa.selenium.WebElement;
5255

5356
import java.util.List;
57+
import java.util.Set;
5458
import java.util.concurrent.TimeUnit;
5559

5660
/**
@@ -432,14 +436,41 @@ public void waitingElementSelectionStateToBeFalseReturnsTrue() {
432436
public void waitingElementSelectionStateToBeThrowsTimeoutExceptionWhenStateDontMatch() {
433437
when(mockElement.isSelected()).thenReturn(true);
434438

435-
assertTrue(wait.until(elementSelectionStateToBe(mockElement, false)));
439+
wait.until(elementSelectionStateToBe(mockElement, false));
436440
}
437441

438442
@Test(expected = StaleElementReferenceException.class)
439443
public void waitingElementSelectionStateToBeThrowsStaleExceptionWhenElementIsStale() {
440444
when(mockElement.isSelected()).thenThrow(new StaleElementReferenceException("Stale element"));
441445

442-
assertTrue(wait.until(elementSelectionStateToBe(mockElement, false)));
446+
wait.until(elementSelectionStateToBe(mockElement, false));
447+
}
448+
449+
@Test
450+
public void waitingNumberOfWindowsToBeTwoWhenThereAreTwoWindowsOpen() {
451+
Set<String> twoWindowHandles = Sets.newHashSet("w1", "w2");
452+
when(mockDriver.getWindowHandles()).thenReturn(twoWindowHandles);
453+
454+
assertTrue(wait.until(numberOfwindowsToBe(2)));
455+
}
456+
457+
@Test(expected = TimeoutException.class)
458+
public void waitingNumberOfWindowsToBeTwoThrowsTimeoutExceptionWhenThereAreThreeWindowsOpen() {
459+
Set<String> threeWindowHandles = Sets.newHashSet("w1", "w2", "w3");
460+
when(mockDriver.getWindowHandles()).thenReturn(threeWindowHandles);
461+
462+
wait.until(numberOfwindowsToBe(2));
463+
464+
// then TimeoutException is thrown
465+
}
466+
467+
@Test(expected = TimeoutException.class)
468+
public void waitingNumberOfWindowsToBeThrowsTimeoutExceptionWhenGetWindowHandlesThrowsWebDriverException() {
469+
when(mockDriver.getWindowHandles()).thenThrow(WebDriverException.class);
470+
471+
wait.until(numberOfwindowsToBe(2));
472+
473+
// then TimeoutException is thrown
443474
}
444475

445476
interface GenericCondition extends ExpectedCondition<Object> {}

0 commit comments

Comments
 (0)