Skip to content

Commit

Permalink
[java] Migrating from Hamcrest to AssertJ in tests and unifying style…
Browse files Browse the repository at this point in the history
… of assertions in client module
  • Loading branch information
barancev committed Sep 3, 2018
1 parent 94e1721 commit db1c2ad
Show file tree
Hide file tree
Showing 177 changed files with 3,563 additions and 5,053 deletions.
11 changes: 11 additions & 0 deletions .idea/libraries/assertj.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions java/client/client.iml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<orderEntry type="library" name="javaparser" level="project" />
<orderEntry type="library" name="okhttp" level="project" />
<orderEntry type="library" scope="TEST" name="jetty-orig" level="project" />
<orderEntry type="library" scope="TEST" name="assertj" level="project" />
<orderEntry type="library" name="auto-service" level="project" />
</component>
<component name="sonarModuleSettings">
Expand Down
75 changes: 36 additions & 39 deletions java/client/test/org/openqa/selenium/AlertsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@

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.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assume.assumeFalse;
import static org.openqa.selenium.WaitingConditions.newWindowIsOpened;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
Expand All @@ -33,10 +30,10 @@
import static org.openqa.selenium.testing.Driver.IE;
import static org.openqa.selenium.testing.Driver.MARIONETTE;
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.isFirefox;

import org.assertj.core.api.Assumptions;
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.environment.webserver.Page;
Expand Down Expand Up @@ -93,7 +90,7 @@ public void testShouldBeAbleToOverrideTheWindowAlertMethod() {
driver.findElement(By.id("alert")).click();

// If we can perform any action, we're good to go
assertEquals("Testing Alerts", driver.getTitle());
assertThat(driver.getTitle()).isEqualTo("Testing Alerts");
}

@Test
Expand All @@ -105,7 +102,7 @@ public void testShouldAllowUsersToAcceptAnAlertManually() {
alert.accept();

// If we can perform any action, we're good to go
assertEquals("Testing Alerts", driver.getTitle());
assertThat(driver.getTitle()).isEqualTo("Testing Alerts");
}

@Test
Expand All @@ -115,8 +112,8 @@ public void testShouldThrowIllegalArgumentExceptionWhenKeysNull() {
driver.findElement(By.id("alert")).click();
Alert alert = wait.until(alertIsPresent());
try {
Throwable t = catchThrowable(() -> alert.sendKeys(null));
assertThat(t, instanceOf(IllegalArgumentException.class));
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> alert.sendKeys(null));
} finally {
alert.accept();
}
Expand All @@ -131,7 +128,7 @@ public void testShouldAllowUsersToAcceptAnAlertWithNoTextManually() {
alert.accept();

// If we can perform any action, we're good to go
assertEquals("Testing Alerts", driver.getTitle());
assertThat(driver.getTitle()).isEqualTo("Testing Alerts");
}

