Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to attach screenshot in the report using EventListener? #2309

Open
dipakkumar1225 opened this issue May 28, 2021 · 5 comments
Open

How to attach screenshot in the report using EventListener? #2309

dipakkumar1225 opened this issue May 28, 2021 · 5 comments
Labels
📖 documentation Improvements or additions to documentation good first issue Good for newcomers ⌛ stale Will soon be closed by stalebot unless there is activity 🙏 help wanted Help wanted - not prioritized by core team

Comments

@dipakkumar1225
Copy link

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

@mpkorstanje
Copy link
Contributor

mpkorstanje commented May 29, 2021

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 mpkorstanje added 📖 documentation Improvements or additions to documentation 🙏 help wanted Help wanted - not prioritized by core team good first issue Good for newcomers labels May 29, 2021
@dipakkumar1225
Copy link
Author

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

@GayanSandaruwan
Copy link
Member

GayanSandaruwan commented Aug 5, 2021

@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.

tan-j5rry added a commit to tan-j5rry/cucumber-jvm that referenced this issue Nov 12, 2021
Init commit for screenshot implementation as further detailed in issue cucumber#2309, i.e., cucumber#2309
@madalinaptj
Copy link

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?

@stale
Copy link

stale bot commented Apr 14, 2023

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 stale bot added the ⌛ stale Will soon be closed by stalebot unless there is activity label Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📖 documentation Improvements or additions to documentation good first issue Good for newcomers ⌛ stale Will soon be closed by stalebot unless there is activity 🙏 help wanted Help wanted - not prioritized by core team
Projects
None yet
Development

No branches or pull requests

4 participants