diff --git a/src/test/java/hookdefinitions/AfterHooks.java b/src/test/java/hookdefinitions/AfterHooks.java new file mode 100644 index 0000000..9b5e4f7 --- /dev/null +++ b/src/test/java/hookdefinitions/AfterHooks.java @@ -0,0 +1,72 @@ +package hookdefinitions; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.List; + +import org.openqa.selenium.TakesScreenshot; +import org.openqa.selenium.WebDriver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Lazy; + +import io.cucumber.java.After; +import io.cucumber.java.Scenario; +import ui.pages.HomePage; + +/** + * Class containing all hooks which should be executed after each scenarios. + * + * @author helk + * + */ +public class AfterHooks { + + private Logger logger = LoggerFactory.getLogger(AfterHooks.class); + + @Autowired + WebDriver driver; + + @Lazy + @Autowired + HomePage homePage; + + @Lazy + @Qualifier("createLibrariesToDeleteList") + @Autowired + List librariesToDelete; + + @After("@createLibrary") + public void deleteLibraries() { + logger.info("Deleting Libraries..."); + + // TODO: Maybe use the REST API to delete the Libraries after the scenarios + for (String libraryName : librariesToDelete) { + homePage.navigateTo(); + homePage.openMyLibraries(); + homePage.deleteLibrary(libraryName); + } + } + + @After + public void handleTestFailure(Scenario scenario) { + if (scenario.isFailed()) { + this.takeScreenshot(scenario.getName() + "_" + scenario.getStatus().name()); + } + } + + private void takeScreenshot(String screenshotFileName) { + try { + String screenshotPath = "./target/" + screenshotFileName + "_Screenshot.jpg"; + File screenshot = ((TakesScreenshot) driver).getScreenshotAs(org.openqa.selenium.OutputType.FILE); + Files.copy(screenshot.toPath(), new File(screenshotPath).toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException exc) { + logger.error("Error copying the screenshot file.", exc); + } + } + +} diff --git a/src/test/java/hookdefinitions/InitializationHooks.java b/src/test/java/hookdefinitions/BeforeHooks.java similarity index 83% rename from src/test/java/hookdefinitions/InitializationHooks.java rename to src/test/java/hookdefinitions/BeforeHooks.java index 78d7ebe..16474f9 100644 --- a/src/test/java/hookdefinitions/InitializationHooks.java +++ b/src/test/java/hookdefinitions/BeforeHooks.java @@ -14,14 +14,14 @@ import ui.pages.StartPage; /** - * Class containing all hooks which should be executed before the scenarios. + * Class containing all hooks which should be executed before each scenarios. * * @author helk * */ -public class InitializationHooks { +public class BeforeHooks { - private Logger logger = LoggerFactory.getLogger(InitializationHooks.class); + private Logger logger = LoggerFactory.getLogger(BeforeHooks.class); @Autowired @Qualifier("loadTestDataProperties") @@ -35,7 +35,8 @@ public class InitializationHooks { StartPage startPage; // @Before(order = 1) - // -> Initialization of the WebDriver is done in TestConfiguration + // -> Initialization of the WebDriver is done in TestConfiguration via + // SCOPE_CUCUMBER_GLUE @Before(order = 2) public void openKeeperStartPage() { diff --git a/src/test/java/hookdefinitions/CleanUpHooks.java b/src/test/java/hookdefinitions/CleanUpHooks.java deleted file mode 100644 index f97a639..0000000 --- a/src/test/java/hookdefinitions/CleanUpHooks.java +++ /dev/null @@ -1,45 +0,0 @@ -package hookdefinitions; - -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.context.annotation.Lazy; - -import io.cucumber.java.After; -import ui.pages.HomePage; - -/** - * Class containing all hooks which should be executed after the scenarios. - * - * @author helk - * - */ -public class CleanUpHooks { - - private Logger logger = LoggerFactory.getLogger(CleanUpHooks.class); - - @Lazy - @Autowired - HomePage homePage; - - @Lazy - @Qualifier("createLibrariesToDeleteList") - @Autowired - List librariesToDelete; - - @After("@createLibrary") - public void deleteLibraries() { - logger.info("Deleting Libraries..."); - - // TODO: Maybe use the REST API to delete the Libraries after the scenarios - for (String libraryName : librariesToDelete) { - homePage.navigateTo(); - homePage.openMyLibraries(); - homePage.deleteLibrary(libraryName); - } - } - -}