Taking Screenshot of Failed Testcases in Selenium C#

Today in this tutorial we will learn about How to Take Screenshots of failed Testcases using WebDriver in Selenium C#.

Use WebDriver API for Taking Screenshot

As we are going to discuss screenshots, it plays a vital role in doing failed cases. So it is always better to take the screenshot whenever the test case failed because we can visually verify the reason for failed testcases. Here we will discuss how to use the WebDriver API to take the screenshot.

Take Screenshot of  a failed testcase in selenium C#

Now in order to take the screenshot, we are going to use a method called “TakeScreenshot” and this method is coming from the “IWebDriver” interface.

Whenever we use this method for taking a screenshot it is going to return us an object of type “Screenshot” class. In this class, we have a method by that we can save the captured screenshot.

Inside our Visual Studio in our project in TestScript, we will add a directory named ScreenShot inside it we will create a class and named it TakeScreenShots.

[TestClass]
public class TakeScreenShots
{
[TestMethod]
public void ScreenShot()
{
NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite());
LinkHelper.ClickLink(By.LinkText("File a Bug");
TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_login"), ObjectRepository.Config.GetUsername());
TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_password"), ObjectRepository.Config.GetPassword());
Screenshot screen= ObjectRepository.Driver.TakeScreenshot();
screen.SaveAsFile("Screen.jpeg",ImageFormat.Jpeg);
}
}

Explanation of code:

Here we are just opening the Bugzilla webpage with the NavigationHelper, then opening the link with LinkHelper after that providing the Username, Password.

As I have told you earlier TakeScreenshot() method is coming from the IWebDriver Interface so here we will use ObjectRepository.Driver.TakeScreenshot();

This is the method. If we look at it here, it is going to return us the object of type Screenshot class so here we have written Screenshot screen= ObjectRepository.Driver.TakeScreenshot();

Now with the help of this object, we are going to save the captured screenshot, screen.SaveAsFile("Screen.jpeg",ImageFormat.Jpeg);  If we go into the class definition of Screenshot class here SaveAsFile(string filename, ImageFormat format); this is the method which we are using. 

This method takes two arguments the first argument will take the name of the file and the second argument is the image format so let us take the filename as “Screen.jpeg” and the format is ImageFormat.Jpeg

You can use other format types also such as Bmp, Gif but I recommend to you, to use Jpeg because this format takes very less amount of memory. Now we will put a breakpoint on the first line and build the solution and run the script in debug mode.

Output: First it has hit the Debug point, if we do a stepover so it has reached on Bugzilla webpage then it will click on the “File a Bug” link then provide username password and now we are going to take a screenshot after that we are going to save the screenshot.

So if we open the location of our solution SeleniumWebDriver -> Bin -> Debug.  Now inside this, as we can see here we have the file name Screen.jpeg present

and this is the screenshot we have taken.

 

Now moving on we are going to create a Generic method that will take the screenshot for us. Inside our ComponentHelper we have a GenericHelper go to the GenericHelper.cs here we will create a static method.

public static void TakeScreenshot(string filename="Screen")
{
Screenshot screen= ObjectRepository.Driver.TakeScreenshot();
 if(filename.Equals("Screen"))
 {
 filename= filename + DateTime.UtcNow.ToString("yyyy-MM-dd-mm-ss") + ".jpeg";
 screen.SaveAsFile(filename,ImageFormat.Jpeg);
 return;
 }
screen.SaveAsFile(filename,ImageFormat.Jpeg);
}

In public static void TakeScreenshot() and inside this, the argument will be a filename. So whenever we are using this type of format that represents this is a function with the default argument.

That means when the user calls this function and if he is not supplying any value for the filename argument then the default value of the filename will be “Screen” and if he is supplying any value for the filename argument this particular value will be overridden by the supplied value.

So here Screenshot screen= ObjectRepository.Driver.TakeScreenshot(); and here we are going to put a check if(filename.Equals(" "), so Equals is a method that will use to compare our filename with “Screen”.

That means the user has not supplied any value for this filename so here we will create an autogenerated name so filename= filename + DateTime.UtcNow.ToString("yyyy-MM-dd-mm-ss") + the format of filename that is .jpeg .

so here we are appending the current date-time with the filename, DateTime.UtcNow is a static property that returns us the current date time and we are converting the particular value in the form of a string with ToString(“yyyy-MM-dd-mm-ss”) this is the format and then appending the extension.

After this, we will call screen.SavsAsFile(filename,ImageFormat.Jpeg); and after this, we are returning this to the function and in case the user has supplied any filename, so we will use screen.SaveAsFile(filename,ImageFormat.Jpeg); in that case, this if condition will be false and it is going to execute screen.SaveAsFile(filename,ImageFormat.Jpeg); this particular statement.

In our TakeScreenShots.cs we will put a breakpoint on the NavigationHelper line and in our class, we are going to call GenericHelper.TakeScreenshot first with, without argument, and after this with an argument that is ("Test.jpeg");

[TestClass] 
public class TakeScreenShots 
{ 
[TestMethod] 
public void ScreenShot() 
{ NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite()); 
LinkHelper.ClickLink(By.LinkText("File a Bug"); TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_login"), ObjectRepository.Config.GetUsername()); TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_password"), ObjectRepository.Config.GetPassword()); 
//Screenshot screen= ObjectRepository.Driver.TakeScreenshot(); //screen.SaveAsFile("Screen.jpeg",ImageFormat.Jpeg); 
GenericHelper.TakeScreenshot();
GenericHelper.TakeScreenshot("Test.jpeg");
} 
}

again build the solution and run the script in debug mode.

Output: First it has hit the Debug point if we do a stepover so it will open the Bugzilla webpage, then it will click on the “File a Bug” link then it will provide the username and password, and now this time we have not supplied any value inside this method so as we can see here the default value of filename is “Screen”.

So it has taken the screenshot and now this condition will be true so it will go inside as we can see here the name “Screen” has appended with the current date and time and it is going to save the screenshot and return from the function.

Doing a stepover now this time we have supplied the value of the filename as we can see here the current value of the filename is “Test.jpeg” it will take the screenshot, the condition is false and again it will take the screenshot and return from the function.

So if we will open the location of our solution inside SeleniumWebDriver -> Bin -> Debug. We can see here the filename with appended date time and this is the screenshot which is taken by the method where we are not supplying any filename

and this is the screenshot in which we have supplied the “Test.jpeg” in argument as the filename.

So in this manner, we can use WebDriver API for taking the screenshot in Selenium C#.