forked from citerus/dddsample-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Acceptance tests written by andeemarks. Converted them to run with Ht…
…mlUnit and MockMvc instead of Selenium since the sample app now uses Spring Boot.
- Loading branch information
1 parent
c412a62
commit 0602c4e
Showing
10 changed files
with
341 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/test/java/se/citerus/dddsample/acceptance/AbstractAcceptanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package se.citerus.dddsample.acceptance; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.runner.RunWith; | ||
import org.openqa.selenium.WebDriver; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import se.citerus.dddsample.Application; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@SpringBootTest(classes = Application.class) | ||
public abstract class AbstractAcceptanceTest { | ||
|
||
@Autowired | ||
private WebApplicationContext context; | ||
|
||
protected WebDriver driver; | ||
|
||
@Before | ||
public void setup() { | ||
driver = MockMvcHtmlUnitDriverBuilder.webAppContextSetup(context).contextPath("/dddsample").build(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
driver.quit(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/se/citerus/dddsample/acceptance/AdminAcceptanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package se.citerus.dddsample.acceptance; | ||
|
||
import org.junit.Test; | ||
import se.citerus.dddsample.acceptance.pages.AdminPage; | ||
import se.citerus.dddsample.acceptance.pages.CargoBookingPage; | ||
import se.citerus.dddsample.acceptance.pages.CargoDestinationPage; | ||
import se.citerus.dddsample.acceptance.pages.CargoDetailsPage; | ||
|
||
import java.time.LocalDate; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
|
||
public class AdminAcceptanceTest extends AbstractAcceptanceTest { | ||
|
||
@Test | ||
public void adminSiteCargoListContainsCannedCargo() { | ||
AdminPage page = new AdminPage(driver); | ||
page.listAllCargo(); | ||
|
||
assertTrue("Cargo list doesn't contain ABC123", page.listedCargoContains("ABC123")); | ||
assertTrue("Cargo list doesn't contain JKL567", page.listedCargoContains("JKL567")); | ||
} | ||
|
||
@Test | ||
public void adminSiteCanBookNewCargo() { | ||
AdminPage adminPage = new AdminPage(driver); | ||
|
||
CargoBookingPage cargoBookingPage = adminPage.bookNewCargo(); | ||
cargoBookingPage.selectOrigin("NLRTM"); | ||
cargoBookingPage.selectDestination("USDAL"); | ||
LocalDate arrivalDeadline = LocalDate.now().plus(3, ChronoUnit.WEEKS); | ||
cargoBookingPage.selectArrivalDeadline(arrivalDeadline); | ||
CargoDetailsPage cargoDetailsPage = cargoBookingPage.book(); | ||
|
||
String newCargoTrackingId = cargoDetailsPage.getTrackingId(); | ||
adminPage = cargoDetailsPage.listAllCargo(); | ||
assertTrue("Cargo list doesn't contain " + newCargoTrackingId, adminPage.listedCargoContains(newCargoTrackingId)); | ||
|
||
cargoDetailsPage = adminPage.showDetailsFor(newCargoTrackingId); | ||
cargoDetailsPage.expectOriginOf("NLRTM"); | ||
cargoDetailsPage.expectDestinationOf("USDAL"); | ||
|
||
CargoDestinationPage cargoDestinationPage = cargoDetailsPage.changeDestination(); | ||
cargoDetailsPage = cargoDestinationPage.selectDestinationTo("AUMEL"); | ||
cargoDetailsPage.expectDestinationOf("AUMEL"); | ||
cargoDetailsPage.expectArrivalDeadlineOf(arrivalDeadline); | ||
|
||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/se/citerus/dddsample/acceptance/CustomerAcceptanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package se.citerus.dddsample.acceptance; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import se.citerus.dddsample.acceptance.pages.CustomerPage; | ||
|
||
public class CustomerAcceptanceTest extends AbstractAcceptanceTest { | ||
private CustomerPage customerPage; | ||
|
||
@Before | ||
public void goToCustomerPage() { | ||
customerPage = new CustomerPage(driver); | ||
} | ||
|
||
@Test | ||
public void customerSiteCanTrackValidCargo() { | ||
customerPage.trackCargoWithIdOf("ABC123"); | ||
customerPage.expectCargoLocation("New York"); | ||
} | ||
|
||
@Test | ||
public void customerSiteErrorsOnInvalidCargo() { | ||
customerPage.trackCargoWithIdOf("XXX999"); | ||
customerPage.expectErrorFor("Unknown tracking id"); | ||
} | ||
|
||
@Test | ||
public void customerSiteNotifiesOnMisdirectedCargo() { | ||
customerPage.trackCargoWithIdOf("JKL567"); | ||
customerPage.expectNotificationOf("Cargo is misdirected"); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/se/citerus/dddsample/acceptance/pages/AdminPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package se.citerus.dddsample.acceptance.pages; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class AdminPage { | ||
private final WebDriver driver; | ||
|
||
public AdminPage(WebDriver driver) { | ||
this.driver = driver; | ||
driver.get("http://localhost:8080/dddsample/admin/list"); | ||
assertEquals("Cargo Administration", driver.getTitle()); | ||
} | ||
|
||
public void listAllCargo() { | ||
driver.findElement(By.linkText("List all cargos")).click(); | ||
assertEquals("Cargo Administration", driver.getTitle()); | ||
} | ||
|
||
public CargoBookingPage bookNewCargo() { | ||
driver.findElement(By.linkText("Book new cargo")).click(); | ||
|
||
return new CargoBookingPage(driver); | ||
} | ||
|
||
public boolean listedCargoContains(String expectedTrackingId) { | ||
List<WebElement> cargoList = driver.findElements(By.cssSelector("#body table tbody tr td a")); | ||
Optional<WebElement> matchingCargo = cargoList.stream().filter(cargo -> cargo.getText().equals(expectedTrackingId)).findFirst(); | ||
return matchingCargo.isPresent(); | ||
} | ||
|
||
public CargoDetailsPage showDetailsFor(String cargoTrackingId) { | ||
driver.findElement(By.linkText(cargoTrackingId)).click(); | ||
|
||
return new CargoDetailsPage(driver); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/se/citerus/dddsample/acceptance/pages/CargoBookingPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package se.citerus.dddsample.acceptance.pages; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.support.ui.Select; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class CargoBookingPage { | ||
|
||
private final WebDriver driver; | ||
|
||
public CargoBookingPage(WebDriver driver) { | ||
this.driver = driver; | ||
|
||
WebElement newCargoTableCaption = driver.findElement(By.cssSelector("table caption")); | ||
|
||
assertEquals("Book new cargo", newCargoTableCaption.getText()); | ||
} | ||
|
||
public void selectOrigin(String origin) { | ||
Select select = new Select(driver.findElement(By.name("originUnlocode"))); | ||
select.selectByVisibleText(origin); | ||
} | ||
|
||
public void selectDestination(String destination) { | ||
Select select = new Select(driver.findElement(By.name("destinationUnlocode"))); | ||
select.selectByVisibleText(destination); | ||
} | ||
|
||
public CargoDetailsPage book() { | ||
driver.findElement(By.name("originUnlocode")).submit(); | ||
|
||
return new CargoDetailsPage(driver); | ||
} | ||
|
||
public void selectArrivalDeadline(LocalDate arrivalDeadline) { | ||
WebElement datePicker = driver.findElement(By.id("arrivalDeadline")); | ||
datePicker.clear(); | ||
datePicker.sendKeys(arrivalDeadline.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/se/citerus/dddsample/acceptance/pages/CargoDestinationPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package se.citerus.dddsample.acceptance.pages; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.support.ui.Select; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
|
||
public class CargoDestinationPage { | ||
private final WebDriver driver; | ||
|
||
public CargoDestinationPage(WebDriver driver) { | ||
this.driver = driver; | ||
WebElement cargoDestinationHeader = driver.findElement(By.cssSelector("table caption")); | ||
|
||
assertTrue(cargoDestinationHeader.getText().startsWith("Change destination for cargo ")); | ||
} | ||
|
||
public CargoDetailsPage selectDestinationTo(String destination) { | ||
WebElement destinationPicker = driver.findElement(By.name("unlocode")); | ||
Select select = new Select(destinationPicker); | ||
select.selectByVisibleText(destination); | ||
|
||
destinationPicker.submit(); | ||
|
||
return new CargoDetailsPage(driver); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/test/java/se/citerus/dddsample/acceptance/pages/CargoDetailsPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package se.citerus.dddsample.acceptance.pages; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class CargoDetailsPage { | ||
public static final String TRACKING_ID_HEADER = "Details for cargo "; | ||
private final WebDriver driver; | ||
private String trackingId; | ||
|
||
public CargoDetailsPage(WebDriver driver) { | ||
this.driver = driver; | ||
|
||
WebElement newCargoTableCaption = driver.findElement(By.cssSelector("table caption")); | ||
|
||
assertTrue(newCargoTableCaption.getText().startsWith(TRACKING_ID_HEADER)); | ||
trackingId = newCargoTableCaption.getText().replaceFirst(TRACKING_ID_HEADER, ""); | ||
} | ||
|
||
public String getTrackingId() { | ||
return trackingId; | ||
} | ||
|
||
public AdminPage listAllCargo() { | ||
driver.findElement(By.linkText("List all cargos")).click(); | ||
|
||
return new AdminPage(driver); | ||
} | ||
|
||
public void expectOriginOf(String expectedOrigin) { | ||
String actualOrigin = driver.findElement(By.xpath("//div[@id='container']/table/tbody/tr[1]/td[2]")).getText(); | ||
|
||
assertEquals(expectedOrigin, actualOrigin); | ||
} | ||
|
||
public void expectDestinationOf(String expectedDestination) { | ||
String actualDestination = driver.findElement(By.xpath("//div[@id='container']/table/tbody/tr[2]/td[2]")).getText(); | ||
|
||
assertEquals(expectedDestination, actualDestination); | ||
} | ||
|
||
public CargoDestinationPage changeDestination() { | ||
driver.findElement(By.linkText("Change destination")).click(); | ||
|
||
return new CargoDestinationPage(driver); | ||
} | ||
|
||
public void expectArrivalDeadlineOf(LocalDate expectedArrivalDeadline) { | ||
String actualArrivalDeadline = driver.findElement(By.xpath("//div[@id='container']/table/tbody/tr[4]/td[2]")).getText(); | ||
|
||
assertEquals(expectedArrivalDeadline.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")), actualArrivalDeadline); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/se/citerus/dddsample/acceptance/pages/CustomerPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package se.citerus.dddsample.acceptance.pages; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class CustomerPage { | ||
private final WebDriver driver; | ||
|
||
public CustomerPage(WebDriver driver) { | ||
this.driver = driver; | ||
driver.get("http://localhost:8080/dddsample/track"); | ||
assertEquals("Tracking cargo", driver.getTitle()); | ||
} | ||
|
||
public void trackCargoWithIdOf(String trackingId) { | ||
WebElement element = driver.findElement(By.id("idInput")); | ||
element.sendKeys(trackingId); | ||
element.submit(); | ||
|
||
} | ||
|
||
public void expectCargoLocation(String expectedLocation) { | ||
WebElement cargoSummary = driver.findElement(By.cssSelector("#result h2")); | ||
assertTrue(cargoSummary.getText().endsWith(expectedLocation)); | ||
} | ||
|
||
public void expectErrorFor(String expectedErrorMessage) { | ||
WebElement error = driver.findElement(By.cssSelector(".error")); | ||
assertTrue(error.getText().endsWith(expectedErrorMessage)); | ||
} | ||
|
||
public void expectNotificationOf(String expectedNotificationMessage) { | ||
WebElement error = driver.findElement(By.cssSelector(".notify")); | ||
assertTrue(error.getText().endsWith(expectedNotificationMessage)); | ||
} | ||
} |