Taking screenshots
During your Automated Testing, you can take screenshots of the current webpage by issuing a "Take Screenshot" command.
Depending on which framework/language binding you are using, the syntax might be a little bit different.
If you want to automatically take a screenshot for each step in your test, you can use the custom screenshot TestingBot capability.
const fs = require('fs')
webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
return driver.takeScreenshot().then(function(data) {
fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
if(err) throw err
})
})
}
driver.saveScreenshot('screenshot.png')
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SeleniumTest
{
public class ScreenShotRemoteWebDriver : RemoteWebDriver, ITakesScreenshot
{
public ScreenShotRemoteWebDriver(Uri uri, OpenQA.Selenium.Chrome.ChromeOptions dc)
: base(uri, dc)
{
}
public new Screenshot GetScreenshot()
{
Response screenshotResponse = this.Execute(DriverCommand.Screenshot, null);
string base64 = screenshotResponse.Value.ToString();
return new Screenshot(base64);
}
}
class Program
{
static void Main(string[] args)
{
ScreenShotRemoteWebDriver driver;
OpenQA.Selenium.Chrome.ChromeOptions capability = new OpenQA.Selenium.Chrome.ChromeOptions();
capability.AddAdditionalCapability("browserName", "Chrome", true);
capability.AddAdditionalCapability("version", "latest", true);
capability.AddAdditionalCapability("platform", "WIN10", true);
capability.AddAdditionalCapability("key", "key", true);
capability.AddAdditionalCapability("secret", "secret", true);
capability.AddAdditionalCapability("chromeOptions", chromeOptions, true);
driver = new ScreenShotRemoteWebDriver(
new Uri("https://hub.testingbot.com/wd/hub/"), capability
);
driver.Navigate().GoToUrl("https://www.google.com/ncr");
Console.WriteLine(driver.Title);
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("TestingBot");
query.Submit();
Console.WriteLine(driver.Title);
ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
screenshot.SaveAsFile("/tmp/screenshot.png", OpenQA.Selenium.ScreenshotImageFormat.Png);
driver.Quit();
}
}
}