Tuesday, March 14, 2017

How to capture Screenshot in Selenium Webdriver (C#)


Selenium WebDriver provides the capability to capture screenshots during a test execution. This ability is really important when you need to investigate different failures that occurred during the execution or in some cases to track the different stages of the run.

The capability to take a screenshot with selenium is extracted from the "ITakesScreenshot" interface that allowing us to take a screenshot of the current page displayed in the driver instance.

תוצאת תמונה עבור ‪selenium webdriver‬‏


Remember that each screenshot that you take is translated into physical file needs to be saved in the file system, in C# we need to add a specific class that allows us to perform few basic operations in our File System (Reading and Writing), to do so, please add this code:

using System.IO;
using System.Text;
using System.Drawing;

Taking screenshot of the entire screen (All Elements)

In the following test, we will log into a webpage and take screenshot of the entire page in .JPEG format.

If you want to skip the long code example, you just can use the following code: 

((ITakesScreenshot)FirefoxInstance).GetScreenshot().SaveAsFile("File Location",System.Drawing.Imaging.ImageFormat.Jpeg);

Connecting the ITakesScreenshot with the Driver instance.
GetScreenshot ()
This method will take the screenshot
SaveAsFile ()
The method receives two parameters:

File location
Determines where do we going to save the screenshot

Determine the format of the picture

If you decide to increase your knowledge, please review the following code:

static int ScreenCounter = 0; //Will be update per screenshot that we took

public void Screenshots()
IWebDriver FirefoxInstance = new FirefoxDriver();
TakesScreenshotWithDate(FirefoxInstance, @"C:\FileLocation\","File_Name_", System.Drawing.Imaging.ImageFormat.Jpeg);

The Method Arguments:

IWebDriver object

The path that we are going to use to save the screenshots
The File name that we going to use

Specify the File Format that we are going to use

private void TakesScreenshotWithDate(IWebDriver BrowserInstance ,string Path,string FileName,System.Drawing.Imaging.ImageFormat Format)
ScreenCounter++; //Updates the number of screenshots that we took during the execution

StringBuilder TimeAndDate = new StringBuilder(DateTime.Now.ToString());
TimeAndDate.Replace(":", "_");

Remember that we cannot save a file with '/' Or ':', but we still need a unique identifier, Therefore, we will make this simple replace.

Before: 12/06/2015 14:54:55
After: 12_06_2015 14_54_55

DirectoryInfo Validation = new DirectoryInfo(Path); //System IO object

if (Validation.Exists == true) //Capture screen if the path is available
((ITakesScreenshot)BrowserInstance).GetScreenshot().SaveAsFile(Path + ScreenCounter.ToString() + "." + Filename + TimeAndDate.ToString() + "." + Format, Format);
else //Create the folder and then Capture the screen
((ITakesScreenshot)BrowserInstance).GetScreenshot().SaveAsFile(Path + ScreenCounter.ToString() + "." + FileName + TimeAndDate.ToString() + "." + Format, Format);

No comments:

Post a Comment

My Presentations