-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1 Take a screenshot after each failed scenario.
-> A screenshot helps identifying the cause of a failed test much faster - The screenshot is saved in the projects target directory
- Loading branch information
Showing
3 changed files
with
77 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.