跳到正文
cccskills
免费在 GitHub 公开

testdriver:cache

Speed up tests with screenshot-based caching

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(1)

  • SKILL.md7.3 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

<!-- Generated from cache.mdx. DO NOT EDIT. -->

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.
flowchart LR
    A[Screenshot + cacheKey] --> B{Screen Diff\n< threshold?}
    B -- Yes --> C{Template\nMatch OK?}
    C -- Yes --> D[Return cached position]
    B -- No --> E[AI Call - fresh]
    C -- No --> F[AI Call - fresh]

Configuration

Constructor Options

const testdriver = new TestDriver({
  cache: {
    enabled: true,
    thresholds: {
      find: {
        screen: 0.05,     // 5% pixel diff allowed (default)
        element: 0.8,     // 80% OpenCV correlation required (default)
      },
      assert: 0.05,       // 5% pixel diff for assertions (default)
    },
  },
  cacheKey: 'my-custom-key',   // overrides auto-generated key
});
<ParamField path="cache" type="CacheConfig | false"> Cache configuration object, or `false` to disable entirely. <Expandable title="properties"> <ParamField path="enabled" type="boolean" default={true}> Enable or disable the cache system. Requires a valid `cacheKey` to actually activate. </ParamField>
<ParamField path="thresholds" type="CacheThresholds">
  Threshold configuration for different command types.

  <Expandable title="properties">
    <ParamField path="find" type="FindCacheThresholds">
      Thresholds for `find()` and `findAll()`.

      <Expandable title="properties">
        <ParamField path="screen" type="number" default={0.05}>
          Maximum pixel diff percentage allowed between the current screenshot and the cached screenshot. Lower values require a closer match. Range: `0` to `1`.
        </ParamField>
        
        <ParamField path="element" type="number" default={0.8}>
          Minimum OpenCV template matching correlation required for the cached element crop. Higher values require a closer match. Range: `0` to `1`. Only used for `find()`, not `findAll()`.
        </ParamField>
      </Expandable>
    </ParamField>
    
    <ParamField path="assert" type="number" default={0.05}>
      Maximum pixel diff allowed for assertion cache hits.
    </ParamField>
  </Expandable>
</ParamField>
</Expandable> </ParamField> <ParamField path="cacheKey" type="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. </ParamField>

Disabling Cache

// Via constructor
const testdriver = new TestDriver({ cache: false });

// Via environment variable
// TD_NO_CACHE=true npx vitest run

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:

// Stricter screen matching for this specific find
const el = await testdriver.find('submit button', {
  cache: {
    thresholds: { screen: 0.01, element: 0.95 },
  },
});

// Custom cache key for a specific assertion
await testdriver.assert('dashboard loaded', {
  cache: { threshold: 0.01 },
  cacheKey: 'dashboard-check',
});

Threshold Priority

Thresholds are resolved in priority order (highest wins):

PrioritySourceExample
1 (highest)Per-command optionfind(desc, { cache: { thresholds: { screen: 0.1 } } })
2Legacy number argumentfind(desc, 0.1)
3Global constructor confignew TestDriver({ cache: { thresholds: { find: { screen: 0.1 } } } })
4 (lowest)Hard-coded defaultsscreen: 0.05, element: 0.8, assert: 0.05

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
// Auto-generated cache key from test file hash
const testdriver = new TestDriver();
// cacheKey = "a3f2b1c4d5e6f7a8" (auto)

// Manual override
const testdriver = new TestDriver({ cacheKey: 'login-test-v2' });

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:

FieldDescription
cacheHittrue if cache was used
similarityPixel diff percentage between screenshots
cacheSimilarityOpenCV template match score

Use getDebugInfo() on an element to inspect cache results:

const el = await testdriver.find('submit button');
const debug = el.getDebugInfo();
console.log(debug);
// { cacheHit: true, similarity: 0.02, cacheSimilarity: 0.92, ... }

Types

interface CacheConfig {
  enabled?: boolean;            // Default: true
  thresholds?: CacheThresholds;
}

interface CacheThresholds {
  find?: FindCacheThresholds;
  assert?: number;              // Default: 0.05
}

interface FindCacheThresholds {
  screen?: number;              // Default: 0.05
  element?: number;             // Default: 0.8
}

interface CacheDebugInfo {
  cacheHit: boolean;
  similarity: number;
  cacheSimilarity: number;
}

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

How the TestDriver agent behaves on GitHub issues, pull requests, and @mentions

中文简介正在准备中。当前显示原文说明。

testdriverai/testdriverai4732026年9月21日 更新

Execute natural language tasks using AI

中文简介正在准备中。当前显示原文说明。

testdriverai/testdriverai4732026年9月21日 更新

Make AI-powered assertions about screen state

中文简介正在准备中。当前显示原文说明。

testdriverai/testdriverai4732026年9月21日 更新

Deploy TestDriver on your AWS infrastructure using CloudFormation

中文简介正在准备中。当前显示原文说明。

testdriverai/testdriverai4732026年9月21日 更新

How TestDriver learns your app and caches what it discovers for instant, deterministic replays

中文简介正在准备中。当前显示原文说明。

testdriverai/testdriverai4732026年9月21日 更新

1.7x faster test execution with intelligent caching and optimization

中文简介正在准备中。当前显示原文说明。

testdriverai/testdriverai4732026年9月21日 更新

查看 testdriverai 的全部技能

报告此技能的问题