Skip to main content

Overview

The cache system makes repeated test runs faster. It compares screenshots to cached results. When the screen did not change much, TestDriver uses the cached element positions again. It does not make an AI call. The cache works at two levels:
  • Screen cache: a pixel diff comparison between the present screenshot and the cached screenshot
  • Element cache: OpenCV template matching to make sure that the cached element position is still correct

How It Works

  1. On find(), the SDK sends the present screenshot and the cache metadata to the API.
  2. The API compares the screenshot against the cached results for the same cacheKey.
  3. If the screen pixel diff is in the screen threshold AND the element template match is more than the element threshold, TestDriver returns the cached position.
  4. If not, TestDriver makes a new AI call and caches the result.

Configuration

Constructor Options

CacheConfig | false
Cache configuration object, or false to disable entirely.
string
Unique key for cache lookups. If not provided, an auto-generated key is created from a SHA-256 hash of the calling test file (first 16 hex characters). The cache key changes automatically when your test file changes, providing automatic cache invalidation.

Disabling Cache

When cache is disabled, all thresholds are set to -1 internally, causing the API to skip cache lookups.

Per-Command Overrides

Override cache thresholds for individual commands:

Threshold Priority

Thresholds are resolved in priority order (highest wins):

Auto-Generated Cache Key

When you don’t specify a cacheKey, the SDK automatically generates one:
  1. Walks the call stack to find your test file
  2. Reads the file content
  3. Computes a SHA-256 hash of the content
  4. Uses the first 16 hex characters as the cache key
This means:
  • Same test file → same cache key → cache hits
  • Modified test file → different hash → automatic cache invalidation
  • Different test files → different keys → isolated caches

Template Matching (OpenCV)

Element cache validation uses OpenCV’s normalized cross-correlation coefficient (TM_CCOEFF_NORMED) to verify that the cached element is still visible at the expected position. Algorithm:
  1. Load the cached element crop (needle) and current screenshot (haystack)
  2. Run cv.matchTemplate() with TM_CCOEFF_NORMED
  3. Binary threshold at the configured element threshold
  4. Find contours to extract match positions
  5. Return matches with { x, y, width, height, centerX, centerY }
Scale factors tried: [1, 0.5, 2, 0.75, 1.25, 1.5] Thresholds tried: [0.9, 0.8, 0.7] (picks highest matching threshold) This accounts for minor scaling differences between screenshots taken at different times or resolutions.

Cache Partitioning

Cache entries are partitioned by:
  • cacheKey — identifies the test file
  • os — operating system (linux, windows, darwin)
  • resolution — screen resolution
This means cache from a Linux run won’t be used for a Windows run, even with the same cache key.

Debugging Cache

API responses include cache metadata: Use getDebugInfo() on an element to inspect cache results:

Types