@NeedsLocalEnvironment(reason = "Carefully timing based")
Expand All @@ -154,7 +151,7 @@ public void testShouldGetTextOfAlertOpenedInSetTimeout() {
// and only if it happens before the alert actually loads.
Alert alert = driver.switchTo().alert();
try {
assertEquals("Slow", alert.getText());
assertThat(alert.getText()).isEqualTo("Slow");
} finally {
alert.accept();
}
Expand All @@ -169,7 +166,7 @@ public void testShouldAllowUsersToDismissAnAlertManually() {
alert.dismiss();

// If we can perform any action, we're good to go
assertEquals("Testing Alerts", driver.getTitle());
assertThat(driver.getTitle()).isEqualTo("Testing Alerts");
}

@Test
Expand All @@ -181,7 +178,7 @@ public void testShouldAllowAUserToAcceptAPrompt() {
alert.accept();

// If we can perform any action, we're good to go
assertEquals("Testing Prompt", driver.getTitle());
assertThat(driver.getTitle()).isEqualTo("Testing Prompt");
}

@Test
Expand All @@ -193,7 +190,7 @@ public void testShouldAllowAUserToDismissAPrompt() {
alert.dismiss();

// If we can perform any action, we're good to go
assertEquals("Testing Prompt", driver.getTitle());
assertThat(driver.getTitle()).isEqualTo("Testing Prompt");
}

@Test
Expand All @@ -218,8 +215,8 @@ public void testSettingTheValueOfAnAlertThrows() {

Alert alert = wait.until(alertIsPresent());
try {
Throwable t = catchThrowable(() -> alert.sendKeys("cheese"));
assertThat(t, instanceOf(ElementNotInteractableException.class));
assertThatExceptionOfType(ElementNotInteractableException.class)
.isThrownBy(() -> alert.sendKeys("cheese"));
} finally {
alert.accept();
}
Expand All @@ -234,7 +231,7 @@ public void testShouldAllowTheUserToGetTheTextOfAnAlert() {
String value = alert.getText();
alert.accept();

assertEquals("cheese", value);
assertThat(value).isEqualTo("cheese");
}

@Test
Expand All @@ -246,7 +243,7 @@ public void testShouldAllowTheUserToGetTheTextOfAPrompt() {
String value = alert.getText();
alert.accept();

assertEquals("Enter something", value);
assertThat(value).isEqualTo("Enter something");
}

@Test
Expand All @@ -257,8 +254,8 @@ public void testAlertShouldNotAllowAdditionalCommandsIfDismissed() {
Alert alert = wait.until(alertIsPresent());
alert.accept();

Throwable t = catchThrowable(alert::getText);
assertThat(t, instanceOf(NoAlertPresentException.class));
assertThatExceptionOfType(NoAlertPresentException.class)
.isThrownBy(alert::getText);
}

@SwitchToTopAfterTest
Expand All @@ -276,7 +273,7 @@ public void testShouldAllowUsersToAcceptAnAlertInAFrame() {
alert.accept();

// If we can perform any action, we're good to go
assertEquals("Testing Alerts", driver.getTitle());
assertThat(driver.getTitle()).isEqualTo("Testing Alerts");
}

@SwitchToTopAfterTest
Expand All @@ -297,15 +294,15 @@ public void testShouldAllowUsersToAcceptAnAlertInANestedFrame() {
alert.accept();

// If we can perform any action, we're good to go
assertEquals("Testing Alerts", driver.getTitle());
assertThat(driver.getTitle()).isEqualTo("Testing Alerts");
}

@Test
public void testSwitchingToMissingAlertThrows() {
driver.get(alertPage("cheese"));

Throwable t = catchThrowable(() -> driver.switchTo().alert());
assertThat(t, instanceOf(NoAlertPresentException.class));
assertThatExceptionOfType(NoAlertPresentException.class)
.isThrownBy(() -> driver.switchTo().alert());
}

@Test
Expand All @@ -321,8 +318,8 @@ public void testSwitchingToMissingAlertInAClosedWindowThrows() {
wait.until(ableToSwitchToWindow("newwindow"));
driver.close();

Throwable t = catchThrowable(() -> driver.switchTo().alert());
assertThat(t, instanceOf(NoSuchWindowException.class));
assertThatExceptionOfType(NoSuchWindowException.class)
.isThrownBy(() -> driver.switchTo().alert());

} finally {
driver.switchTo().window(mainWindow);
Expand Down Expand Up @@ -394,7 +391,7 @@ public void testShouldHandleAlertOnPageLoad() {
String value = alert.getText();
alert.accept();

assertEquals("onload", value);
assertThat(value).isEqualTo("onload");
wait.until(textInElementLocated(By.tagName("p"), "Page with onload event handler"));
}

Expand All @@ -409,7 +406,7 @@ public void testShouldHandleAlertOnPageLoadUsingGet() {
String value = alert.getText();
alert.accept();

assertEquals("onload", value);
assertThat(value).isEqualTo("onload");
wait.until(textInElementLocated(By.tagName("p"), "Page with onload event handler"));
}

Expand All @@ -433,8 +430,8 @@ public void testShouldNotHandleAlertInAnotherWindow() {
driver.findElement(By.id("open-new-window")).click();
wait.until(newWindowIsOpened(currentWindowHandles));

Throwable t = catchThrowable(() -> wait.until(alertIsPresent()));
assertThat(t, instanceOf(TimeoutException.class));
assertThatExceptionOfType(TimeoutException.class)
.isThrownBy(() -> wait.until(alertIsPresent()));

} finally {
driver.switchTo().window("newwindow");
Expand Down Expand Up @@ -466,7 +463,7 @@ public void testShouldHandleAlertOnPageUnload() {
String value = alert.getText();
alert.accept();

assertEquals("onbeforeunload", value);
assertThat(value).isEqualTo("onbeforeunload");
wait.until(textInElementLocated(By.id("link"), "open new page"));
}

Expand Down Expand Up @@ -513,7 +510,7 @@ public void testShouldHandleAlertOnWindowClose() {
String value = alert.getText();
alert.accept();

assertEquals("onbeforeunload", value);
assertThat(value).isEqualTo("onbeforeunload");

} finally {
driver.switchTo().window(mainWindow);
Expand All @@ -533,10 +530,10 @@ public void testIncludesAlertTextInUnhandledAlertException() {
driver.findElement(By.id("alert")).click();
wait.until(alertIsPresent());

Throwable t = catchThrowable(driver::getTitle);
assertThat(t, instanceOf(UnhandledAlertException.class));
assertThat(((UnhandledAlertException) t).getAlertText(), is("cheese"));
assertThat(t.getMessage(), containsString("cheese"));
assertThatExceptionOfType(UnhandledAlertException.class)
.isThrownBy(driver::getTitle)
.withMessage("cheese")
.satisfies(ex -> assertThat(ex.getAlertText()).isEqualTo("cheese"));
}

@NoDriverAfterTest
Expand Down Expand Up @@ -564,8 +561,8 @@ public void shouldHandleAlertOnFormSubmit() {
String value = alert.getText();
alert.accept();

assertEquals("Tasty cheese", value);
assertEquals("Testing Alerts", driver.getTitle());
assertThat(value).isEqualTo("Tasty cheese");
assertThat(driver.getTitle()).isEqualTo("Testing Alerts");
}

private static ExpectedCondition<Boolean> textInElementLocated(
Expand Down
Loading

0 comments on commit db1c2ad

Please sign in to comment.