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?