소스 검색

Acceptance tests written by andeemarks. Converted them to run with HtmlUnit and MockMvc instead of Selenium since the sample app now uses Spring Boot.

marlin 9 년 전
부모
커밋
0602c4e9bd

+ 7
- 1
pom.xml 파일 보기

@@ -151,7 +151,7 @@
151 151
         <dependency>
152 152
             <groupId>commons-io</groupId>
153 153
             <artifactId>commons-io</artifactId>
154
-            <version>1.3.1</version>
154
+            <version>2.4</version>
155 155
         </dependency>
156 156
         <dependency>
157 157
             <groupId>commons-dbcp</groupId>
@@ -191,6 +191,12 @@
191 191
             <version>2.3</version>
192 192
             <scope>test</scope>
193 193
         </dependency>
194
+        <dependency>
195
+            <groupId>org.seleniumhq.selenium</groupId>
196
+            <artifactId>selenium-java</artifactId>
197
+            <version>3.5.3</version>
198
+            <scope>test</scope>
199
+        </dependency>
194 200
     </dependencies>
195 201
 
196 202
     <reporting>

+ 1
- 1
src/main/java/se/citerus/dddsample/interfaces/booking/web/CargoAdminController.java 파일 보기

@@ -61,7 +61,7 @@ public final class CargoAdminController {
61 61
     @RequestMapping(value = "/register", method = RequestMethod.POST)
62 62
     public void register(HttpServletRequest request, HttpServletResponse response,
63 63
                          RegistrationCommand command) throws Exception {
64
-        Date arrivalDeadline = new SimpleDateFormat("M/dd/yyyy").parse(command.getArrivalDeadline());
64
+        Date arrivalDeadline = new SimpleDateFormat("dd/MM/yyyy").parse(command.getArrivalDeadline());
65 65
         String trackingId = bookingServiceFacade.bookNewCargo(
66 66
                 command.getOriginUnlocode(), command.getDestinationUnlocode(), arrivalDeadline
67 67
         );

+ 32
- 0
src/test/java/se/citerus/dddsample/acceptance/AbstractAcceptanceTest.java 파일 보기

@@ -0,0 +1,32 @@
1
+package se.citerus.dddsample.acceptance;
2
+
3
+import org.junit.After;
4
+import org.junit.Before;
5
+import org.junit.runner.RunWith;
6
+import org.openqa.selenium.WebDriver;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.boot.test.context.SpringBootTest;
9
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10
+import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder;
11
+import org.springframework.web.context.WebApplicationContext;
12
+import se.citerus.dddsample.Application;
13
+
14
+@RunWith(SpringJUnit4ClassRunner.class)
15
+@SpringBootTest(classes = Application.class)
16
+public abstract class AbstractAcceptanceTest {
17
+
18
+    @Autowired
19
+    private WebApplicationContext context;
20
+
21
+    protected WebDriver driver;
22
+
23
+    @Before
24
+    public void setup() {
25
+        driver = MockMvcHtmlUnitDriverBuilder.webAppContextSetup(context).contextPath("/dddsample").build();
26
+    }
27
+
28
+    @After
29
+    public void tearDown() {
30
+        driver.quit();
31
+    }
32
+}

+ 50
- 0
src/test/java/se/citerus/dddsample/acceptance/AdminAcceptanceTest.java 파일 보기

@@ -0,0 +1,50 @@
1
+package se.citerus.dddsample.acceptance;
2
+
3
+import org.junit.Test;
4
+import se.citerus.dddsample.acceptance.pages.AdminPage;
5
+import se.citerus.dddsample.acceptance.pages.CargoBookingPage;
6
+import se.citerus.dddsample.acceptance.pages.CargoDestinationPage;
7
+import se.citerus.dddsample.acceptance.pages.CargoDetailsPage;
8
+
9
+import java.time.LocalDate;
10
+import java.time.temporal.ChronoUnit;
11
+
12
+import static junit.framework.TestCase.assertTrue;
13
+
14
+public class AdminAcceptanceTest extends AbstractAcceptanceTest {
15
+
16
+    @Test
17
+    public void adminSiteCargoListContainsCannedCargo() {
18
+        AdminPage page = new AdminPage(driver);
19
+        page.listAllCargo();
20
+
21
+        assertTrue("Cargo list doesn't contain ABC123", page.listedCargoContains("ABC123"));
22
+        assertTrue("Cargo list doesn't contain JKL567", page.listedCargoContains("JKL567"));
23
+    }
24
+
25
+    @Test
26
+    public void adminSiteCanBookNewCargo() {
27
+        AdminPage adminPage = new AdminPage(driver);
28
+
29
+        CargoBookingPage cargoBookingPage = adminPage.bookNewCargo();
30
+        cargoBookingPage.selectOrigin("NLRTM");
31
+        cargoBookingPage.selectDestination("USDAL");
32
+        LocalDate arrivalDeadline = LocalDate.now().plus(3, ChronoUnit.WEEKS);
33
+        cargoBookingPage.selectArrivalDeadline(arrivalDeadline);
34
+        CargoDetailsPage cargoDetailsPage = cargoBookingPage.book();
35
+
36
+        String newCargoTrackingId = cargoDetailsPage.getTrackingId();
37
+        adminPage = cargoDetailsPage.listAllCargo();
38
+        assertTrue("Cargo list doesn't contain " + newCargoTrackingId, adminPage.listedCargoContains(newCargoTrackingId));
39
+
40
+        cargoDetailsPage = adminPage.showDetailsFor(newCargoTrackingId);
41
+        cargoDetailsPage.expectOriginOf("NLRTM");
42
+        cargoDetailsPage.expectDestinationOf("USDAL");
43
+
44
+        CargoDestinationPage cargoDestinationPage = cargoDetailsPage.changeDestination();
45
+        cargoDetailsPage = cargoDestinationPage.selectDestinationTo("AUMEL");
46
+        cargoDetailsPage.expectDestinationOf("AUMEL");
47
+        cargoDetailsPage.expectArrivalDeadlineOf(arrivalDeadline);
48
+
49
+    }
50
+}

+ 33
- 0
src/test/java/se/citerus/dddsample/acceptance/CustomerAcceptanceTest.java 파일 보기

@@ -0,0 +1,33 @@
1
+package se.citerus.dddsample.acceptance;
2
+
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+import se.citerus.dddsample.acceptance.pages.CustomerPage;
6
+
7
+public class CustomerAcceptanceTest extends AbstractAcceptanceTest {
8
+    private CustomerPage customerPage;
9
+
10
+    @Before
11
+    public void goToCustomerPage() {
12
+        customerPage = new CustomerPage(driver);
13
+    }
14
+
15
+    @Test
16
+    public void customerSiteCanTrackValidCargo() {
17
+        customerPage.trackCargoWithIdOf("ABC123");
18
+        customerPage.expectCargoLocation("New York");
19
+    }
20
+
21
+    @Test
22
+    public void customerSiteErrorsOnInvalidCargo() {
23
+        customerPage.trackCargoWithIdOf("XXX999");
24
+        customerPage.expectErrorFor("Unknown tracking id");
25
+    }
26
+
27
+    @Test
28
+    public void customerSiteNotifiesOnMisdirectedCargo() {
29
+        customerPage.trackCargoWithIdOf("JKL567");
30
+        customerPage.expectNotificationOf("Cargo is misdirected");
31
+    }
32
+
33
+}

+ 43
- 0
src/test/java/se/citerus/dddsample/acceptance/pages/AdminPage.java 파일 보기

@@ -0,0 +1,43 @@
1
+package se.citerus.dddsample.acceptance.pages;
2
+
3
+import org.openqa.selenium.By;
4
+import org.openqa.selenium.WebDriver;
5
+import org.openqa.selenium.WebElement;
6
+
7
+import java.util.List;
8
+import java.util.Optional;
9
+
10
+import static org.junit.Assert.assertEquals;
11
+
12
+public class AdminPage {
13
+    private final WebDriver driver;
14
+
15
+    public AdminPage(WebDriver driver) {
16
+        this.driver = driver;
17
+        driver.get("http://localhost:8080/dddsample/admin/list");
18
+        assertEquals("Cargo Administration", driver.getTitle());
19
+    }
20
+
21
+    public void listAllCargo() {
22
+        driver.findElement(By.linkText("List all cargos")).click();
23
+        assertEquals("Cargo Administration", driver.getTitle());
24
+    }
25
+
26
+    public CargoBookingPage bookNewCargo() {
27
+        driver.findElement(By.linkText("Book new cargo")).click();
28
+
29
+        return new CargoBookingPage(driver);
30
+    }
31
+
32
+    public boolean listedCargoContains(String expectedTrackingId) {
33
+        List<WebElement> cargoList = driver.findElements(By.cssSelector("#body table tbody tr td a"));
34
+        Optional<WebElement> matchingCargo = cargoList.stream().filter(cargo -> cargo.getText().equals(expectedTrackingId)).findFirst();
35
+        return matchingCargo.isPresent();
36
+    }
37
+
38
+    public CargoDetailsPage showDetailsFor(String cargoTrackingId) {
39
+        driver.findElement(By.linkText(cargoTrackingId)).click();
40
+
41
+        return new CargoDetailsPage(driver);
42
+    }
43
+}

+ 46
- 0
src/test/java/se/citerus/dddsample/acceptance/pages/CargoBookingPage.java 파일 보기

@@ -0,0 +1,46 @@
1
+package se.citerus.dddsample.acceptance.pages;
2
+
3
+import org.openqa.selenium.By;
4
+import org.openqa.selenium.WebDriver;
5
+import org.openqa.selenium.WebElement;
6
+import org.openqa.selenium.support.ui.Select;
7
+
8
+import java.time.LocalDate;
9
+import java.time.format.DateTimeFormatter;
10
+
11
+import static org.junit.Assert.assertEquals;
12
+
13
+public class CargoBookingPage {
14
+
15
+    private final WebDriver driver;
16
+
17
+    public CargoBookingPage(WebDriver driver) {
18
+        this.driver = driver;
19
+
20
+        WebElement newCargoTableCaption = driver.findElement(By.cssSelector("table caption"));
21
+
22
+        assertEquals("Book new cargo", newCargoTableCaption.getText());
23
+    }
24
+
25
+    public void selectOrigin(String origin) {
26
+        Select select = new Select(driver.findElement(By.name("originUnlocode")));
27
+        select.selectByVisibleText(origin);
28
+    }
29
+
30
+    public void selectDestination(String destination) {
31
+        Select select = new Select(driver.findElement(By.name("destinationUnlocode")));
32
+        select.selectByVisibleText(destination);
33
+    }
34
+
35
+    public CargoDetailsPage book() {
36
+        driver.findElement(By.name("originUnlocode")).submit();
37
+
38
+        return new CargoDetailsPage(driver);
39
+    }
40
+
41
+    public void selectArrivalDeadline(LocalDate arrivalDeadline) {
42
+        WebElement datePicker = driver.findElement(By.id("arrivalDeadline"));
43
+        datePicker.clear();
44
+        datePicker.sendKeys(arrivalDeadline.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
45
+    }
46
+}

+ 29
- 0
src/test/java/se/citerus/dddsample/acceptance/pages/CargoDestinationPage.java 파일 보기

@@ -0,0 +1,29 @@
1
+package se.citerus.dddsample.acceptance.pages;
2
+
3
+import org.openqa.selenium.By;
4
+import org.openqa.selenium.WebDriver;
5
+import org.openqa.selenium.WebElement;
6
+import org.openqa.selenium.support.ui.Select;
7
+
8
+import static junit.framework.TestCase.assertTrue;
9
+
10
+public class CargoDestinationPage {
11
+    private final WebDriver driver;
12
+
13
+    public CargoDestinationPage(WebDriver driver) {
14
+        this.driver = driver;
15
+        WebElement cargoDestinationHeader = driver.findElement(By.cssSelector("table caption"));
16
+
17
+        assertTrue(cargoDestinationHeader.getText().startsWith("Change destination for cargo "));
18
+    }
19
+
20
+    public CargoDetailsPage selectDestinationTo(String destination) {
21
+        WebElement destinationPicker = driver.findElement(By.name("unlocode"));
22
+        Select select = new Select(destinationPicker);
23
+        select.selectByVisibleText(destination);
24
+
25
+        destinationPicker.submit();
26
+
27
+        return new CargoDetailsPage(driver);
28
+    }
29
+}

+ 60
- 0
src/test/java/se/citerus/dddsample/acceptance/pages/CargoDetailsPage.java 파일 보기

@@ -0,0 +1,60 @@
1
+package se.citerus.dddsample.acceptance.pages;
2
+
3
+import org.openqa.selenium.By;
4
+import org.openqa.selenium.WebDriver;
5
+import org.openqa.selenium.WebElement;
6
+
7
+import java.time.LocalDate;
8
+import java.time.format.DateTimeFormatter;
9
+
10
+import static junit.framework.TestCase.assertTrue;
11
+import static org.junit.Assert.assertEquals;
12
+
13
+public class CargoDetailsPage {
14
+    public static final String TRACKING_ID_HEADER = "Details for cargo ";
15
+    private final WebDriver driver;
16
+    private String trackingId;
17
+
18
+    public CargoDetailsPage(WebDriver driver) {
19
+        this.driver = driver;
20
+
21
+        WebElement newCargoTableCaption = driver.findElement(By.cssSelector("table caption"));
22
+
23
+        assertTrue(newCargoTableCaption.getText().startsWith(TRACKING_ID_HEADER));
24
+        trackingId = newCargoTableCaption.getText().replaceFirst(TRACKING_ID_HEADER, "");
25
+    }
26
+
27
+    public String getTrackingId() {
28
+        return trackingId;
29
+    }
30
+
31
+    public AdminPage listAllCargo() {
32
+        driver.findElement(By.linkText("List all cargos")).click();
33
+
34
+        return new AdminPage(driver);
35
+    }
36
+
37
+    public void expectOriginOf(String expectedOrigin) {
38
+        String actualOrigin = driver.findElement(By.xpath("//div[@id='container']/table/tbody/tr[1]/td[2]")).getText();
39
+
40
+        assertEquals(expectedOrigin, actualOrigin);
41
+    }
42
+
43
+    public void expectDestinationOf(String expectedDestination) {
44
+        String actualDestination = driver.findElement(By.xpath("//div[@id='container']/table/tbody/tr[2]/td[2]")).getText();
45
+
46
+        assertEquals(expectedDestination, actualDestination);
47
+    }
48
+
49
+    public CargoDestinationPage changeDestination() {
50
+        driver.findElement(By.linkText("Change destination")).click();
51
+
52
+        return new CargoDestinationPage(driver);
53
+    }
54
+
55
+    public void expectArrivalDeadlineOf(LocalDate expectedArrivalDeadline) {
56
+        String actualArrivalDeadline = driver.findElement(By.xpath("//div[@id='container']/table/tbody/tr[4]/td[2]")).getText();
57
+
58
+        assertEquals(expectedArrivalDeadline.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")), actualArrivalDeadline);
59
+    }
60
+}

+ 40
- 0
src/test/java/se/citerus/dddsample/acceptance/pages/CustomerPage.java 파일 보기

@@ -0,0 +1,40 @@
1
+package se.citerus.dddsample.acceptance.pages;
2
+
3
+import org.openqa.selenium.By;
4
+import org.openqa.selenium.WebDriver;
5
+import org.openqa.selenium.WebElement;
6
+
7
+import static org.junit.Assert.assertEquals;
8
+import static org.junit.Assert.assertTrue;
9
+
10
+public class CustomerPage {
11
+    private final WebDriver driver;
12
+
13
+    public CustomerPage(WebDriver driver) {
14
+        this.driver = driver;
15
+        driver.get("http://localhost:8080/dddsample/track");
16
+        assertEquals("Tracking cargo", driver.getTitle());
17
+    }
18
+
19
+    public void trackCargoWithIdOf(String trackingId) {
20
+        WebElement element = driver.findElement(By.id("idInput"));
21
+        element.sendKeys(trackingId);
22
+        element.submit();
23
+
24
+    }
25
+
26
+    public void expectCargoLocation(String expectedLocation) {
27
+        WebElement cargoSummary = driver.findElement(By.cssSelector("#result h2"));
28
+        assertTrue(cargoSummary.getText().endsWith(expectedLocation));
29
+    }
30
+
31
+    public void expectErrorFor(String expectedErrorMessage) {
32
+        WebElement error = driver.findElement(By.cssSelector(".error"));
33
+        assertTrue(error.getText().endsWith(expectedErrorMessage));
34
+    }
35
+
36
+    public void expectNotificationOf(String expectedNotificationMessage) {
37
+        WebElement error = driver.findElement(By.cssSelector(".notify"));
38
+        assertTrue(error.getText().endsWith(expectedNotificationMessage));
39
+    }
40
+}