Skip to content

Commit 7dc41eb

Browse files
committed
Implementing utility method to catch expected exceptions in tests
1 parent cb71f15 commit 7dc41eb

28 files changed

+423
-717
lines changed

java/client/test/org/openqa/selenium/AlertsTest.java

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
package org.openqa.selenium;
1919

2020
import static org.hamcrest.Matchers.containsString;
21+
import static org.hamcrest.Matchers.instanceOf;
22+
import static org.hamcrest.Matchers.is;
2123
import static org.junit.Assert.assertEquals;
2224
import static org.junit.Assert.assertThat;
23-
import static org.junit.Assert.assertTrue;
24-
import static org.junit.Assert.fail;
2525
import static org.junit.Assume.assumeFalse;
2626
import static org.openqa.selenium.WaitingConditions.newWindowIsOpened;
2727
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
@@ -34,6 +34,7 @@
3434
import static org.openqa.selenium.testing.Driver.MARIONETTE;
3535
import static org.openqa.selenium.testing.Driver.PHANTOMJS;
3636
import static org.openqa.selenium.testing.Driver.SAFARI;
37+
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;
3738
import static org.openqa.selenium.testing.TestUtilities.getFirefoxVersion;
3839
import static org.openqa.selenium.testing.TestUtilities.isChrome;
3940
import static org.openqa.selenium.testing.TestUtilities.isFirefox;
@@ -175,9 +176,8 @@ public void testSettingTheValueOfAnAlertThrows() {
175176

176177
Alert alert = wait.until(alertIsPresent());
177178
try {
178-
alert.sendKeys("cheese");
179-
fail("Expected exception");
180-
} catch (ElementNotInteractableException expected) {
179+
Throwable t = catchThrowable(() -> alert.sendKeys("cheese"));
180+
assertThat(t, instanceOf(ElementNotInteractableException.class));
181181
} finally {
182182
alert.accept();
183183
}
@@ -216,12 +216,8 @@ public void testAlertShouldNotAllowAdditionalCommandsIfDismissed() {
216216
Alert alert = wait.until(alertIsPresent());
217217
alert.accept();
218218

219-
try {
220-
alert.getText();
221-
} catch (NoAlertPresentException expected) {
222-
return;
223-
}
224-
fail("Expected NoAlertPresentException");
219+
Throwable t = catchThrowable(alert::getText);
220+
assertThat(t, instanceOf(NoAlertPresentException.class));
225221
}
226222

227223
@JavascriptEnabled
@@ -257,12 +253,8 @@ public void testShouldAllowUsersToAcceptAnAlertInANestedFrame() {
257253
@JavascriptEnabled
258254
@Test
259255
public void testSwitchingToMissingAlertThrows() throws Exception {
260-
try {
261-
driver.switchTo().alert();
262-
fail("Expected exception");
263-
} catch (NoAlertPresentException expected) {
264-
// Expected
265-
}
256+
Throwable t = catchThrowable(() -> driver.switchTo().alert());
257+
assertThat(t, instanceOf(NoAlertPresentException.class));
266258
}
267259

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

278-
try {
279-
driver.switchTo().alert();
280-
fail("Expected exception");
281-
} catch (NoSuchWindowException expected) {
282-
// Expected
283-
}
270+
Throwable t = catchThrowable(() -> driver.switchTo().alert());
271+
assertThat(t, instanceOf(NoSuchWindowException.class));
284272

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

377-
boolean gotException = false;
378-
try {
379-
wait.until(alertIsPresent());
380-
} catch (AssertionError expected) {
381-
// Expected
382-
gotException = true;
383-
}
384-
assertTrue(gotException);
365+
Throwable t = catchThrowable(() -> wait.until(alertIsPresent()));
366+
assertThat(t, instanceOf(TimeoutException.class));
385367

386368
} finally {
387369
driver.switchTo().window(onloadWindow);
@@ -479,13 +461,11 @@ public void testShouldHandleAlertOnWindowClose() {
479461
public void testIncludesAlertTextInUnhandledAlertException() {
480462
driver.findElement(By.id("alert")).click();
481463
wait.until(alertIsPresent());
482-
try {
483-
driver.getTitle();
484-
fail("Expected UnhandledAlertException");
485-
} catch (UnhandledAlertException e) {
486-
assertEquals("cheese", e.getAlertText());
487-
assertThat(e.getMessage(), containsString("cheese"));
488-
}
464+
465+
Throwable t = catchThrowable(driver::getTitle);
466+
assertThat(t, instanceOf(UnhandledAlertException.class));
467+
assertThat(((UnhandledAlertException) t).getAlertText(), is("cheese"));
468+
assertThat(t.getMessage(), containsString("cheese"));
489469
}
490470

491471
@NoDriverAfterTest

java/client/test/org/openqa/selenium/ArchitectureTest.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919

2020
import static org.hamcrest.Matchers.containsString;
2121
import static org.hamcrest.Matchers.instanceOf;
22-
import static org.hamcrest.Matchers.is;
2322
import static org.junit.Assert.assertEquals;
2423
import static org.junit.Assert.assertFalse;
2524
import static org.junit.Assert.assertNotNull;
2625
import static org.junit.Assert.assertThat;
2726
import static org.junit.Assert.assertTrue;
28-
import static org.junit.Assert.fail;
27+
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;
2928

3029
import org.junit.Test;
3130
import org.junit.runner.RunWith;
@@ -123,24 +122,16 @@ public void determineArchARM() {
123122

124123
@Test
125124
public void determineArchEmpty() {
126-
try {
127-
Architecture.extractFromSysProperty("");
128-
fail("Expected UnsupportedOperationException");
129-
} catch (RuntimeException e) {
130-
assertThat(e, is(instanceOf(UnsupportedOperationException.class)));
131-
assertThat(e.getMessage(), containsString("Unknown architecture"));
132-
}
125+
Throwable t = catchThrowable(() -> Architecture.extractFromSysProperty(""));
126+
assertThat(t, instanceOf(UnsupportedOperationException.class));
127+
assertThat(t.getMessage(), containsString("Unknown architecture"));
133128
}
134129

135130
@Test
136131
public void determineArchBogus() {
137-
try {
138-
Architecture.extractFromSysProperty("hoobaflooba");
139-
fail("Expected UnsupportedOperationException");
140-
} catch (RuntimeException e) {
141-
assertThat(e, is(instanceOf(UnsupportedOperationException.class)));
142-
assertThat(e.getMessage(), containsString("Unknown architecture"));
143-
}
132+
Throwable t = catchThrowable(() -> Architecture.extractFromSysProperty("hoobaflooba"));
133+
assertThat(t, instanceOf(UnsupportedOperationException.class));
134+
assertThat(t.getMessage(), containsString("Unknown architecture"));
144135
}
145136

146137
@Test

java/client/test/org/openqa/selenium/ChildrenFindingTest.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818
package org.openqa.selenium;
1919

2020
import static org.hamcrest.Matchers.equalTo;
21+
import static org.hamcrest.Matchers.instanceOf;
2122
import static org.hamcrest.Matchers.is;
2223
import static org.junit.Assert.assertEquals;
2324
import static org.junit.Assert.assertNotNull;
2425
import static org.junit.Assert.assertThat;
25-
import static org.junit.Assert.fail;
2626
import static org.openqa.selenium.testing.Driver.CHROME;
2727
import static org.openqa.selenium.testing.Driver.IE;
2828
import static org.openqa.selenium.testing.Driver.REMOTE;
29+
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;
2930

3031
import org.junit.Test;
3132
import org.openqa.selenium.testing.Ignore;
3233
import org.openqa.selenium.testing.JUnit4TestBase;
3334

3435
import java.util.List;
3536

36-
3737
public class ChildrenFindingTest extends JUnit4TestBase {
3838
@Test
3939
public void testFindElementByXPath() {
@@ -64,14 +64,9 @@ public void testFindingDotSlashElementsOnElementByXPathShouldFindNotTopLevelElem
6464
@Test
6565
public void testFindElementByXPathWhenNoMatch() {
6666
driver.get(pages.nestedPage);
67-
6867
WebElement element = driver.findElement(By.name("form2"));
69-
try {
70-
element.findElement(By.xpath(".//select/x"));
71-
fail("Did not expect to find element");
72-
} catch (NoSuchElementException ignored) {
73-
// this is expected
74-
}
68+
Throwable t = catchThrowable(() -> element.findElement(By.xpath(".//select/x")));
69+
assertThat(t, instanceOf(NoSuchElementException.class));
7570
}
7671

7772
@Test
@@ -141,12 +136,8 @@ public void testFindElementByIdWhenIdContainsNonAlphanumericCharacters() {
141136
public void testFindElementByIdWhenNoMatchInContext() {
142137
driver.get(pages.nestedPage);
143138
WebElement element = driver.findElement(By.id("test_id_div"));
144-
try {
145-
element.findElement(By.id("test_id_out"));
146-
fail();
147-
} catch (NoSuchElementException e) {
148-
// This is expected
149-
}
139+
Throwable t = catchThrowable(() -> element.findElement(By.id("test_id_out")));
140+
assertThat(t, instanceOf(NoSuchElementException.class));
150141
}
151142

152143
@Test
@@ -290,11 +281,8 @@ public void testShouldFindGrandChildren() {
290281
public void testShouldNotFindElementOutSideTree() {
291282
driver.get(pages.formPage);
292283
WebElement element = driver.findElement(By.name("login"));
293-
try {
294-
element.findElement(By.name("x"));
295-
} catch (NoSuchElementException e) {
296-
// this is expected
297-
}
284+
Throwable t = catchThrowable(() -> element.findElement(By.name("x")));
285+
assertThat(t, instanceOf(NoSuchElementException.class));
298286
}
299287

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

324-
List<WebElement> elements =
325-
elem.findElements(By.partialLinkText("link"));
312+
List<WebElement> elements = elem.findElements(By.partialLinkText("link"));
326313
assertNotNull(elements);
327314
assertEquals(6, elements.size());
328315
}
@@ -352,12 +339,7 @@ public void testElementCanGetLinkByLinkTestIgnoringTrailingWhitespace() {
352339
driver.get(pages.simpleTestPage);
353340
WebElement elem = driver.findElement(By.id("links"));
354341

355-
WebElement link = null;
356-
try {
357-
link = elem.findElement(By.linkText("link with trailing space"));
358-
} catch (NoSuchElementException e) {
359-
fail("Should have found link");
360-
}
342+
WebElement link = elem.findElement(By.linkText("link with trailing space"));
361343
assertEquals("linkWithTrailingSpace", link.getAttribute("id"));
362344
}
363345

java/client/test/org/openqa/selenium/ClearTest.java

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
package org.openqa.selenium;
1919

20+
import static org.hamcrest.CoreMatchers.instanceOf;
2021
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.fail;
22+
import static org.junit.Assert.assertThat;
23+
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;
2224

2325
import org.junit.Test;
2426
import org.openqa.selenium.testing.JUnit4TestBase;
@@ -36,26 +38,18 @@ public void testWritableTextInputShouldClear() {
3638
@Test
3739
public void testTextInputShouldNotClearWhenDisabled() {
3840
driver.get(pages.readOnlyPage);
39-
try {
40-
WebElement element = driver.findElement(By.id("textInputnotenabled"));
41-
assertEquals(false, element.isEnabled());
42-
element.clear();
43-
fail("Should not have succeeded");
44-
} catch (InvalidElementStateException e) {
45-
// This is expected
46-
}
41+
WebElement element = driver.findElement(By.id("textInputnotenabled"));
42+
assertEquals(false, element.isEnabled());
43+
Throwable t = catchThrowable(element::clear);
44+
assertThat(t, instanceOf(InvalidElementStateException.class));
4745
}
4846

4947
@Test
5048
public void testTextInputShouldNotClearWhenReadOnly() {
51-
try {
52-
driver.get(pages.readOnlyPage);
53-
WebElement element = driver.findElement(By.id("readOnlyTextInput"));
54-
element.clear();
55-
fail("Should not have succeeded");
56-
} catch (InvalidElementStateException e) {
57-
// This is expected
58-
}
49+
driver.get(pages.readOnlyPage);
50+
WebElement element = driver.findElement(By.id("readOnlyTextInput"));
51+
Throwable t = catchThrowable(element::clear);
52+
assertThat(t, instanceOf(InvalidElementStateException.class));
5953
}
6054

6155
@Test
@@ -68,26 +62,18 @@ public void testWritableTextAreaShouldClear() {
6862

6963
@Test
7064
public void testTextAreaShouldNotClearWhenDisabled() {
71-
try {
72-
driver.get(pages.readOnlyPage);
73-
WebElement element = driver.findElement(By.id("textAreaNotenabled"));
74-
element.clear();
75-
fail("Should not have succeeded");
76-
} catch (InvalidElementStateException e) {
77-
// This is expected
78-
}
65+
driver.get(pages.readOnlyPage);
66+
WebElement element = driver.findElement(By.id("textAreaNotenabled"));
67+
Throwable t = catchThrowable(element::clear);
68+
assertThat(t, instanceOf(InvalidElementStateException.class));
7969
}
8070

8171
@Test
8272
public void testTextAreaShouldNotClearWhenReadOnly() {
83-
try {
84-
driver.get(pages.readOnlyPage);
85-
WebElement element = driver.findElement(By.id("textAreaReadOnly"));
86-
element.clear();
87-
fail("Should not have succeeded");
88-
} catch (InvalidElementStateException e) {
89-
// This is expected
90-
}
73+
driver.get(pages.readOnlyPage);
74+
WebElement element = driver.findElement(By.id("textAreaReadOnly"));
75+
Throwable t = catchThrowable(element::clear);
76+
assertThat(t, instanceOf(InvalidElementStateException.class));
9177
}
9278

9379
@Test

java/client/test/org/openqa/selenium/ClickScrollingTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
package org.openqa.selenium;
1919

2020
import static org.hamcrest.Matchers.greaterThan;
21+
import static org.hamcrest.Matchers.instanceOf;
2122
import static org.hamcrest.Matchers.is;
2223
import static org.junit.Assert.assertEquals;
2324
import static org.junit.Assert.assertThat;
2425
import static org.junit.Assert.assertTrue;
25-
import static org.junit.Assert.fail;
2626
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
2727
import static org.openqa.selenium.testing.Driver.CHROME;
2828
import static org.openqa.selenium.testing.Driver.HTMLUNIT;
2929
import static org.openqa.selenium.testing.Driver.IE;
3030
import static org.openqa.selenium.testing.Driver.MARIONETTE;
3131
import static org.openqa.selenium.testing.Driver.PHANTOMJS;
3232
import static org.openqa.selenium.testing.Driver.SAFARI;
33+
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;
3334

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

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

60-
long yOffset = (Long) ((JavascriptExecutor) driver)
61-
.executeScript(scrollScript);
61+
long yOffset = (Long) ((JavascriptExecutor) driver).executeScript(scrollScript);
6262

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

7373
WebElement link = driver.findElement(By.id("link"));
74-
try {
75-
link.click();
76-
} catch (MoveTargetOutOfBoundsException e) {
77-
fail("Should not be out of bounds: " + e.getMessage());
78-
}
74+
link.click();
7975
}
8076

8177
@Test
@@ -172,13 +168,14 @@ public void testShouldBeAbleToClickElementThatIsOutOfViewInAFrame() {
172168
}
173169

174170
@SwitchToTopAfterTest
175-
@Test(expected = MoveTargetOutOfBoundsException.class)
171+
@Test
176172
@Ignore(reason = "All tested browses scroll non-scrollable frames")
177173
public void testShouldNotBeAbleToClickElementThatIsOutOfViewInANonScrollableFrame() {
178174
driver.get(appServer.whereIs("scrolling_tests/page_with_non_scrolling_frame.html"));
179175
driver.switchTo().frame("scrolling_frame");
180176
WebElement element = driver.findElement(By.name("scroll_checkbox"));
181-
element.click();
177+
Throwable t = catchThrowable(element::click);
178+
assertThat(t, instanceOf(MoveTargetOutOfBoundsException.class));
182179
}
183180

184181
@SwitchToTopAfterTest

0 commit comments

Comments
 (0)