Designers of webdriver have already given this functionality in their API by introducing a interface ITakesScreenshot . I have just created a custom function to be used easily by the my team.
.
Here is the custom function designed using ITakesScreenshot of WebDriver
public void TakeScreenShotOfPage(string filePath, string ImageNameWithExtention)
{
string ImageName = ImageNameWithExtention;
int index = ImageName.LastIndexOf(".");
string ImageFormat = ImageName.Substring(index+1);
string filePath1 = System.IO.Path.Combine(filePath,ImageName);
System.Drawing.Imaging.ImageFormat i = null;
switch (ImageFormat)
{
case "BMP":
{
i = System.Drawing.Imaging.ImageFormat.Bmp;
}
break;
case "PNG":
default:
{
i = System.Drawing.Imaging.ImageFormat.Png;
}
break;
case "GIF":
{
i = System.Drawing.Imaging.ImageFormat.Gif;
}
break;
case "JPEG":
{
i = System.Drawing.Imaging.ImageFormat.Jpeg;
}
break;
case "TIFF":
{
i = System.Drawing.Imaging.ImageFormat.Tiff;
}
break;
}
((ITakesScreenshot)m_Browser).GetScreenshot().SaveAsFile(filePath1,i);
}
Now this function can be used anywhere in your scripts or library to define in a way so that on failure you can get screen shots.
YellowSearchPage y = new YellowSearchPage();
y.SearchWhat = "Hotel";
y.SearchWhere = "Amelin, Lubartowski, Lubelskie";
y.ClickSearchButton();
if (y.IsElementPresent("ByName", "Header", false))
{
System.Console.Write("Success");
}
else
{
y.TakeScreenShotOfPage("c:\\", "Test124.PNG");
// Please note that you image name can be generated using TestCaseID and TestCase Name to have it more specific.
}
Please note that for above program to work one needs to add system.drawing dll in the reference path and add a line "Using System.drawing;" to your code
ReplyDelete