Skip to content

Commit

Permalink
Implementing utility method to catch expected exceptions in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Apr 4, 2017
1 parent cb71f15 commit 7dc41eb
Show file tree
Hide file tree
Showing 28 changed files with 423 additions and 717 deletions.
56 changes: 18 additions & 38 deletions java/client/test/org/openqa/selenium/AlertsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
package org.openqa.selenium;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.openqa.selenium.WaitingConditions.newWindowIsOpened;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
Expand All @@ -34,6 +34,7 @@
import static org.openqa.selenium.testing.Driver.MARIONETTE;
import static org.openqa.selenium.testing.Driver.PHANTOMJS;
import static org.openqa.selenium.testing.Driver.SAFARI;
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;
import static org.openqa.selenium.testing.TestUtilities.getFirefoxVersion;
import static org.openqa.selenium.testing.TestUtilities.isChrome;
import static org.openqa.selenium.testing.TestUtilities.isFirefox;
Expand Down Expand Up @@ -175,9 +176,8 @@ public void testSettingTheValueOfAnAlertThrows() {

Alert alert = wait.until(alertIsPresent());
try {
alert.sendKeys("cheese");
fail("Expected exception");
} catch (ElementNotInteractableException expected) {
Throwable t = catchThrowable(() -> alert.sendKeys("cheese"));
assertThat(t, instanceOf(ElementNotInteractableException.class));
} finally {
alert.accept();
}
Expand Down Expand Up @@ -216,12 +216,8 @@ public void testAlertShouldNotAllowAdditionalCommandsIfDismissed() {
Alert alert = wait.until(alertIsPresent());
alert.accept();

try {
alert.getText();
} catch (NoAlertPresentException expected) {
return;
}
fail("Expected NoAlertPresentException");
Throwable t = catchThrowable(alert::getText);
assertThat(t, instanceOf(NoAlertPresentException.class));
}

@JavascriptEnabled
Expand Down Expand Up @@ -257,12 +253,8 @@ public void testShouldAllowUsersToAcceptAnAlertInANestedFrame() {
@JavascriptEnabled
@Test
public void testSwitchingToMissingAlertThrows() throws Exception {
try {
driver.switchTo().alert();
fail("Expected exception");
} catch (NoAlertPresentException expected) {
// Expected
}
Throwable t = catchThrowable(() -> driver.switchTo().alert());
assertThat(t, instanceOf(NoAlertPresentException.class));
}

@JavascriptEnabled
Expand All @@ -275,12 +267,8 @@ public void testSwitchingToMissingAlertInAClosedWindowThrows() throws Exception
wait.until(ableToSwitchToWindow("newwindow"));
driver.close();

try {
driver.switchTo().alert();
fail("Expected exception");
} catch (NoSuchWindowException expected) {
// Expected
}
Throwable t = catchThrowable(() -> driver.switchTo().alert());
assertThat(t, instanceOf(NoSuchWindowException.class));

} finally {
driver.switchTo().window(mainWindow);
Expand Down Expand Up @@ -374,14 +362,8 @@ public void testShouldNotHandleAlertInAnotherWindow() {
driver.findElement(By.id("open-window-with-onload-alert")).click();
onloadWindow = wait.until(newWindowIsOpened(currentWindowHandles));

boolean gotException = false;
try {
wait.until(alertIsPresent());
} catch (AssertionError expected) {
// Expected
gotException = true;
}
assertTrue(gotException);
Throwable t = catchThrowable(() -> wait.until(alertIsPresent()));
assertThat(t, instanceOf(TimeoutException.class));

} finally {
driver.switchTo().window(onloadWindow);
Expand Down Expand Up @@ -479,13 +461,11 @@ public void testShouldHandleAlertOnWindowClose() {
public void testIncludesAlertTextInUnhandledAlertException() {
driver.findElement(By.id("alert")).click();
wait.until(alertIsPresent());
try {
driver.getTitle();
fail("Expected UnhandledAlertException");
} catch (UnhandledAlertException e) {
assertEquals("cheese", e.getAlertText());
assertThat(e.getMessage(), containsString("cheese"));
}

Throwable t = catchThrowable(driver::getTitle);
assertThat(t, instanceOf(UnhandledAlertException.class));
assertThat(((UnhandledAlertException) t).getAlertText(), is("cheese"));
assertThat(t.getMessage(), containsString("cheese"));
}

@NoDriverAfterTest
Expand Down
23 changes: 7 additions & 16 deletions java/client/test/org/openqa/selenium/ArchitectureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
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.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -123,24 +122,16 @@ public void determineArchARM() {

@Test
public void determineArchEmpty() {
try {
Architecture.extractFromSysProperty("");
fail("Expected UnsupportedOperationException");
} catch (RuntimeException e) {
assertThat(e, is(instanceOf(UnsupportedOperationException.class)));
assertThat(e.getMessage(), containsString("Unknown architecture"));
}
Throwable t = catchThrowable(() -> Architecture.extractFromSysProperty(""));
assertThat(t, instanceOf(UnsupportedOperationException.class));
assertThat(t.getMessage(), containsString("Unknown architecture"));
}

@Test
public void determineArchBogus() {
try {
Architecture.extractFromSysProperty("hoobaflooba");
fail("Expected UnsupportedOperationException");
} catch (RuntimeException e) {
assertThat(e, is(instanceOf(UnsupportedOperationException.class)));
assertThat(e.getMessage(), containsString("Unknown architecture"));
}
Throwable t = catchThrowable(() -> Architecture.extractFromSysProperty("hoobaflooba"));
assertThat(t, instanceOf(UnsupportedOperationException.class));
assertThat(t.getMessage(), containsString("Unknown architecture"));
}

@Test
Expand Down
38 changes: 10 additions & 28 deletions java/client/test/org/openqa/selenium/ChildrenFindingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
package org.openqa.selenium;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.openqa.selenium.testing.Driver.CHROME;
import static org.openqa.selenium.testing.Driver.IE;
import static org.openqa.selenium.testing.Driver.REMOTE;
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;

import org.junit.Test;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;

import java.util.List;


public class ChildrenFindingTest extends JUnit4TestBase {
@Test
public void testFindElementByXPath() {
Expand Down Expand Up @@ -64,14 +64,9 @@ public void testFindingDotSlashElementsOnElementByXPathShouldFindNotTopLevelElem
@Test
public void testFindElementByXPathWhenNoMatch() {
driver.get(pages.nestedPage);

WebElement element = driver.findElement(By.name("form2"));
try {
element.findElement(By.xpath(".//select/x"));
fail("Did not expect to find element");
} catch (NoSuchElementException ignored) {
// this is expected
}
Throwable t = catchThrowable(() -> element.findElement(By.xpath(".//select/x")));
assertThat(t, instanceOf(NoSuchElementException.class));
}

@Test
Expand Down Expand Up @@ -141,12 +136,8 @@ public void testFindElementByIdWhenIdContainsNonAlphanumericCharacters() {
public void testFindElementByIdWhenNoMatchInContext() {
driver.get(pages.nestedPage);
WebElement element = driver.findElement(By.id("test_id_div"));
try {
element.findElement(By.id("test_id_out"));
fail();
} catch (NoSuchElementException e) {
// This is expected
}
Throwable t = catchThrowable(() -> element.findElement(By.id("test_id_out")));
assertThat(t, instanceOf(NoSuchElementException.class));
}

@Test
Expand Down Expand Up @@ -290,11 +281,8 @@ public void testShouldFindGrandChildren() {
public void testShouldNotFindElementOutSideTree() {
driver.get(pages.formPage);
WebElement element = driver.findElement(By.name("login"));
try {
element.findElement(By.name("x"));
} catch (NoSuchElementException e) {
// this is expected
}
Throwable t = catchThrowable(() -> element.findElement(By.name("x")));
assertThat(t, instanceOf(NoSuchElementException.class));
}

@Test
Expand All @@ -321,8 +309,7 @@ public void testFindMultipleElements() {
driver.get(pages.simpleTestPage);
WebElement elem = driver.findElement(By.id("links"));

List<WebElement> elements =
elem.findElements(By.partialLinkText("link"));
List<WebElement> elements = elem.findElements(By.partialLinkText("link"));
assertNotNull(elements);
assertEquals(6, elements.size());
}
Expand Down Expand Up @@ -352,12 +339,7 @@ public void testElementCanGetLinkByLinkTestIgnoringTrailingWhitespace() {
driver.get(pages.simpleTestPage);
WebElement elem = driver.findElement(By.id("links"));

WebElement link = null;
try {
link = elem.findElement(By.linkText("link with trailing space"));
} catch (NoSuchElementException e) {
fail("Should have found link");
}
WebElement link = elem.findElement(By.linkText("link with trailing space"));
assertEquals("linkWithTrailingSpace", link.getAttribute("id"));
}

Expand Down
52 changes: 19 additions & 33 deletions java/client/test/org/openqa/selenium/ClearTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

package org.openqa.selenium;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThat;
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;

import org.junit.Test;
import org.openqa.selenium.testing.JUnit4TestBase;
Expand All @@ -36,26 +38,18 @@ public void testWritableTextInputShouldClear() {
@Test
public void testTextInputShouldNotClearWhenDisabled() {
driver.get(pages.readOnlyPage);
try {
WebElement element = driver.findElement(By.id("textInputnotenabled"));
assertEquals(false, element.isEnabled());
element.clear();
fail("Should not have succeeded");
} catch (InvalidElementStateException e) {
// This is expected
}
WebElement element = driver.findElement(By.id("textInputnotenabled"));
assertEquals(false, element.isEnabled());
Throwable t = catchThrowable(element::clear);
assertThat(t, instanceOf(InvalidElementStateException.class));
}

@Test
public void testTextInputShouldNotClearWhenReadOnly() {
try {
driver.get(pages.readOnlyPage);
WebElement element = driver.findElement(By.id("readOnlyTextInput"));
element.clear();
fail("Should not have succeeded");
} catch (InvalidElementStateException e) {
// This is expected
}
driver.get(pages.readOnlyPage);
WebElement element = driver.findElement(By.id("readOnlyTextInput"));
Throwable t = catchThrowable(element::clear);
assertThat(t, instanceOf(InvalidElementStateException.class));
}

@Test
Expand All @@ -68,26 +62,18 @@ public void testWritableTextAreaShouldClear() {

@Test
public void testTextAreaShouldNotClearWhenDisabled() {
try {
driver.get(pages.readOnlyPage);
WebElement element = driver.findElement(By.id("textAreaNotenabled"));
element.clear();
fail("Should not have succeeded");
} catch (InvalidElementStateException e) {
// This is expected
}
driver.get(pages.readOnlyPage);
WebElement element = driver.findElement(By.id("textAreaNotenabled"));
Throwable t = catchThrowable(element::clear);
assertThat(t, instanceOf(InvalidElementStateException.class));
}

@Test
public void testTextAreaShouldNotClearWhenReadOnly() {
try {
driver.get(pages.readOnlyPage);
WebElement element = driver.findElement(By.id("textAreaReadOnly"));
element.clear();
fail("Should not have succeeded");
} catch (InvalidElementStateException e) {
// This is expected
}
driver.get(pages.readOnlyPage);
WebElement element = driver.findElement(By.id("textAreaReadOnly"));
Throwable t = catchThrowable(element::clear);
assertThat(t, instanceOf(InvalidElementStateException.class));
}

@Test
Expand Down
17 changes: 7 additions & 10 deletions java/client/test/org/openqa/selenium/ClickScrollingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
package org.openqa.selenium;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
import static org.openqa.selenium.testing.Driver.CHROME;
import static org.openqa.selenium.testing.Driver.HTMLUNIT;
import static org.openqa.selenium.testing.Driver.IE;
import static org.openqa.selenium.testing.Driver.MARIONETTE;
import static org.openqa.selenium.testing.Driver.PHANTOMJS;
import static org.openqa.selenium.testing.Driver.SAFARI;
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;

import org.junit.Test;
import org.openqa.selenium.interactions.MoveTargetOutOfBoundsException;
Expand Down Expand Up @@ -57,8 +58,7 @@ public void testClickingOnAnchorScrollsPage() {

driver.findElement(By.partialLinkText("last speech")).click();

long yOffset = (Long) ((JavascriptExecutor) driver)
.executeScript(scrollScript);
long yOffset = (Long) ((JavascriptExecutor) driver).executeScript(scrollScript);

// Focusing on to click, but not actually following,
// the link will scroll it in to view, which is a few pixels further than 0
Expand All @@ -71,11 +71,7 @@ public void testShouldScrollToClickOnAnElementHiddenByOverflow() {
driver.get(url);

WebElement link = driver.findElement(By.id("link"));
try {
link.click();
} catch (MoveTargetOutOfBoundsException e) {
fail("Should not be out of bounds: " + e.getMessage());
}
link.click();
}

@Test
Expand Down Expand Up @@ -172,13 +168,14 @@ public void testShouldBeAbleToClickElementThatIsOutOfViewInAFrame() {
}

@SwitchToTopAfterTest
@Test(expected = MoveTargetOutOfBoundsException.class)
@Test
@Ignore(reason = "All tested browses scroll non-scrollable frames")
public void testShouldNotBeAbleToClickElementThatIsOutOfViewInANonScrollableFrame() {
driver.get(appServer.whereIs("scrolling_tests/page_with_non_scrolling_frame.html"));
driver.switchTo().frame("scrolling_frame");
WebElement element = driver.findElement(By.name("scroll_checkbox"));
element.click();
Throwable t = catchThrowable(element::click);
assertThat(t, instanceOf(MoveTargetOutOfBoundsException.class));
}

@SwitchToTopAfterTest
Expand Down
Loading

0 comments on commit 7dc41eb

Please sign in to comment.