Code Snippet


Most of the paid tools like QTP provides the facility to users for taking screenshots of the webpage during automation suit execution.User can do analysis of the failed test scripts using these screenshots so that he can analyze results faster, else he will need to perform all the steps by himself to know what exactly has gone wrong. What if the same facility can be added to our webdriver framework. Yes we can do it by designing a custom function for taking screenshots and using it in code smartly where ever needed. This will help us in doing analysis of the failed test scripts and also it will help us in reducing analysis efforts.
 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.
     

            }

1 comment:

  1. 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