Wednesday, May 27, 2020

Cucumber Selenium test automation framework using C# - Part 8 - Attaching Screenshots to the report

Prerequisites


Please complete Part 7 of this tutorial before continuing.

Adding screenshots to test report using Extent Reports


1) In order to maintain multiple screenshots before adding them to the report, we need to add a public class in Utility package and name it as StepImageContext and add the following content to it.

public string StepInformation { get; set; }
public string ScreenshotPathWithName { get; set; }


2) We need to have a method to capture screenshot and save in given location and another method to add screenshots with step information into the instance of StepImageContext list. Add these two methods as given below.

3) After adding above methods, navigate to Hooks.cs class and create a method to add screenshots to log. We need to check if StepImageContext instance is empty. Attaching screenshot to log should be available only if at least one screenshot is added. Also create another method to add screenshot to the log when there is a failure.

4) Next step is to call the screenshot capture method (LogInfoWithScreenshot) to all the places step information is added as given below. Do not add this method to Step definition pending section.

5) At the end of the AfterStep() method, clear the current scenario context.

6) Once you complete that, define an object of StepImageContext and add screenshots as required. A sample is shown below. Make sure to add screenshots for each step.

6) Rebuild the code and execute the test. In the report you should see added screenshots.