Overview
The cache system speeds up repeated test runs by comparing screenshots to cached results. When the screen hasn’t changed significantly, cached element positions are reused instead of making an AI call. Cache works at two levels:- Screen cache — pixel diff comparison between the current screenshot and the cached screenshot
- Element cache — OpenCV template matching to verify the cached element position is still correct
How It Works
- On
find(), the SDK sends the current screenshot and cache metadata to the API - The API compares the screenshot against previously cached results for the same
cacheKey - If the screen pixel diff is within the
screenthreshold AND the element template match exceeds theelementthreshold, the cached position is returned - Otherwise, a new AI call is made and the result is cached
Configuration
Constructor Options
Cache configuration object, or
false to disable entirely.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
-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):| Priority | Source | Example |
|---|---|---|
| 1 (highest) | Per-command option | find(desc, { cache: { thresholds: { screen: 0.1 } } }) |
| 2 | Legacy number argument | find(desc, 0.1) |
| 3 | Global constructor config | new TestDriver({ cache: { thresholds: { find: { screen: 0.1 } } } }) |
| 4 (lowest) | Hard-coded defaults | screen: 0.05, element: 0.8, assert: 0.05 |
Auto-Generated Cache Key
When you don’t specify acacheKey, the SDK automatically generates one:
- Walks the call stack to find your test file
- Reads the file content
- Computes a SHA-256 hash of the content
- Uses the first 16 hex characters as the cache key
- 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:
- Load the cached element crop (needle) and current screenshot (haystack)
- Run
cv.matchTemplate()withTM_CCOEFF_NORMED - Binary threshold at the configured element threshold
- Find contours to extract match positions
- Return matches with
{ x, y, width, height, centerX, centerY }
[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 fileos— operating system (linux, windows, darwin)resolution— screen resolution
Debugging Cache
API responses include cache metadata:| Field | Description |
|---|---|
cacheHit | true if cache was used |
similarity | Pixel diff percentage between screenshots |
cacheSimilarity | OpenCV template match score |
getDebugInfo() on an element to inspect cache results:

