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

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

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

Steps to add screenshot in extent report

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

How to take screenshot in selenium C#

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

Save screenshot in driver at a specific path

screenshot.SaveAsFile(path, ScreenshotImageFormat.Png);

How to take dynamic path of 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 project to we need to replace “\\bin\\Debug” with 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 get the screenshot and save in drive in C#

So, the final method will look like below code. Now whenever we need to get 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. Its 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 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 steps we are calling “GetShot(Path)” method to get the captured screenshot. And in the next line adding that shot in the extent report.

 

Add a Comment

Your email address will not be published. Required fields are marked *

three × 2 =