Skip to content

Commit

Permalink
Added E2E test
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreyarthaud committed Oct 23, 2019
1 parent fc579f5 commit fcd5924
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/resources/templates/calculator.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ <h2>
</div>
<div class="row">
<div class="col">
<input type="text" th:field="*{leftArgument}" class="form-control" placeholder="First Value">
<input id="left" type="text" th:field="*{leftArgument}" class="form-control" placeholder="First Value">
</div>
<div class="col">
<select th:field="*{calculationType}" class="form-control form-control-lg">
<select id="type" th:field="*{calculationType}" class="form-control form-control-lg">
<option value="ADDITION" selected="true"> + </option>
<option value="SUBTRACTION" selected="true"> - </option>
<option value="MULTIPLICATION"> x </option>
<option value="DIVISION"> / </option>
</select>
</div>
<div class="col">
<input type="text" th:field="*{rightArgument}" class="form-control" placeholder="Second Value">
<input id="right" type="text" th:field="*{rightArgument}" class="form-control" placeholder="Second Value">
</div>
</div>
<div class="row">
<div class="col">
<button type="submit" class="btn btn-primary">=</button>
<button id="submit" type="submit" class="btn btn-primary">=</button>

</div>
<div class="text-danger validation-summary-valid" data-valmsg-summary="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.openclassrooms.testing.calcul.e2e;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import io.github.bonigarcia.wdm.WebDriverManager;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class StudentMultiplicationJourneyE2E {

@LocalServerPort
private Integer port;
private WebDriver webDriver = null;
private String baseUrl;

@BeforeAll
public static void setUpFirefoxDriver() {
WebDriverManager.firefoxdriver().setup();
}

@BeforeEach
public void setUpWebDriver() {
webDriver = new FirefoxDriver();
baseUrl = "http://localhost:" + port + "/calculator";
}

@AfterEach
public void quitWebDriver() {
if (webDriver != null) {
webDriver.quit();
}
}

@Test
public void aStudentUsesTheCalculatorToMultiplyTwoBySixteen() {

// GIVEN
webDriver.get(baseUrl);
final WebElement leftField = webDriver.findElement(By.id("left"));
final WebElement typeDropdown = webDriver.findElement(By.id("type"));
final WebElement rightField = webDriver.findElement(By.id("right"));
final WebElement submitButton = webDriver.findElement(By.id("submit"));

// WHEN
leftField.sendKeys("2");
typeDropdown.sendKeys("x");
rightField.sendKeys("16");
submitButton.click();

// THEN
final WebDriverWait waiter = new WebDriverWait(webDriver, 5);
final WebElement solutionElement = waiter.until(
ExpectedConditions.presenceOfElementLocated(By.id("solution")));
final String solution = solutionElement.getText();
assertThat(solution).isEqualTo("32"); // 2 x 16
}
}

0 comments on commit fcd5924

Please sign in to comment.