How to add screenshot in extent report using specflow selenium c#

How to take a screenshot of failed testcases in selenium c# and how to add a screenshot in extent report using Specflow. In this Specflow tutorial, we will see how to add a screenshot of failed testcases in extent report. If you don’t know how to create an extent report while using Specflow you should follow this tutorial first. How to generate extent report with Specflow and selenium c#

There are many ways to capture a screenshot of the whole screen is c#. So we are going to a simple quick way. As a plan, I am going to create a custom method that you can use in your project to take a screenshot.

Steps to add a screenshot in extent report

  • Take a screenshot.
  • Save that screenshot at a location in your system’s drive.
  • Add that screenshot after your failed testcase.

How to take a screenshot in selenium C#

Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();

Save the screenshot in the driver at a specific path

screenshot.SaveAsFile(path, ScreenshotImageFormat.Png);

How to take dynamic path of the project to save screenshot

string path1 = AppDomain.CurrentDomain.BaseDirectory.Replace("\bin\Debug", "");
string path = path1+"Screenshot\" + Utility.RandomString(4, true) + ".png";

In the above code “AppDomain.CurrentDomain.BaseDirectory” will return a path till your Debug folder of the project to we need to replace “\\bin\\Debug” with the screenshot folder of the project.

“Utility.RandomString(4, true)” will create a random string of 4 char that will be name of your screenshot. And “.png” will be your image extension.

How to get the screenshot and save in drive-in C#

So, the final method will look like the below code. Now, whenever we need to get a screenshot will call this method.

publicstaticstringTakeScreenshot()
        {
string path1 = AppDomain.CurrentDomain.BaseDirectory.Replace("\bin\Debug", "");
string path = path1+"Screenshot\" + Utility.RandomString(4, true) + ".png";
            Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
            screenshot.SaveAsFile(path, ScreenshotImageFormat.Png);
return path;
        }

Add this screenshot in the extent report

scenario.AddScreenCaptureFromPath(path)

Here we need to add the path of our screenshot that we have saved in the drive by using the above method. It’s really simple let’s see an example.

//Call method will return a path as string.
string path =  LoginPage.GetShot();
//Pass this path in the ”AddScreenCaptureFromPath”
scenario.AddScreenCaptureFromPath(path);

Let’s have look of complete code to add screenshot while testcase is failed ==>

How to add a screenshot in extent report using Specflow selenium c#

if(ScenarioContext.Current.TestError != null)
            {                            
if (stepType == "Given") {
                 scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
string path = LoginPage.GetShot();
                    scenario.AddScreenCaptureFromPath(path);
                }
elseif (stepType == "When")
                {
                    scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
string path = LoginPage.GetShot();
                    scenario.AddScreenCaptureFromPath(path);
                }
elseif (stepType == "Then") { 
                    scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
string path =  LoginPage.GetShot();
                    scenario.AddScreenCaptureFromPath(path);
                }
elseif (stepType == "And")
                {
                    scenario.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
string path = LoginPage.GetShot();
                    scenario.AddScreenCaptureFromPath(path);
                }               
            }

In this above code after every step, we are calling the “GetShot(Path)” method to get the captured screenshot. And in the next line adding that shot in the extent report.

Step by Step Specflow Tutorial

 

Add a Comment

Your email address will not be published.

11 + seven =