Screenshots
[AfterScenario("screenshot")]
public static void takeScreenshot()
{
Screenshot s = ((ITakesScreenshot)((IWebDriver)FeatureContext.Current["driver"])).GetScreenshot();
string fileName = ScenarioContext.Current.ScenarioInfo.Title.Replace(" ","") + DateTime.Now.ToString("_MMddyyyyHHmmss") + ".png";
try
{
string screenshotDir = ScenarioContext.Current.ScenarioContainer.Resolve<TestContext>().WorkDirectory.ToString() + @"\screenshots";
if (!Directory.Exists(screenshotDir))
{
Directory.CreateDirectory(screenshotDir);
}
s.SaveAsFile(screenshotDir + @"\" + fileName, ScreenshotImageFormat.Png);
TestContext.Out.WriteLine($"-> info: added screenshot ({fileName}) to dir ({screenshotDir})");; }
catch(Exception e)
{
TestContext.Out.WriteLine($"-> error: ({e})");
}
} let fs = require('fs');
if (!fs.existsSync("./screenshots")){
console.log("Screenshots dir being created.");
fs.mkdirSync("./screenshots");
}
browser.saveScreenshot("./screenshots/" + Math.random() + ".png");Last updated
Was this helpful?