Skip to content

Commit

Permalink
Refactoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Apr 4, 2017
1 parent 7dc41eb commit 5f96eb0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 99 deletions.
9 changes: 4 additions & 5 deletions java/client/test/org/openqa/selenium/VisibilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
152 changes: 65 additions & 87 deletions java/client/test/org/openqa/selenium/WindowSwitchingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -58,7 +83,6 @@ public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOpera
TestUtilities.getEffectivePlatform().is(Platform.WINDOWS));

driver.get(pages.xhtmlTestPage);
String current = driver.getWindowHandle();
Set<String> currentWindowHandles = driver.getWindowHandles();

driver.findElement(By.linkText("Open new window")).click();
Expand All @@ -75,30 +99,20 @@ 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)
@Ignore({MARIONETTE})
@Test
public void testShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle() {
driver.get(pages.xhtmlTestPage);
String current = driver.getWindowHandle();
Set<String> currentWindowHandles = driver.getWindowHandles();

driver.findElement(By.linkText("Open new window")).click();
Expand All @@ -108,20 +122,15 @@ 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)
@Ignore({MARIONETTE})
@Test
public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed() {
driver.get(pages.xhtmlTestPage);
String current = driver.getWindowHandle();
Set<String> currentWindowHandles = driver.getWindowHandles();

driver.findElement(By.linkText("Open new window")).click();
Expand All @@ -131,23 +140,18 @@ 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)
@Ignore({MARIONETTE})
@Test
public void testShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIsClosed() {
driver.get(pages.xhtmlTestPage);
String current = driver.getWindowHandle();
Set<String> currentWindowHandles = driver.getWindowHandles();

driver.findElement(By.linkText("Open new window")).click();
Expand All @@ -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
Expand All @@ -181,14 +181,13 @@ public void testShouldBeAbleToIterateOverAllOpenWindows() {
Set<String> allWindowHandles = driver.getWindowHandles();

// There should be three windows. We should also see each of the window titles at least once.
Set<String> seenHandles = Sets.newHashSet();
for (String handle : allWindowHandles) {
assertFalse(seenHandles.contains(handle));
Set<String> 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
Expand All @@ -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<String> currentWindowHandles = driver.getWindowHandles();

driver.findElement(By.name("windowThree")).click();
Expand All @@ -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
Expand All @@ -242,7 +235,6 @@ public void testCanCallGetWindowHandlesAfterClosingAWindow() throws Exception {

Boolean isIEDriver = TestUtilities.isInternetExplorer(driver);
Boolean isIE6 = TestUtilities.isIe6(driver);
String currentHandle = driver.getWindowHandle();
Set<String> currentWindowHandles = driver.getWindowHandles();

driver.findElement(By.name("windowThree")).click();
Expand All @@ -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<String> 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<String> 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
Expand Down Expand Up @@ -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)
Expand All @@ -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);

Expand Down
2 changes: 0 additions & 2 deletions java/client/test/org/openqa/selenium/WindowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5f96eb0

Please sign in to comment.