What would be the best way to capture screenshots after each step when running integration tests?
Tests are written in Java using Selenium(3.0.1) and Cucumber(1.2.4).
Code for taking a screenshot after a test is below, but I need a screenshot after each method annotated with @Given, @When, @Then.
@After
public void after(Scenario scenario){
final byte[] screenshot = driver.getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
}
Thank you for any hints.
Here is the Answer to your Question:
Lets assume your methods are as follows:
You can write a library to take screenshots like:
Now you can easily call the library after every method to take the screenshot as follows:
Let me know if this Answers your Question.
Selenium exposed a interfaced called as WebDriverEventListener, which you can implement you own code generally this interface has methods like afterFindBy , beforeFindBy just need to implement that method to take screen shots.
After implementing this method then you need to inject this implemented class to driver object as shown below
Now whenever driver finds the element then it will invoke that respective injected method.
Let me know if it solves your problem
There is no afterStep annotation in cucumber java. So you cann't do it in straight forward. You can do it in another way as mentioned in @DebanjanB answer.
But this can be achievable in cucumber-ruby with after step annotation.
Solved this using Aspects. Was pretty tricky, note the annotation:
Below is the full code, written by Viviana Cattenazzi.
pom.xml
.......
StepsInterceptor.java
This might not be what you asked but this can help others too! (I haven't used Cucumber though)
Here is my code to take a screen shot and add it to a PDF file (If you want to do something else with it that you can).
Just you have to call
screenshotPDF(webDriver, testName)
method whenever you want!At this time, OP might have figured out alternative options. However this may help others. Latest version of io.cucumber has both @BeforeStep and @AfterStep, so you can embed screenshot capturing in to @AfterStep function at only one place which solves your problem.
https://stackoverflow.com/a/53135127/5885718