Skip to content

Commit

Permalink
#1 Take a screenshot after each failed scenario.
Browse files Browse the repository at this point in the history
-> A screenshot helps identifying the cause of a failed test much faster
- The screenshot is saved in the projects target directory
  • Loading branch information
helkv committed Mar 23, 2020
1 parent d2634cc commit 76a6391
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 49 deletions.
72 changes: 72 additions & 0 deletions src/test/java/hookdefinitions/AfterHooks.java
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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() {
Expand Down
45 changes: 0 additions & 45 deletions src/test/java/hookdefinitions/CleanUpHooks.java

This file was deleted.

0 comments on commit 76a6391

Please sign in to comment.