Skip to content
Snippets Groups Projects
Commit f173e0fd authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

#645 - improve selenium test duration: restart server not after every test method

parent d165c4cc
No related branches found
No related tags found
No related merge requests found
package de.deadlocker8.budgetmaster.integration.helpers; package de.deadlocker8.budgetmaster.integration.helpers;
import org.junit.jupiter.api.BeforeEach; import de.deadlocker8.budgetmaster.Main;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxOptions;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.annotation.DirtiesContext;
@SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SeleniumTestWatcher.class)
@DirtiesContext
@SeleniumTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class SeleniumTestBase public class SeleniumTestBase
{ {
protected WebDriver driver; protected WebDriver driver;
...@@ -13,8 +22,9 @@ public class SeleniumTestBase ...@@ -13,8 +22,9 @@ public class SeleniumTestBase
@LocalServerPort @LocalServerPort
protected int port; protected int port;
@BeforeEach @Order(1)
public final void init() @BeforeAll
public void init()
{ {
FirefoxOptions options = new FirefoxOptions(); FirefoxOptions options = new FirefoxOptions();
options.setHeadless(false); options.setHeadless(false);
...@@ -23,6 +33,11 @@ public class SeleniumTestBase ...@@ -23,6 +33,11 @@ public class SeleniumTestBase
driver.manage().window().maximize(); driver.manage().window().maximize();
} }
@AfterAll
public void afterAll() {
driver.quit();
}
public WebDriver getDriver() public WebDriver getDriver()
{ {
return driver; return driver;
......
...@@ -6,26 +6,11 @@ import org.openqa.selenium.WebDriver; ...@@ -6,26 +6,11 @@ import org.openqa.selenium.WebDriver;
public class SeleniumTestWatcher implements TestWatcher public class SeleniumTestWatcher implements TestWatcher
{ {
@Override
public void testSuccessful(ExtensionContext context)
{
final WebDriver driver = getDriver(context);
driver.quit();
}
@Override
public void testAborted(ExtensionContext context, Throwable cause)
{
final WebDriver driver = getDriver(context);
driver.quit();
}
@Override @Override
public void testFailed(ExtensionContext context, Throwable cause) public void testFailed(ExtensionContext context, Throwable cause)
{ {
final WebDriver driver = getDriver(context); final WebDriver driver = getDriver(context);
IntegrationTestHelper.saveScreenshots(driver, context.getRequiredTestMethod().getName(), context.getRequiredTestClass().getSimpleName()); IntegrationTestHelper.saveScreenshots(driver, context.getRequiredTestMethod().getName(), context.getRequiredTestClass().getSimpleName());
driver.quit();
} }
private WebDriver getDriver(ExtensionContext context) private WebDriver getDriver(ExtensionContext context)
......
package de.deadlocker8.budgetmaster.integration.selenium; package de.deadlocker8.budgetmaster.integration.selenium;
import de.deadlocker8.budgetmaster.Main;
import de.deadlocker8.budgetmaster.accounts.Account; import de.deadlocker8.budgetmaster.accounts.Account;
import de.deadlocker8.budgetmaster.accounts.AccountType; import de.deadlocker8.budgetmaster.accounts.AccountType;
import de.deadlocker8.budgetmaster.authentication.UserService; import de.deadlocker8.budgetmaster.authentication.UserService;
import de.deadlocker8.budgetmaster.integration.helpers.*; import de.deadlocker8.budgetmaster.integration.helpers.IntegrationTestHelper;
import de.deadlocker8.budgetmaster.integration.helpers.SeleniumTestBase;
import de.deadlocker8.budgetmaster.integration.helpers.TransactionTestHelper;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.By; import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import java.io.File; import java.io.File;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -26,15 +25,11 @@ import java.util.List; ...@@ -26,15 +25,11 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
@ExtendWith(SeleniumTestWatcher.class)
@SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@SeleniumTest
class NewTransactionTransferTest extends SeleniumTestBase class NewTransactionTransferTest extends SeleniumTestBase
{ {
private IntegrationTestHelper helper; private IntegrationTestHelper helper;
@BeforeEach @BeforeAll
public void prepare() public void prepare()
{ {
helper = new IntegrationTestHelper(driver, port); helper = new IntegrationTestHelper(driver, port);
...@@ -48,6 +43,10 @@ class NewTransactionTransferTest extends SeleniumTestBase ...@@ -48,6 +43,10 @@ class NewTransactionTransferTest extends SeleniumTestBase
final Account account2 = new Account("Account2", AccountType.CUSTOM); final Account account2 = new Account("Account2", AccountType.CUSTOM);
helper.uploadDatabase(path, Arrays.asList("DefaultAccount0815", "sfsdf"), List.of(account1, account2)); helper.uploadDatabase(path, Arrays.asList("DefaultAccount0815", "sfsdf"), List.of(account1, account2));
}
@BeforeEach
public void beforeEach() {
// open transactions page // open transactions page
driver.get(helper.getUrl() + "/transactions"); driver.get(helper.getUrl() + "/transactions");
driver.findElement(By.id("button-new-transaction")).click(); driver.findElement(By.id("button-new-transaction")).click();
......
...@@ -9,19 +9,15 @@ import de.deadlocker8.budgetmaster.integration.helpers.SeleniumTest; ...@@ -9,19 +9,15 @@ import de.deadlocker8.budgetmaster.integration.helpers.SeleniumTest;
import de.deadlocker8.budgetmaster.integration.helpers.SeleniumTestBase; import de.deadlocker8.budgetmaster.integration.helpers.SeleniumTestBase;
import de.deadlocker8.budgetmaster.integration.helpers.SeleniumTestWatcher; import de.deadlocker8.budgetmaster.integration.helpers.SeleniumTestWatcher;
import de.thecodelabs.utils.util.Localization; import de.thecodelabs.utils.util.Localization;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;
import org.openqa.selenium.By; import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import java.io.File; import java.io.File;
...@@ -30,14 +26,9 @@ import java.util.List; ...@@ -30,14 +26,9 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SeleniumTestWatcher.class)
@SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@SeleniumTest
class SearchTest extends SeleniumTestBase class SearchTest extends SeleniumTestBase
{ {
@BeforeEach @BeforeAll
public void prepare() public void prepare()
{ {
IntegrationTestHelper helper = new IntegrationTestHelper(driver, port); IntegrationTestHelper helper = new IntegrationTestHelper(driver, port);
...@@ -51,6 +42,10 @@ class SearchTest extends SeleniumTestBase ...@@ -51,6 +42,10 @@ class SearchTest extends SeleniumTestBase
final Account account2 = new Account("Account2", AccountType.CUSTOM); final Account account2 = new Account("Account2", AccountType.CUSTOM);
helper.uploadDatabase(path, Arrays.asList("DefaultAccount0815", "sfsdf"), List.of(account1, account2)); helper.uploadDatabase(path, Arrays.asList("DefaultAccount0815", "sfsdf"), List.of(account1, account2));
}
@BeforeEach
public void beforeEach() {
// search // search
WebElement inputSearch = driver.findElement(By.id("search")); WebElement inputSearch = driver.findElement(By.id("search"));
inputSearch.sendKeys("e"); inputSearch.sendKeys("e");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment