Add a test using selenium

This commit is contained in:
Manuel Thalmann 2023-10-10 17:33:42 +02:00
parent f1710e481b
commit b8b0a56cb1

View file

@ -3,7 +3,6 @@ package eu.profinit.education.flightlog;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.openqa.selenium.By; import org.openqa.selenium.By;
@ -17,11 +16,9 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import eu.profinit.education.flightlog.configuration.WebDriverConfiguration; import eu.profinit.education.flightlog.configuration.WebDriverConfiguration;
import lombok.extern.slf4j.Slf4j;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = WebDriverConfiguration.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = WebDriverConfiguration.class)
@Slf4j
@ActiveProfiles("inttest") @ActiveProfiles("inttest")
public class FlightLogSeleniumTests { public class FlightLogSeleniumTests {
@ -41,9 +38,46 @@ public class FlightLogSeleniumTests {
webDriver.quit(); webDriver.quit();
} }
@Ignore("Test is not implemented")
@Test @Test
public void testAddNewFlight() throws Exception { public void testAddNewFlight() throws Exception {
// TODO tutorial-3.5: Implement an end to end test using Selenium that registers a new flight and checks whether it was created // wait till the application is fully loaded
new WebDriverWait(webDriver, 5).until(ExpectedConditions.numberOfElementsToBe(By.xpath("//tbody//tr"), 1));
// get the number of flights already registered, there should already be one
int numberOfRegisteredFlightsBefore = webDriver.findElements(By.xpath("//tbody//tr")).size();
assertEquals("There is a one flight at the start of the application", 1, numberOfRegisteredFlightsBefore);
// open new flight tab
webDriver.findElement(By.xpath("/html/body/div/div/ul/li[2]/a")).click();
// fill in information about a flight and confirm it
webDriver.findElement(By.xpath("//*[@id=\"takeoffTime\"]")).sendKeys("27100020221830");
webDriver.findElement(By.xpath("/html/body/router-view/div/form/compose[2]/compose/div[1]/div/div/label[3]"))
.click();
// let's wait till the pilots will be loaded into the form
new WebDriverWait(webDriver, 30)
.until(ExpectedConditions.presenceOfNestedElementsLocatedBy(
By.xpath("/html/body/router-view/div/form/compose[1]/div[2]/compose/div[3]/div/select"),
By.tagName("option")));
webDriver.findElement(By.xpath("/html/body/router-view/div/form/div[2]/div/button")).click();
// let's wait till the data will be processed and saved
new WebDriverWait(webDriver, 60).until(ExpectedConditions.alertIsPresent());
webDriver.switchTo().alert().accept();
// move to a list of all flights
webDriver.findElement(By.xpath("/html/body/div/div/ul/li[1]/a")).click();
// wait till records will appear on the page
new WebDriverWait(webDriver, 5)
.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//tbody//tr")));
// get a list of created flights
int numberOfRegisteredFlightsAfter = webDriver.findElements(By.xpath("//tbody//tr")).size();
// perform necessarry checks
assertEquals("There should be only one new flight created.", 1,
numberOfRegisteredFlightsAfter - numberOfRegisteredFlightsBefore);
} }
} }