diff --git a/java/client/test/org/openqa/selenium/VisibilityTest.java b/java/client/test/org/openqa/selenium/VisibilityTest.java index 9ca8acd865ee5..9fb43ca0b6437 100644 --- a/java/client/test/org/openqa/selenium/VisibilityTest.java +++ b/java/client/test/org/openqa/selenium/VisibilityTest.java @@ -50,11 +50,10 @@ public class VisibilityTest extends JUnit4TestBase { public void testShouldAllowTheUserToTellIfAnElementIsDisplayedOrNot() { driver.get(pages.javascriptPage); - assertThat(driver.findElement(By.id("displayed")).isDisplayed(), - is(true)); - assertThat(driver.findElement(By.id("none")).isDisplayed(), is(false)); - assertThat(driver.findElement(By.id("suppressedParagraph")).isDisplayed(), is(false)); - assertThat(driver.findElement(By.id("hidden")).isDisplayed(), is(false)); + assertTrue(driver.findElement(By.id("displayed")).isDisplayed()); + assertFalse(driver.findElement(By.id("none")).isDisplayed()); + assertFalse(driver.findElement(By.id("suppressedParagraph")).isDisplayed()); + assertFalse(driver.findElement(By.id("hidden")).isDisplayed()); } @Test diff --git a/java/client/test/org/openqa/selenium/WindowSwitchingTest.java b/java/client/test/org/openqa/selenium/WindowSwitchingTest.java index b09066b0ae5c7..ae8f0ced70d6d 100644 --- a/java/client/test/org/openqa/selenium/WindowSwitchingTest.java +++ b/java/client/test/org/openqa/selenium/WindowSwitchingTest.java @@ -20,7 +20,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeFalse; @@ -34,9 +33,11 @@ import static org.openqa.selenium.testing.Driver.REMOTE; import static org.openqa.selenium.testing.TestUtilities.catchThrowable; -import com.google.common.collect.Sets; - +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TestRule; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.testing.Ignore; import org.openqa.selenium.testing.JUnit4TestBase; @@ -47,9 +48,33 @@ import org.openqa.selenium.testing.drivers.Browser; import java.util.Set; +import java.util.stream.Collectors; public class WindowSwitchingTest extends JUnit4TestBase { + @Rule + public final TestRule switchToMainWindow = new TestWatcher() { + private String mainWindow; + + @Override + protected void starting(Description description) { + super.starting(description); + mainWindow = driver.getWindowHandle(); + } + + @Override + protected void finished(Description description) { + try { + if (! mainWindow.equals(driver.getWindowHandle())) { + driver.close(); + } + } catch (Exception ignore) { + } + driver.switchTo().window(mainWindow); + super.finished(description); + } + }; + @SwitchToTopAfterTest @NoDriverAfterTest(failedOnly = true) @Test @@ -58,7 +83,6 @@ public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOpera TestUtilities.getEffectivePlatform().is(Platform.WINDOWS)); driver.get(pages.xhtmlTestPage); - String current = driver.getWindowHandle(); Set currentWindowHandles = driver.getWindowHandles(); driver.findElement(By.linkText("Open new window")).click(); @@ -75,22 +99,13 @@ public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOpera driver.findElement(By.id("iframe_page_heading")); driver.switchTo().frame("iframe1"); assertThat(driver.getWindowHandle(), equalTo(handle)); - - driver.close(); - driver.switchTo().window(current); } @Test public void testShouldThrowNoSuchWindowException() { driver.get(pages.xhtmlTestPage); - String current = driver.getWindowHandle(); - - try { - Throwable t = catchThrowable(() -> driver.switchTo().window("invalid name")); - assertThat(t, instanceOf(NoSuchWindowException.class)); - } finally { - driver.switchTo().window(current); - } + Throwable t = catchThrowable(() -> driver.switchTo().window("invalid name")); + assertThat(t, instanceOf(NoSuchWindowException.class)); } @NoDriverAfterTest(failedOnly = true) @@ -98,7 +113,6 @@ public void testShouldThrowNoSuchWindowException() { @Test public void testShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle() { driver.get(pages.xhtmlTestPage); - String current = driver.getWindowHandle(); Set currentWindowHandles = driver.getWindowHandles(); driver.findElement(By.linkText("Open new window")).click(); @@ -108,12 +122,8 @@ public void testShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle() { driver.switchTo().window("result"); driver.close(); - try { - Throwable t = catchThrowable(driver::getWindowHandle); - assertThat(t, instanceOf(NoSuchWindowException.class)); - } finally { - driver.switchTo().window(current); - } + Throwable t = catchThrowable(driver::getWindowHandle); + assertThat(t, instanceOf(NoSuchWindowException.class)); } @NoDriverAfterTest(failedOnly = true) @@ -121,7 +131,6 @@ public void testShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle() { @Test public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed() { driver.get(pages.xhtmlTestPage); - String current = driver.getWindowHandle(); Set currentWindowHandles = driver.getWindowHandles(); driver.findElement(By.linkText("Open new window")).click(); @@ -131,15 +140,11 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed( driver.switchTo().window("result"); driver.close(); - try { - Throwable t = catchThrowable(driver::getTitle); - assertThat(t, instanceOf(NoSuchWindowException.class)); + Throwable t = catchThrowable(driver::getTitle); + assertThat(t, instanceOf(NoSuchWindowException.class)); - Throwable t2 = catchThrowable(() -> driver.findElement(By.tagName("body"))); - assertThat(t2, instanceOf(NoSuchWindowException.class)); - } finally { - driver.switchTo().window(current); - } + Throwable t2 = catchThrowable(() -> driver.findElement(By.tagName("body"))); + assertThat(t2, instanceOf(NoSuchWindowException.class)); } @NoDriverAfterTest(failedOnly = true) @@ -147,7 +152,6 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed( @Test public void testShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIsClosed() { driver.get(pages.xhtmlTestPage); - String current = driver.getWindowHandle(); Set currentWindowHandles = driver.getWindowHandles(); driver.findElement(By.linkText("Open new window")).click(); @@ -158,12 +162,8 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIs WebElement body = driver.findElement(By.tagName("body")); driver.close(); - try { - Throwable t = catchThrowable(body::getText); - assertThat(t, instanceOf(NoSuchWindowException.class)); - } finally { - driver.switchTo().window(current); - } + Throwable t = catchThrowable(body::getText); + assertThat(t, instanceOf(NoSuchWindowException.class)); } @NoDriverAfterTest @@ -181,14 +181,13 @@ public void testShouldBeAbleToIterateOverAllOpenWindows() { Set allWindowHandles = driver.getWindowHandles(); // There should be three windows. We should also see each of the window titles at least once. - Set seenHandles = Sets.newHashSet(); - for (String handle : allWindowHandles) { - assertFalse(seenHandles.contains(handle)); + Set allWindowTitles = allWindowHandles.stream().map(handle -> { driver.switchTo().window(handle); - seenHandles.add(handle); - } + return driver.getTitle(); + }).collect(Collectors.toSet()); assertEquals(3, allWindowHandles.size()); + assertEquals(3, allWindowTitles.size()); } @JavascriptEnabled @@ -202,7 +201,6 @@ public void testClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToH driver.get(pages.xhtmlTestPage); Boolean isIEDriver = TestUtilities.isInternetExplorer(driver); Boolean isIE6 = TestUtilities.isIe6(driver); - String currentHandle = driver.getWindowHandle(); Set currentWindowHandles = driver.getWindowHandles(); driver.findElement(By.name("windowThree")).click(); @@ -215,20 +213,15 @@ public void testClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToH if (TestUtilities.isChrome(driver) && TestUtilities.getEffectivePlatform(driver).is(ANDROID)) { Thread.sleep(1000); } - try { - wait.until(ExpectedConditions.presenceOfElementLocated(By.id("close"))); - driver.findElement(By.id("close")).click(); - if (isIEDriver && !isIE6) { - Alert alert = wait.until(alertIsPresent()); - alert.accept(); - } + wait.until(ExpectedConditions.presenceOfElementLocated(By.id("close"))).click(); - // If we make it this far, we're all good. - } finally { - driver.switchTo().window(currentHandle); - driver.findElement(By.id("linkId")); + if (isIEDriver && !isIE6) { + Alert alert = wait.until(alertIsPresent()); + alert.accept(); } + + // If we make it this far, we're all good. } @JavascriptEnabled @@ -242,7 +235,6 @@ public void testCanCallGetWindowHandlesAfterClosingAWindow() throws Exception { Boolean isIEDriver = TestUtilities.isInternetExplorer(driver); Boolean isIE6 = TestUtilities.isIe6(driver); - String currentHandle = driver.getWindowHandle(); Set currentWindowHandles = driver.getWindowHandles(); driver.findElement(By.name("windowThree")).click(); @@ -256,29 +248,23 @@ public void testCanCallGetWindowHandlesAfterClosingAWindow() throws Exception { if (TestUtilities.isChrome(driver) && TestUtilities.getEffectivePlatform(driver).is(ANDROID)) { Thread.sleep(1000); } - try { - wait.until(ExpectedConditions.presenceOfElementLocated(By.id("close"))).click(); - - if (isIEDriver && !isIE6) { - Alert alert = wait.until(alertIsPresent()); - alert.accept(); - } - Set allHandles = wait.until(windowHandleCountToBe(allWindowHandles - 1)); + wait.until(ExpectedConditions.presenceOfElementLocated(By.id("close"))).click(); - assertEquals(currentWindowHandles.size(), allHandles.size()); - } finally { - driver.switchTo().window(currentHandle); + if (isIEDriver && !isIE6) { + Alert alert = wait.until(alertIsPresent()); + alert.accept(); } + + Set allHandles = wait.until(windowHandleCountToBe(allWindowHandles - 1)); + + assertEquals(currentWindowHandles.size(), allHandles.size()); } @Test public void testCanObtainAWindowHandle() { driver.get(pages.xhtmlTestPage); - - String currentHandle = driver.getWindowHandle(); - - assertNotNull(currentHandle); + assertNotNull(driver.getWindowHandle()); } @Test @@ -310,17 +296,12 @@ public void testCanCloseWindowWhenMultipleWindowsAreOpen() { // There should be two windows. We should also see each of the window titles at least once. assertEquals(2, allWindowHandles.size()); - for (String handle : allWindowHandles) { - if (! handle.equals(mainHandle)) { - driver.switchTo().window(handle); - driver.close(); - break; - } - } + allWindowHandles.stream().filter(anObject -> ! mainHandle.equals(anObject)).forEach(handle -> { + driver.switchTo().window(handle); + driver.close(); + }); assertEquals(1, driver.getWindowHandles().size()); - - driver.switchTo().window(mainHandle); } @NoDriverAfterTest(failedOnly = true) @@ -340,13 +321,10 @@ public void testCanCloseWindowAndSwitchBackToMainWindow() { // There should be two windows. We should also see each of the window titles at least once. assertEquals(2, allWindowHandles.size()); - for (String handle : allWindowHandles) { - if (! handle.equals(mainHandle)) { - driver.switchTo().window(handle); - driver.close(); - break; - } - } + allWindowHandles.stream().filter(anObject -> ! mainHandle.equals(anObject)).forEach(handle -> { + driver.switchTo().window(handle); + driver.close(); + }); driver.switchTo().window(mainHandle); diff --git a/java/client/test/org/openqa/selenium/WindowTest.java b/java/client/test/org/openqa/selenium/WindowTest.java index 0531f58414608..4853d20abad15 100644 --- a/java/client/test/org/openqa/selenium/WindowTest.java +++ b/java/client/test/org/openqa/selenium/WindowTest.java @@ -36,8 +36,6 @@ import org.openqa.selenium.testing.TestUtilities; import org.openqa.selenium.testing.drivers.SauceDriver; -import java.util.logging.Logger; - public class WindowTest extends JUnit4TestBase { @Test diff --git a/java/client/test/org/openqa/selenium/testing/TestUtilities.java b/java/client/test/org/openqa/selenium/testing/TestUtilities.java index 42adf492f3797..b4bb07be2bc8a 100644 --- a/java/client/test/org/openqa/selenium/testing/TestUtilities.java +++ b/java/client/test/org/openqa/selenium/testing/TestUtilities.java @@ -26,7 +26,6 @@ import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.testing.drivers.SauceDriver; -import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -96,13 +95,11 @@ public static boolean isChrome(WebDriver driver) { } public static boolean isOldChromedriver(WebDriver driver) { - Capabilities caps; - try { - caps = ((HasCapabilities) driver).getCapabilities(); - } catch (ClassCastException e) { + if (!(driver instanceof HasCapabilities)) { // Driver does not support capabilities -- not a chromedriver at all. return false; } + Capabilities caps = ((HasCapabilities) driver).getCapabilities(); String chromedriverVersion = (String) caps.getCapability("chrome.chromedriverVersion"); if (chromedriverVersion != null) { String[] versionMajorMinor = chromedriverVersion.split("\\.", 2);