diff --git a/src/test/java/de/deadlocker8/budgetmaster/integration/SearchTest.java b/src/test/java/de/deadlocker8/budgetmaster/integration/SearchTest.java index f8fe97108630750308e5de45983804d191e88add..5a003a6647ac695be038a870fe22d81632a8e311 100644 --- a/src/test/java/de/deadlocker8/budgetmaster/integration/SearchTest.java +++ b/src/test/java/de/deadlocker8/budgetmaster/integration/SearchTest.java @@ -5,6 +5,7 @@ import de.deadlocker8.budgetmaster.authentication.UserService; import de.deadlocker8.budgetmaster.integration.helpers.IntegrationTestHelper; import de.deadlocker8.budgetmaster.integration.helpers.SeleniumTest; import de.thecodelabs.utils.util.Localization; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -12,12 +13,12 @@ import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; -import org.openqa.selenium.support.ui.ExpectedConditions; -import org.openqa.selenium.support.ui.WebDriverWait; -import org.springframework.beans.factory.annotation.Autowired; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.firefox.FirefoxOptions; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; import java.io.File; import java.util.Arrays; @@ -27,13 +28,12 @@ import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -@RunWith(SpringJUnit4ClassRunner.class) +@RunWith(SpringRunner.class) @SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) @SeleniumTest public class SearchTest { - @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") - @Autowired private WebDriver driver; @LocalServerPort @@ -42,6 +42,10 @@ public class SearchTest @Before public void prepare() { + FirefoxOptions options = new FirefoxOptions(); + options.setHeadless(true); + driver = new FirefoxDriver(options); + // prepare IntegrationTestHelper helper = new IntegrationTestHelper(driver, port); helper.start(); @@ -57,6 +61,11 @@ public class SearchTest driver.findElement(By.id("buttonSearch")).click(); } + @After + public void adftet() { + driver.quit(); + } + @Test public void searchFromNavbar() { diff --git a/src/test/java/de/deadlocker8/budgetmaster/integration/helpers/SeleniumTestExecutionListener.java b/src/test/java/de/deadlocker8/budgetmaster/integration/helpers/SeleniumTestExecutionListener.java index 3f6f98f5e4f100bee2ca90fd7a61f899c26ea41d..742d3dba7254b40697217a95949170139240643f 100644 --- a/src/test/java/de/deadlocker8/budgetmaster/integration/helpers/SeleniumTestExecutionListener.java +++ b/src/test/java/de/deadlocker8/budgetmaster/integration/helpers/SeleniumTestExecutionListener.java @@ -1,27 +1,18 @@ package de.deadlocker8.budgetmaster.integration.helpers; -import org.openqa.selenium.OutputType; -import org.openqa.selenium.TakesScreenshot; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.firefox.FirefoxDriver; -import org.openqa.selenium.firefox.FirefoxOptions; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; +import de.thecodelabs.utils.util.Localization; +import de.thecodelabs.utils.util.SystemUtils; import org.springframework.core.Ordered; import org.springframework.test.context.TestContext; import org.springframework.test.context.support.AbstractTestExecutionListener; -import java.io.File; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; public class SeleniumTestExecutionListener extends AbstractTestExecutionListener { - private WebDriver webDriver; - @Override public int getOrder() { @@ -35,57 +26,36 @@ public class SeleniumTestExecutionListener extends AbstractTestExecutionListener { throw new RuntimeException("Test profile not activated. Skipping tests. (Set -DtestProfile=true in your VM arguments)"); } - - if(webDriver != null) - { - return; - } - ApplicationContext context = testContext.getApplicationContext(); - if(context instanceof ConfigurableApplicationContext) - { - - FirefoxOptions options = new FirefoxOptions(); - options.setHeadless(true); - webDriver = new FirefoxDriver(options); - - ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) context; - ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory(); - bf.registerResolvableDependency(WebDriver.class, webDriver); - } - } - - @Override - public void beforeTestMethod(TestContext testContext) - { - } - - @Override - public void afterTestClass(TestContext testContext) - { - if(webDriver != null) - { - webDriver.quit(); - } } @Override public void afterTestMethod(TestContext testContext) throws Exception { - if(testContext.getTestException() == null) + final Path path = SystemUtils.getApplicationSupportDirectoryPath(Localization.getString("folder"), "test", "budgetmaster.mv.db"); + try { - return; + Files.deleteIfExists(path); } - File screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE); - - String testName = testContext.getTestClass().getSimpleName(); - String methodName = testContext.getTestMethod().getName(); - final Path destination = Paths.get("screenshots", testName + "_" + methodName + "_" + screenshot.getName()); - - if(Files.notExists(destination.getParent())) + catch(IOException e) { - Files.createDirectories(destination.getParent()); + e.printStackTrace(); } - Files.copy(screenshot.toPath(), destination); +// if(testContext.getTestException() == null) +// { +// return; +// } +// File screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE); +// +// String testName = testContext.getTestClass().getSimpleName(); +// String methodName = testContext.getTestMethod().getName(); +// final Path destination = Paths.get("screenshots", testName + "_" + methodName + "_" + screenshot.getName()); +// +// if(Files.notExists(destination.getParent())) +// { +// Files.createDirectories(destination.getParent()); +// } +// +// Files.copy(screenshot.toPath(), destination); } }