Screenshots

Testery supports taking screenshots and having those screenshots attached to your test results. This works by placing screenshots in a screenshots folder in your current directory using the framework of your choice.

Testery highly recommends adding screenshots to your tests as this can often help you troubleshoot test failures that occur due to pages not loading quickly, elements changing, sites being down, etc.

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

In the SpecFlow example above, the after scenario hook will be executed right after a test either passes or fails if when the @takeScreenshot tag is included on a feature file. The ("screenshot") can be removed from the [AfterScenario] annotation and that will run the hook without having to specify the @screenshot tag.

Last updated