# 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.

{% tabs %}
{% tab title="SpecFlow" %}

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

{% endtab %}

{% tab title="webdriver.io" %}

```javascript
  let fs = require('fs');
  if (!fs.existsSync("./screenshots")){
    console.log("Screenshots dir being created.");
    fs.mkdirSync("./screenshots");
  } 
  browser.saveScreenshot("./screenshots/" + Math.random() + ".png");
```

{% endtab %}
{% endtabs %}

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.testery.io/get-to-know-testery/screenshots.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
