testdriverai 7.8.0-test.6 → 7.8.0-test.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 7.8.0-test.7 (2026-03-13)
2
+
3
+ 🔧 Maintenance
4
+
5
+ - Fix version bump syntax in release workflow [f3ac61dc]
6
+
1
7
  ## 7.8.0-test.6 (2026-03-13)
2
8
 
3
9
  🔧 Maintenance
@@ -0,0 +1,221 @@
1
+ ---
2
+ name: testdriver:cache
3
+ description: Speed up tests with screenshot-based caching
4
+ ---
5
+ <!-- Generated from cache.mdx. DO NOT EDIT. -->
6
+
7
+ ## Overview
8
+
9
+ 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.
10
+
11
+ Cache works at two levels:
12
+ - **Screen cache** — pixel diff comparison between the current screenshot and the cached screenshot
13
+ - **Element cache** — OpenCV template matching to verify the cached element position is still correct
14
+
15
+ ## How It Works
16
+
17
+ 1. On `find()`, the SDK sends the current screenshot and cache metadata to the API
18
+ 2. The API compares the screenshot against previously cached results for the same `cacheKey`
19
+ 3. If the screen pixel diff is within the `screen` threshold AND the element template match exceeds the `element` threshold, the cached position is returned
20
+ 4. Otherwise, a new AI call is made and the result is cached
21
+
22
+ ```mermaid
23
+ flowchart LR
24
+ A[Screenshot + cacheKey] --> B{Screen Diff\n< threshold?}
25
+ B -- Yes --> C{Template\nMatch OK?}
26
+ C -- Yes --> D[Return cached position]
27
+ B -- No --> E[AI Call - fresh]
28
+ C -- No --> F[AI Call - fresh]
29
+ ```
30
+
31
+ ## Configuration
32
+
33
+ ### Constructor Options
34
+
35
+ ```javascript
36
+ const testdriver = new TestDriver({
37
+ cache: {
38
+ enabled: true,
39
+ thresholds: {
40
+ find: {
41
+ screen: 0.05, // 5% pixel diff allowed (default)
42
+ element: 0.8, // 80% OpenCV correlation required (default)
43
+ },
44
+ assert: 0.05, // 5% pixel diff for assertions (default)
45
+ },
46
+ },
47
+ cacheKey: 'my-custom-key', // overrides auto-generated key
48
+ });
49
+ ```
50
+
51
+ <ParamField path="cache" type="CacheConfig | false">
52
+ Cache configuration object, or `false` to disable entirely.
53
+
54
+ <Expandable title="properties">
55
+ <ParamField path="enabled" type="boolean" default={true}>
56
+ Enable or disable the cache system. Requires a valid `cacheKey` to actually activate.
57
+ </ParamField>
58
+
59
+ <ParamField path="thresholds" type="CacheThresholds">
60
+ Threshold configuration for different command types.
61
+
62
+ <Expandable title="properties">
63
+ <ParamField path="find" type="FindCacheThresholds">
64
+ Thresholds for `find()` and `findAll()`.
65
+
66
+ <Expandable title="properties">
67
+ <ParamField path="screen" type="number" default={0.05}>
68
+ Maximum pixel diff percentage allowed between the current screenshot and the cached screenshot. Lower values require a closer match. Range: `0` to `1`.
69
+ </ParamField>
70
+
71
+ <ParamField path="element" type="number" default={0.8}>
72
+ 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()`.
73
+ </ParamField>
74
+ </Expandable>
75
+ </ParamField>
76
+
77
+ <ParamField path="assert" type="number" default={0.05}>
78
+ Maximum pixel diff allowed for assertion cache hits.
79
+ </ParamField>
80
+ </Expandable>
81
+ </ParamField>
82
+ </Expandable>
83
+ </ParamField>
84
+
85
+ <ParamField path="cacheKey" type="string">
86
+ 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.
87
+ </ParamField>
88
+
89
+ ### Disabling Cache
90
+
91
+ ```javascript
92
+ // Via constructor
93
+ const testdriver = new TestDriver({ cache: false });
94
+
95
+ // Via environment variable
96
+ // TD_NO_CACHE=true npx vitest run
97
+ ```
98
+
99
+ When cache is disabled, all thresholds are set to `-1` internally, causing the API to skip cache lookups.
100
+
101
+ ### Per-Command Overrides
102
+
103
+ Override cache thresholds for individual commands:
104
+
105
+ ```javascript
106
+ // Stricter screen matching for this specific find
107
+ const el = await testdriver.find('submit button', {
108
+ cache: {
109
+ thresholds: { screen: 0.01, element: 0.95 },
110
+ },
111
+ });
112
+
113
+ // Custom cache key for a specific assertion
114
+ await testdriver.assert('dashboard loaded', {
115
+ cache: { threshold: 0.01 },
116
+ cacheKey: 'dashboard-check',
117
+ });
118
+ ```
119
+
120
+ ## Threshold Priority
121
+
122
+ Thresholds are resolved in priority order (highest wins):
123
+
124
+ | Priority | Source | Example |
125
+ |----------|--------|---------|
126
+ | 1 (highest) | Per-command option | `find(desc, { cache: { thresholds: { screen: 0.1 } } })` |
127
+ | 2 | Legacy number argument | `find(desc, 0.1)` |
128
+ | 3 | Global constructor config | `new TestDriver({ cache: { thresholds: { find: { screen: 0.1 } } } })` |
129
+ | 4 (lowest) | Hard-coded defaults | `screen: 0.05`, `element: 0.8`, `assert: 0.05` |
130
+
131
+ ## Auto-Generated Cache Key
132
+
133
+ When you don't specify a `cacheKey`, the SDK automatically generates one:
134
+
135
+ 1. Walks the call stack to find your test file
136
+ 2. Reads the file content
137
+ 3. Computes a **SHA-256 hash** of the content
138
+ 4. Uses the **first 16 hex characters** as the cache key
139
+
140
+ This means:
141
+ - **Same test file** → same cache key → cache hits
142
+ - **Modified test file** → different hash → automatic cache invalidation
143
+ - **Different test files** → different keys → isolated caches
144
+
145
+ ```javascript
146
+ // Auto-generated cache key from test file hash
147
+ const testdriver = new TestDriver();
148
+ // cacheKey = "a3f2b1c4d5e6f7a8" (auto)
149
+
150
+ // Manual override
151
+ const testdriver = new TestDriver({ cacheKey: 'login-test-v2' });
152
+ ```
153
+
154
+ ## Template Matching (OpenCV)
155
+
156
+ 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.
157
+
158
+ **Algorithm:**
159
+ 1. Load the cached element crop (needle) and current screenshot (haystack)
160
+ 2. Run `cv.matchTemplate()` with `TM_CCOEFF_NORMED`
161
+ 3. Binary threshold at the configured element threshold
162
+ 4. Find contours to extract match positions
163
+ 5. Return matches with `{ x, y, width, height, centerX, centerY }`
164
+
165
+ **Scale factors tried:** `[1, 0.5, 2, 0.75, 1.25, 1.5]`
166
+ **Thresholds tried:** `[0.9, 0.8, 0.7]` (picks highest matching threshold)
167
+
168
+ This accounts for minor scaling differences between screenshots taken at different times or resolutions.
169
+
170
+ ## Cache Partitioning
171
+
172
+ Cache entries are partitioned by:
173
+ - **`cacheKey`** — identifies the test file
174
+ - **`os`** — operating system (linux, windows, darwin)
175
+ - **`resolution`** — screen resolution
176
+
177
+ This means cache from a Linux run won't be used for a Windows run, even with the same cache key.
178
+
179
+ ## Debugging Cache
180
+
181
+ API responses include cache metadata:
182
+
183
+ | Field | Description |
184
+ |---|---|
185
+ | `cacheHit` | `true` if cache was used |
186
+ | `similarity` | Pixel diff percentage between screenshots |
187
+ | `cacheSimilarity` | OpenCV template match score |
188
+
189
+ Use `getDebugInfo()` on an element to inspect cache results:
190
+
191
+ ```javascript
192
+ const el = await testdriver.find('submit button');
193
+ const debug = el.getDebugInfo();
194
+ console.log(debug);
195
+ // { cacheHit: true, similarity: 0.02, cacheSimilarity: 0.92, ... }
196
+ ```
197
+
198
+ ## Types
199
+
200
+ ```typescript
201
+ interface CacheConfig {
202
+ enabled?: boolean; // Default: true
203
+ thresholds?: CacheThresholds;
204
+ }
205
+
206
+ interface CacheThresholds {
207
+ find?: FindCacheThresholds;
208
+ assert?: number; // Default: 0.05
209
+ }
210
+
211
+ interface FindCacheThresholds {
212
+ screen?: number; // Default: 0.05
213
+ element?: number; // Default: 0.8
214
+ }
215
+
216
+ interface CacheDebugInfo {
217
+ cacheHit: boolean;
218
+ similarity: number;
219
+ cacheSimilarity: number;
220
+ }
221
+ ```
@@ -0,0 +1,246 @@
1
+ ---
2
+ name: testdriver:errors
3
+ description: Custom error classes and error handling
4
+ ---
5
+ <!-- Generated from errors.mdx. DO NOT EDIT. -->
6
+
7
+ ## Overview
8
+
9
+ TestDriver provides custom error classes with rich debugging information. These are exported from the SDK and can be used for `instanceof` checks in your tests.
10
+
11
+ ```javascript
12
+ import TestDriver, { ElementNotFoundError, AIError } from 'testdriverai';
13
+ ```
14
+
15
+ ## ElementNotFoundError
16
+
17
+ Thrown when `find()` cannot locate an element on screen, or when calling `click()`/`hover()` on an unfound element.
18
+
19
+ ```javascript
20
+ try {
21
+ await testdriver.find('nonexistent button').click();
22
+ } catch (error) {
23
+ if (error instanceof ElementNotFoundError) {
24
+ console.log(error.description); // "nonexistent button"
25
+ console.log(error.screenshotPath); // path to debug screenshot
26
+ console.log(error.pixelDiffPath); // path to pixel diff image
27
+ }
28
+ }
29
+ ```
30
+
31
+ ### Properties
32
+
33
+ <ParamField path="name" type="string">
34
+ Always `"ElementNotFoundError"`.
35
+ </ParamField>
36
+
37
+ <ParamField path="message" type="string">
38
+ Enhanced message with a debug block containing element description, cache status, similarity scores, and AI response details.
39
+ </ParamField>
40
+
41
+ <ParamField path="description" type="string">
42
+ The original element description passed to `find()`.
43
+ </ParamField>
44
+
45
+ <ParamField path="screenshotPath" type="string | null">
46
+ Absolute path to a debug screenshot saved at the time of failure. Written to `<os.tmpdir>/testdriver-debug/screenshot-<timestamp>.png`.
47
+ </ParamField>
48
+
49
+ <ParamField path="pixelDiffPath" type="string | null">
50
+ Absolute path to a pixel diff image showing the comparison between the cached and current screenshots. Written to `<os.tmpdir>/testdriver-debug/pixel-diff-error-<timestamp>.png`. Only present when cache was involved.
51
+ </ParamField>
52
+
53
+ <ParamField path="cachedImagePath" type="string | null">
54
+ URL to the cached image that was compared against.
55
+ </ParamField>
56
+
57
+ <ParamField path="aiResponse" type="object | null">
58
+ Sanitized AI response object. Large binary fields (`croppedImage`, `screenshot`, `pixelDiffImage`) are removed. Contains cache metadata like `similarity`, `cacheHit`, `cacheStrategy`, `cacheDiffPercent`, and `threshold`.
59
+ </ParamField>
60
+
61
+ <ParamField path="timestamp" type="string">
62
+ ISO 8601 timestamp of when the error was created.
63
+ </ParamField>
64
+
65
+ ### Enhanced Message
66
+
67
+ The error message is automatically enhanced with debugging information:
68
+
69
+ ```
70
+ Element not found: "submit button"
71
+
72
+ === Element Debug Info ===
73
+ Element: submit button
74
+ Cache Hit: false
75
+ Similarity: 0.23
76
+ Cache Strategy: pixel-diff
77
+ Threshold: 0.05
78
+ AI Response Element: null
79
+ ```
80
+
81
+ ### Stack Trace Cleaning
82
+
83
+ Stack traces are automatically cleaned to remove internal SDK frames (`Element.*`, `sdk.js` internals), showing only your test code for easier debugging.
84
+
85
+ ## AIError
86
+
87
+ Thrown when `act()` exhausts all retry attempts.
88
+
89
+ ```javascript
90
+ try {
91
+ await testdriver.act('perform complex workflow', { tries: 3 });
92
+ } catch (error) {
93
+ if (error instanceof AIError) {
94
+ console.log(error.task); // "perform complex workflow"
95
+ console.log(error.tries); // 3
96
+ console.log(error.duration); // 15234 (ms)
97
+ console.log(error.cause); // underlying Error
98
+ }
99
+ }
100
+ ```
101
+
102
+ ### Properties
103
+
104
+ <ParamField path="name" type="string">
105
+ Always `"AIError"`.
106
+ </ParamField>
107
+
108
+ <ParamField path="message" type="string">
109
+ Enhanced message with execution details block.
110
+ </ParamField>
111
+
112
+ <ParamField path="task" type="string">
113
+ The task description passed to `act()`.
114
+ </ParamField>
115
+
116
+ <ParamField path="tries" type="number">
117
+ Number of attempts that were made.
118
+ </ParamField>
119
+
120
+ <ParamField path="maxTries" type="number">
121
+ Maximum number of attempts configured.
122
+ </ParamField>
123
+
124
+ <ParamField path="duration" type="number">
125
+ Total execution time in milliseconds.
126
+ </ParamField>
127
+
128
+ <ParamField path="cause" type="Error | undefined">
129
+ The underlying error that caused the final failure.
130
+ </ParamField>
131
+
132
+ <ParamField path="timestamp" type="string">
133
+ ISO 8601 timestamp of when the error was created.
134
+ </ParamField>
135
+
136
+ ### Enhanced Message
137
+
138
+ ```
139
+ AI failed: Element not found after 3 attempts
140
+
141
+ === AI Execution Details ===
142
+ Task: perform complex workflow
143
+ Tries: 3 / 3
144
+ Duration: 15234ms
145
+ Cause: ElementNotFoundError: Element not found: "submit button"
146
+ ```
147
+
148
+ ## Internal Error Classes
149
+
150
+ These errors are used internally by the agent and are not exported, but may appear as the `cause` of an `AIError`:
151
+
152
+ ### MatchError
153
+
154
+ Thrown when element matching fails (text, image, or assertion).
155
+
156
+ | Property | Type | Description |
157
+ |---|---|---|
158
+ | `fatal` | `boolean` | If `true`, cannot be healed. Default: `false` |
159
+ | `attachScreenshot` | `boolean` | Always `true` — a screenshot is attached to the error |
160
+
161
+ ### CommandError
162
+
163
+ Thrown for invalid arguments or unsupported operations.
164
+
165
+ | Property | Type | Description |
166
+ |---|---|---|
167
+ | `fatal` | `boolean` | Always `true` |
168
+ | `attachScreenshot` | `boolean` | Always `false` |
169
+
170
+ ## Soft Assert Mode
171
+
172
+ Inside `act()`, assertions run in **soft assert mode**. When an assertion fails, it returns the failure result instead of throwing, allowing the AI to process the failure and adjust its approach.
173
+
174
+ ```javascript
175
+ // Inside act(), assertion failures don't throw
176
+ await testdriver.act('verify the dashboard shows correct data', {
177
+ tries: 3,
178
+ });
179
+ // The AI can see assertion results and self-correct
180
+ ```
181
+
182
+ This is automatic — you don't need to configure it. Regular `assert()` calls outside of `act()` will throw normally on failure.
183
+
184
+ ## Error Handling Patterns
185
+
186
+ ### Catching Specific Errors
187
+
188
+ ```javascript
189
+ import TestDriver, { ElementNotFoundError, AIError } from 'testdriverai';
190
+
191
+ try {
192
+ await testdriver.find('submit button').click();
193
+ } catch (error) {
194
+ if (error instanceof ElementNotFoundError) {
195
+ // Element wasn't found — check screenshot for debugging
196
+ console.log('Debug screenshot:', error.screenshotPath);
197
+ } else if (error instanceof AIError) {
198
+ // AI exhausted retries
199
+ console.log(`Failed after ${error.tries} tries in ${error.duration}ms`);
200
+ } else {
201
+ throw error; // Unexpected error
202
+ }
203
+ }
204
+ ```
205
+
206
+ ### Using Debug Screenshots
207
+
208
+ ```javascript
209
+ try {
210
+ const el = await testdriver.find('checkout button');
211
+ await el.click();
212
+ } catch (error) {
213
+ if (error instanceof ElementNotFoundError) {
214
+ // Screenshot of what the screen looked like
215
+ console.log('Screen:', error.screenshotPath);
216
+ // Pixel diff showing cache comparison
217
+ console.log('Diff:', error.pixelDiffPath);
218
+ // Full AI response metadata
219
+ console.log('AI:', JSON.stringify(error.aiResponse, null, 2));
220
+ }
221
+ }
222
+ ```
223
+
224
+ ## Types
225
+
226
+ ```typescript
227
+ class ElementNotFoundError extends Error {
228
+ name: 'ElementNotFoundError';
229
+ description: string;
230
+ screenshotPath: string | null;
231
+ pixelDiffPath: string | null;
232
+ cachedImagePath: string | null;
233
+ aiResponse: Record<string, any> | null;
234
+ timestamp: string;
235
+ }
236
+
237
+ class AIError extends Error {
238
+ name: 'AIError';
239
+ task: string;
240
+ tries: number;
241
+ maxTries: number;
242
+ duration: number;
243
+ cause?: Error;
244
+ timestamp: string;
245
+ }
246
+ ```