cucumber-jvm icon indicating copy to clipboard operation
cucumber-jvm copied to clipboard

How to attach screenshot in the report using EventListener?

Open dipakkumar1225 opened this issue 4 years ago • 5 comments

Hi, I want to capture screenshots using EventListener. I know we can use EmbedEvent for this, but there how can i use Scenario.attach()?

dipakkumar1225 avatar May 28 '21 03:05 dipakkumar1225

You can only use Scenario.attach in your glue code. For example:

import io.cucumber.java.After;
import io.cucumber.java.Scenario;

public class Attachments {

    @After
    public void after(Scenario scenario) {
        if (scenario.isFailed()) {
            byte[] data = // get screenshot from somewhere
            scenario.attach(data, "image/png", "My screenshot");          
        }
    }

When used like this, after each failed scenario an EmbedEvent is emitted.

This is currently not documented in: https://github.com/cucumber/cucumber-jvm/tree/main/java#hooks

mpkorstanje avatar May 29 '21 14:05 mpkorstanje

Thanks for the reply, As per you feedback, now I am using as below.

@After public void after(Scenario scenario) { if (scenario.isFailed()) { byte[] data =((TakesScreenshot) appiumDriver).getScreenshotAs(OutputType.BYTES); scenario.attach(data, "image/png", "My screenshot");
} }

Is there any ways to attach image using custom plugin? image https://javadoc.io/doc/io.cucumber/cucumber-plugin/latest/io/cucumber/plugin/event/EventPublisher.html

dipakkumar1225 avatar Jun 03 '21 13:06 dipakkumar1225

@dipakkumar1225 Yes you can take screenshots via a custom plugin. Just need to implement the EventListener / ConcurrentEventListener, and there you can add event listeners for the events you have mentioned above.

Probably you are looking at taking a screenshot when the scenario has failed. Following snippet will help you.

public class ScreenshotEmitter implements ConcurrentEventListener{
		
  private static final String screenshotDirectory = "./target";
  
  public void setEventPublisher(EventPublisher publisher) {
  
	  publisher.registerHandlerFor(TestStepFinished.class,this::handleTestStepFinished);
  }

private void testStepFinished(TestStepFinished event){
  Result result = event.getResult();
  
  if (!Status.PASSED.equals(result.getStatus())) {
  String message = "Test did not pass (" + result.getStatus() + "): " + currentFeature + " - " + currentScenario;
  if (StringUtils.notEmpty(screenshotDirectory) && result.getError() != null) {
	  try {
              String screenshotName = currentFeature + "_" + currentScenario + "_" + TIMESTAMP.format(new Date()) + ".png";
              File file = getController().captureScreenshot();
              
              // Get destination directory
              File destinationDir = new File(screenshotDirectory);
              
              // Rename the screenshot
              File destinationFile = new File(destinationDir.getAbsolutePath() + File.separator + screenshotName);
              
              // Copy file
              FileUtils.copyFile(file, destinationFile);
              
              String screenshotLink = String.format("<a href='%s' target='_blank'>%s</a>", screenshotDirectory + "/" + screenshotName,
		              screenshotName);
              message += " - " + screenshotLink;
	  } catch (Exception ex) {
                // Just log exceptions caught here. Don't re-throw them otherwise
                // we'll loose the original test failure cause.
                
                ex.printStackTrace();
	  }
  }
  
  System.err.println(message);
  }

}

} 

then you have to add the plugin via launcher parameters.

GayanSandaruwan avatar Aug 05 '21 16:08 GayanSandaruwan

I am trying to attach screenshots only for specific steps (based on specific strings in the step name) and want to use the EventListener as in the example above, but I cannot access the scenario object in the scope of the handler method for TestStepFinished event in order to attach the screenshot directly after taking it. Is there a way to access the scenario for such a purpose, or is this not the intended use? If not, what would be the best practice to attach a screenshot directly to a test step, not to a test case?

madalinaptj avatar Mar 30 '22 16:03 madalinaptj

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two months if no further activity occurs.

stale[bot] avatar Apr 14 '23 08:04 stale[bot]