Sunday, October 6

Capturing screen shots using remote/local webdriver in C#/Selenium webdriver automation test cases

This blog provides details on how can you capture a screenshot using webdriver. You may run webdriver as remote webdriver or local webdriver . Both the webdriver has a method called GetScreenshot() which captures the screenshot of the browser’s current state. In tests it would be useful to capture the screen shot just to make sure the test has executed successfully or capture screenshots when error occurs. So this blog will provide you information on how basically one needs to make calls to these methods and you can use this code or learning from this code to implement it your framework.

Local Webdriver
Lets first look at local webdriver and how can we capture the screenshot. To run this code, you need to include package named system.Drawing. Thus you the top most part of your code is as below
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
using System.Drawing;   


The line of code to capture screenshot is
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();

From the above instance of screenshot object ss , code has to be written to save the file. For this you can use SaveAsFile method of screenshot object as shown below code. If you don’t provide the path, then file will be saved in default directory which is the directory where the dll is stored


ss.SaveAsFile("Localexplorer.jpeg", System.Drawing.Imaging.ImageFormat.Gif);


The test method having this implementation looks like this below


[Test]
public void Test_ScreenShotLocalwebdriver()
{
    // Step b - Initiating webdriver
    IWebDriver driver = new InternetExplorerDriver();
    //Step c : Making driver to navigate

    driver.Navigate().GoToUrl("http://docs.seleniumhq.org/");
    //Take the screenshot
    Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
    //Save the screenshot
    ss.SaveAsFile("Localexplorer.jpeg", System.Drawing.Imaging.ImageFormat.Gif);

    //Step d 
    IWebElement myLink = driver.FindElement(By.LinkText("Download"));
    myLink.Click();


    //Step e
    driver.Quit();

}



Remote webdriver

For the remote webdriver, the same line of code using GetScreenshot() does not work and the complier complains about wrong typecasting.  You may get error such as “

System.InvalidCastException : Unable to cast object of type 'OpenQA.Selenium.Remote.RemoteWebDriver' to type 'OpenQA.Selenium.ITakesScreenshot”

It can be overcome by creating a new class derived from orignial remotewebdriver and interface ITakesScreenShot. In this new drived class the constructor would use the base constructor , but the method getscreenshot can be overidden with new method. This derived class would like this as shown below



    public class ScreenShotRemoteWebDriver : RemoteWebDriver, ITakesScreenshot
    {
        public ScreenShotRemoteWebDriver(Uri RemoteAdress, ICapabilities capabilities)
            : base(RemoteAdress, capabilities)
        {
        }

        public Screenshot GetScreenshot()
        {
            // Get the screenshot as base64. 
            Response screenshotResponse = this.Execute(DriverCommand.Screenshot, null);
            string base64 = screenshotResponse.Value.ToString();
            // ... and convert it. 
            return new Screenshot(base64);
        } 
    }


The complete class file below shows example of two tests . One of them uses local driver and other one uses remote webdriver


using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
using System.Drawing;   


namespace NUnitSelenium
{
    [TestFixture]
    public class UnitTest1
    {

        [SetUp]
        public void SetupTest()
        {
        }
        [Test]
        public void Test_ScreenShotRemoteBrowser()
        {
            // Step b - Initiating webdriver
            IWebDriver driver = new ScreenShotRemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), DesiredCapabilities.InternetExplorer());
            //Step c : Making driver to navigate
            
            driver.Navigate().GoToUrl("http://docs.seleniumhq.org/");
            //Take the screenshot
            Screenshot ss= ((ITakesScreenshot)driver).GetScreenshot();
            //Save the screenshot
            ss.SaveAsFile("Remote browser.jpeg", System.Drawing.Imaging.ImageFormat.Gif);

            //Step d 
            IWebElement myLink = driver.FindElement(By.LinkText("Download"));
            myLink.Click();
            
            
            //Step e
            driver.Quit();

        }

        [Test]
        public void Test_ScreenShotLocalwebdriver()
        {
            // Step b - Initiating webdriver
            IWebDriver driver = new InternetExplorerDriver();
            //Step c : Making driver to navigate

            driver.Navigate().GoToUrl("http://docs.seleniumhq.org/");
            //Take the screenshot
            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            //Save the screenshot
            ss.SaveAsFile("Localexplorer.jpeg", System.Drawing.Imaging.ImageFormat.Gif);

            //Step d 
            IWebElement myLink = driver.FindElement(By.LinkText("Download"));
            myLink.Click();


            //Step e
            driver.Quit();

        }

    }

    public class ScreenShotRemoteWebDriver : RemoteWebDriver, ITakesScreenshot
    {
        public ScreenShotRemoteWebDriver(Uri RemoteAdress, ICapabilities capabilities)
            : base(RemoteAdress, capabilities)
        {
        }

        public Screenshot GetScreenshot()
        {
            // Get the screenshot as base64. 
            Response screenshotResponse = this.Execute(DriverCommand.Screenshot, null);
            string base64 = screenshotResponse.Value.ToString();
            // ... and convert it. 
            return new Screenshot(base64);
        } 
    }

}

Complie the code and then run it in the nUnit . Refer my blog on How to invoke nUnit from Visual studio express and execute tests
At the time writing this blog, for some reason, google chrome driver was not capturing full website snapshot while IE was doing full website page snapshot.

---------------------------------------------

Related Posts Plugin for WordPress, Blogger...