How to Capture Screenshots in Ocaramba: 4 Methods Explained

Ocaramba enables screenshot capture through explicit DriverContext.TakeScreenshot() calls for full-page images, TakeScreenShotOfElement() for specific elements, and automatic capture via Verify.That() when assertions fail.

The accenture/ocaramba framework provides a robust screenshot API that integrates directly with Selenium WebDriver to help debug test failures and document application state. Understanding how to capture screenshots in Ocaramba allows you to store visual evidence as PNG files with automatic timestamping and configurable output directories.

Manual Full-Page Screenshot Capture

The core screenshot workflow in Ocaramba involves two distinct steps: capturing the image and persisting it to disk. In OcarambaLite/DriverContext.cs (lines 602-608), the TakeScreenshot() method casts the underlying WebDriver to ITakesScreenshot and invokes GetScreenshot() to retrieve the raw image data.

// Capture the screenshot
var screenshot = this.DriverContext.TakeScreenshot();

// Wrap with metadata and save
var errorDetail = new ErrorDetail(screenshot, DateTime.Now, null);
string path = this.DriverContext.SaveScreenshot(
    errorDetail, 
    this.DriverContext.ScreenShotFolder, 
    this.DriverContext.TestTitle);

Console.WriteLine($"Saved to: {path}");

The SaveScreenshot method in OcarambaLite/DriverContextHelper.cs (lines 54-73) handles filename sanitization, directory creation, and optional TeamCity integration. It returns the full file path of the saved PNG.

Convenience Method: TakeAndSaveScreenshot

For most use cases, DriverContextHelper provides a one-line wrapper that combines capture and persistence. The TakeAndSaveScreenshot() method (lines 119-126) executes the full workflow and returns an array of generated file paths.

// Returns string[] of paths, or empty array if screenshots are disabled
string[] screenshotPaths = this.DriverContext.TakeAndSaveScreenshot();

This method respects the global configuration flags in BaseConfiguration, automatically checking SeleniumScreenShotEnabled before proceeding.

Element-Level Screenshot Capture

When you need to capture a specific UI component rather than the full viewport, use the static helper in OcarambaLite/Helpers/TakeScreenShot.cs (lines 50-58). The TakeScreenShotOfElement() method casts the IWebElement to ITakesScreenshot and saves only that component's visual area.

using OcarambaLite.Helpers;

IWebElement submitButton = this.Driver.GetElement(By.Id("submit"));
string folder = Path.Combine(
    Directory.GetCurrentDirectory(), 
    BaseConfiguration.ScreenShotFolder);

string filePath = TakeScreenShot.TakeScreenShotOfElement(
    submitButton, 
    folder, 
    "SubmitButton_State.png");

Automatic Screenshots on Verification Failure

Ocaramba's Verify.That() method in OcarambaLite/Verify.cs (lines 90-99) provides automatic screenshot capture when assertions fail. This ensures every verification error leaves visual evidence without explicit try-catch blocks in your test code.

Verify.That(
    this.DriverContext,
    () => Assert.IsTrue(this.Driver.Title.Contains("Dashboard")),
    enableScreenShot: true,
    enableSavePageSource: false);

If the assertion throws an exception, the method automatically calls driverContext.TakeAndSaveScreenshot() before re-throwing the error. Set enableScreenShot to false to skip capture for specific verifications.

Configuration Settings

Screenshot behavior is controlled through BaseConfiguration properties:

  • SeleniumScreenShotEnabled – Global toggle to enable or disable all screenshot capture
  • ScreenShotFolder – Relative or absolute path for output directory
  • TestTitle – Used as part of the filename to identify the test case

These settings ensure that screenshot capture can be disabled in CI environments or directed to specific artifact folders without code changes.

Summary

  • Explicit capture: Use DriverContext.TakeScreenshot() followed by SaveScreenshot() for full control over the process, as implemented in DriverContext.cs and DriverContextHelper.cs.
  • One-line capture: Call TakeAndSaveScreenshot() to execute the full workflow and receive file paths immediately.
  • Element targeting: Use TakeScreenShot.TakeScreenShotOfElement() from TakeScreenShot.cs to isolate specific UI components.
  • Automatic failure capture: Wrap assertions in Verify.That() to trigger screenshots automatically when tests fail, defined in Verify.cs.
  • Global control: Manage screenshot availability through BaseConfiguration flags without modifying test logic.

Frequently Asked Questions

How do I enable automatic screenshots for all failing tests in Ocaramba?

Enable the SeleniumScreenShotEnabled flag in your BaseConfiguration and wrap test assertions using Verify.That(). When an assertion fails, Verify.That() catches the exception, calls TakeAndSaveScreenshot(), and then re-throws the error, ensuring every failure generates a PNG in the configured ScreenShotFolder.

Can I capture screenshots of individual web elements instead of the full page?

Yes. Use the static method TakeScreenShot.TakeScreenShotOfElement() from OcarambaLite/Helpers/TakeScreenShot.cs. Pass the IWebElement, target directory, and filename. This casts the element to ITakesScreenshot and captures only that component's rendered area.

Where does Ocaramba save screenshots by default?

The framework saves screenshots to the path specified in BaseConfiguration.ScreenShotFolder, which defaults to a relative directory. The SaveScreenshot method in DriverContextHelper.cs creates the directory if it does not exist and generates filenames using the test title, timestamp, and a safe character replacement algorithm.

How do I disable screenshots for a specific verification while keeping them enabled globally?

When calling Verify.That(), set the enableScreenShot parameter to false. This overrides the global configuration for that specific assertion, preventing the automatic screenshot capture even if SeleniumScreenShotEnabled is true and the verification fails.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →