0
votes

I am trying to take the screenshot of a test being executed in Selenium WebDriver in C#. I am using Gallio to run my tests. Below is my code for the screenshot:

public void TakeScreenshot(IWebDriver driver, string saveLocation)
{
    ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
    Screenshot screenshot = screenshotDriver.GetScreenshot();
    screenshot.SaveAsFile(saveLocation, ImageFormat.Png);
}

I am using the below code in my tests to take the screenshot:

IWebDriver driver = new ChromeDriver();
TakeScreenshot(driver, @"C:\screenshot.png");

The problem is that while executing, the statement opens up a new chrome window and then pops up an error. My aim is to take the screenshot of an existing running script. What changes should I do to make this happen?

P.S. - Is there a way to take the screenshot while executing the test without initializing a new driver?

3
What version of Chrome are you running? What version of ChromeDriver are you running? Post a screenshot of your "Debug" folder after building your tests.Arran

3 Answers

3
votes

Capturing a ScreenShot using Selenium Webdriver With C#.Net, NUnit.

// 1- Add a reference of System.Drawing in your solution/project.
// 2- use system.Drawing.Imaging namespace in your test.

// Here I am capturing the screen shot of Facebook Home page.

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using NUnit.Framework;
using System.IO;
using System.Collections;
using System.Drawing.Imaging;

namespace FacebookRegistrationUsingC_Sharp
{
    [TestFixture]
    public class ScreenShot
    {
        IWebDriver driver = null;
        IWebElement element = null;

        [SetUp]
        public void SetUp()
        {
            driver = new ChromeDriver("G:\\Selenium_Csharp\\Jar\\chromedriver_win32");           
            driver.Navigate().GoToUrl("https://www.Facebook.com");
            driver.Manage().Window.Maximize();

        }
        [Test]
        public void TestScreenShot()
        {           

            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            ss.SaveAsFile("e:\\pande", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        [TearDown]
        public void TearDown()
        {
            driver = null;
            element = null;
        }
    }
}
0
votes

For the error where chromedriver.exe is not found, add the chromedriver.exe file to your visual studio solution and set 'Copy To Output Directory' to 'Copy always' in the properties window.

You need to navigate to a page before taking a screenshot - if default page in chrome is blank, then you will see the error

unknown error: cannot take screenshot
from unknown error: Cannot access contents of url "about:blank". Extension manifest must request permission to access this host.

If not, you might also run in to permission issues when saving files on the C:\ root, so save your screenshots in a sub directory.

The following code should work:

        IWebDriver driver = new ChromeDriver();
        driver.Navigate().GoToUrl("http://www.google.com");
        TakeScreenshot(driver, @"C:\devtest\screenshot.png");

PS: Always post the error details of any errors that you see to help us assist you better.

0
votes

Here's my solution. I had to create a new, explicit driver page and added the screenshot method in that page. Below is the method which I added:

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);
}