testdriverai 7.3.12 → 7.3.13

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.
Files changed (133) hide show
  1. package/.github/skills/testdriver:ai/SKILL.md +204 -0
  2. package/.github/skills/testdriver:assert/SKILL.md +284 -0
  3. package/.github/skills/testdriver:aws-setup/SKILL.md +515 -0
  4. package/.github/skills/testdriver:caching/SKILL.md +124 -0
  5. package/.github/skills/testdriver:captcha/SKILL.md +159 -0
  6. package/.github/skills/testdriver:ci-cd/SKILL.md +602 -0
  7. package/.github/skills/testdriver:click/SKILL.md +286 -0
  8. package/.github/skills/testdriver:client/SKILL.md +339 -0
  9. package/.github/skills/testdriver:cloud/SKILL.md +119 -0
  10. package/.github/skills/testdriver:customizing-devices/SKILL.md +153 -0
  11. package/.github/skills/testdriver:dashcam/SKILL.md +418 -0
  12. package/.github/skills/testdriver:debugging-with-screenshots/SKILL.md +271 -0
  13. package/.github/skills/testdriver:device-config/SKILL.md +317 -0
  14. package/.github/skills/testdriver:double-click/SKILL.md +102 -0
  15. package/.github/skills/testdriver:elements/SKILL.md +605 -0
  16. package/.github/skills/testdriver:enterprise/SKILL.md +114 -0
  17. package/.github/skills/testdriver:examples/SKILL.md +7 -0
  18. package/.github/skills/testdriver:exec/SKILL.md +345 -0
  19. package/.github/skills/testdriver:find/SKILL.md +721 -0
  20. package/.github/skills/testdriver:focus-application/SKILL.md +293 -0
  21. package/.github/skills/testdriver:generating-tests/SKILL.md +36 -0
  22. package/.github/skills/testdriver:hover/SKILL.md +278 -0
  23. package/.github/skills/testdriver:locating-elements/SKILL.md +71 -0
  24. package/.github/skills/testdriver:making-assertions/SKILL.md +32 -0
  25. package/.github/skills/testdriver:mcp-workflow/SKILL.md +410 -0
  26. package/.github/skills/testdriver:mouse-down/SKILL.md +161 -0
  27. package/.github/skills/testdriver:mouse-up/SKILL.md +164 -0
  28. package/.github/skills/testdriver:performing-actions/SKILL.md +51 -0
  29. package/.github/skills/testdriver:press-keys/SKILL.md +348 -0
  30. package/.github/skills/testdriver:quickstart/SKILL.md +161 -0
  31. package/.github/skills/testdriver:reusable-code/SKILL.md +240 -0
  32. package/.github/skills/testdriver:right-click/SKILL.md +123 -0
  33. package/.github/skills/testdriver:running-tests/SKILL.md +181 -0
  34. package/.github/skills/testdriver:screenshot/SKILL.md +167 -0
  35. package/.github/skills/testdriver:scroll/SKILL.md +299 -0
  36. package/.github/skills/testdriver:secrets/SKILL.md +115 -0
  37. package/.github/skills/testdriver:self-hosted/SKILL.md +65 -0
  38. package/.github/skills/testdriver:test-writer/SKILL.md +451 -0
  39. package/.github/skills/testdriver:testdriver/SKILL.md +523 -0
  40. package/.github/skills/testdriver:testdriver-mechanic/SKILL.md +165 -0
  41. package/.github/skills/testdriver:type/SKILL.md +357 -0
  42. package/.github/skills/testdriver:variables/SKILL.md +111 -0
  43. package/.github/skills/testdriver:waiting-for-elements/SKILL.md +66 -0
  44. package/.github/skills/testdriver:what-is-testdriver/SKILL.md +54 -0
  45. package/.github/workflows/acceptance-windows-scheduled.yaml +6 -1
  46. package/.github/workflows/acceptance.yaml +0 -36
  47. package/.github/workflows/update-examples.yaml +53 -0
  48. package/CHANGELOG.md +4 -0
  49. package/agent/events.js +1 -0
  50. package/agent/index.js +8 -0
  51. package/agent/lib/commands.js +48 -29
  52. package/agent/lib/redraw.js +3 -1
  53. package/agent/lib/sandbox.js +166 -14
  54. package/agent/lib/sdk.js +142 -3
  55. package/agent/lib/system.js +4 -6
  56. package/ai/skills/testdriver:ai/SKILL.md +204 -0
  57. package/ai/skills/testdriver:assert/SKILL.md +315 -0
  58. package/ai/skills/testdriver:aws-setup/SKILL.md +448 -0
  59. package/ai/skills/testdriver:caching/SKILL.md +124 -0
  60. package/ai/skills/testdriver:captcha/SKILL.md +159 -0
  61. package/ai/skills/testdriver:ci-cd/SKILL.md +602 -0
  62. package/ai/skills/testdriver:click/SKILL.md +286 -0
  63. package/ai/skills/testdriver:client/SKILL.md +372 -0
  64. package/ai/skills/testdriver:cloud/SKILL.md +119 -0
  65. package/ai/skills/testdriver:customizing-devices/SKILL.md +153 -0
  66. package/ai/skills/testdriver:dashcam/SKILL.md +418 -0
  67. package/ai/skills/testdriver:debugging-with-screenshots/SKILL.md +401 -0
  68. package/ai/skills/testdriver:device-config/SKILL.md +317 -0
  69. package/ai/skills/testdriver:double-click/SKILL.md +102 -0
  70. package/ai/skills/testdriver:elements/SKILL.md +605 -0
  71. package/ai/skills/testdriver:enterprise/SKILL.md +114 -0
  72. package/ai/skills/testdriver:examples/SKILL.md +7 -0
  73. package/ai/skills/testdriver:exec/SKILL.md +345 -0
  74. package/ai/skills/testdriver:find/SKILL.md +745 -0
  75. package/ai/skills/testdriver:focus-application/SKILL.md +293 -0
  76. package/ai/skills/testdriver:generating-tests/SKILL.md +36 -0
  77. package/ai/skills/testdriver:hover/SKILL.md +278 -0
  78. package/ai/skills/testdriver:locating-elements/SKILL.md +71 -0
  79. package/ai/skills/testdriver:making-assertions/SKILL.md +32 -0
  80. package/ai/skills/testdriver:mcp-workflow/SKILL.md +410 -0
  81. package/ai/skills/testdriver:mouse-down/SKILL.md +161 -0
  82. package/ai/skills/testdriver:mouse-up/SKILL.md +164 -0
  83. package/ai/skills/testdriver:ocr/SKILL.md +235 -0
  84. package/ai/skills/testdriver:performing-actions/SKILL.md +51 -0
  85. package/ai/skills/testdriver:press-keys/SKILL.md +348 -0
  86. package/ai/skills/testdriver:quickstart/SKILL.md +146 -0
  87. package/ai/skills/testdriver:reusable-code/SKILL.md +240 -0
  88. package/ai/skills/testdriver:right-click/SKILL.md +123 -0
  89. package/ai/skills/testdriver:running-tests/SKILL.md +185 -0
  90. package/ai/skills/testdriver:screenshot/SKILL.md +248 -0
  91. package/ai/skills/testdriver:scroll/SKILL.md +335 -0
  92. package/ai/skills/testdriver:secrets/SKILL.md +115 -0
  93. package/ai/skills/testdriver:self-hosted/SKILL.md +65 -0
  94. package/ai/skills/testdriver:test-writer/SKILL.md +451 -0
  95. package/ai/skills/testdriver:testdriver/SKILL.md +631 -0
  96. package/ai/skills/testdriver:testdriver-mechanic/SKILL.md +165 -0
  97. package/ai/skills/testdriver:type/SKILL.md +357 -0
  98. package/ai/skills/testdriver:variables/SKILL.md +111 -0
  99. package/ai/skills/testdriver:waiting-for-elements/SKILL.md +66 -0
  100. package/ai/skills/testdriver:what-is-testdriver/SKILL.md +54 -0
  101. package/debugger/index.html +12 -2
  102. package/docs/v7/examples/scroll-keyboard.mdx +1 -1
  103. package/docs/v7/find.mdx +1 -0
  104. package/examples/config.mjs +1 -1
  105. package/examples/findall-coffee-icons.test.mjs +42 -0
  106. package/examples/flake-diffthreshold-001.test.mjs +9 -0
  107. package/examples/flake-diffthreshold-01.test.mjs +9 -0
  108. package/examples/flake-diffthreshold-05.test.mjs +9 -0
  109. package/examples/{z_flake-noredraw-cache.test.mjs → flake-noredraw-cache.test.mjs} +2 -2
  110. package/examples/{z_flake-noredraw-nocache.test.mjs → flake-noredraw-nocache.test.mjs} +2 -2
  111. package/examples/{z_flake-redraw-cache.test.mjs → flake-redraw-cache.test.mjs} +2 -2
  112. package/examples/{z_flake-redraw-nocache.test.mjs → flake-redraw-nocache.test.mjs} +2 -2
  113. package/examples/flake-rocket-match.test.mjs +30 -0
  114. package/examples/{z_flake-shared.mjs → flake-shared.mjs} +2 -2
  115. package/examples/parse.test.mjs +19 -0
  116. package/examples/scroll-keyboard.test.mjs +1 -1
  117. package/interfaces/cli/lib/base.js +6 -0
  118. package/interfaces/logger.js +51 -13
  119. package/interfaces/vitest-plugin.mjs +137 -0
  120. package/lib/core/index.d.ts +22 -0
  121. package/lib/init-project.js +105 -6
  122. package/lib/vitest/hooks.mjs +2 -5
  123. package/lib/vitest/setup-disable-defender.mjs +52 -0
  124. package/package.json +2 -1
  125. package/sdk-log-formatter.js +90 -0
  126. package/sdk.d.ts +88 -51
  127. package/sdk.js +126 -18
  128. package/setup/aws/disable-defender.sh +42 -0
  129. package/vitest.config.mjs +1 -3
  130. package/examples/z_flake-diffthreshold-001.test.mjs +0 -9
  131. package/examples/z_flake-diffthreshold-01.test.mjs +0 -9
  132. package/examples/z_flake-diffthreshold-05.test.mjs +0 -9
  133. /package/{examples → manual}/captcha-api.test.mjs +0 -0
package/agent/lib/sdk.js CHANGED
@@ -5,6 +5,115 @@ const crypto = require("crypto");
5
5
  const { version } = require("../../package.json");
6
6
  const axios = require("axios");
7
7
 
8
+ /**
9
+ * Default retry configuration
10
+ */
11
+ const DEFAULT_RETRY_CONFIG = {
12
+ maxRetries: 10,
13
+ baseDelayMs: 3000,
14
+ maxDelayMs: 30000,
15
+ // Error codes that should trigger a retry
16
+ retryableNetworkCodes: [
17
+ 'ECONNRESET',
18
+ 'ECONNREFUSED',
19
+ 'ETIMEDOUT',
20
+ 'ENOTFOUND',
21
+ 'ENETUNREACH',
22
+ 'ERR_NETWORK',
23
+ 'ECONNABORTED',
24
+ 'EPIPE',
25
+ 'EAI_AGAIN',
26
+ ],
27
+ // HTTP status codes that should trigger a retry
28
+ retryableStatusCodes: [429, 500, 502, 503, 504],
29
+ };
30
+
31
+ /**
32
+ * Determines if an error is retryable
33
+ * @param {Error} error - The axios error
34
+ * @param {Object} config - Retry configuration
35
+ * @returns {boolean} Whether the request should be retried
36
+ */
37
+ function isRetryableError(error, config = DEFAULT_RETRY_CONFIG) {
38
+ // Network-level errors (no response received)
39
+ if (!error.response) {
40
+ return config.retryableNetworkCodes.includes(error.code);
41
+ }
42
+
43
+ // HTTP status code based retries
44
+ const status = error.response?.status;
45
+ return config.retryableStatusCodes.includes(status);
46
+ }
47
+
48
+ /**
49
+ * Calculate delay for next retry using exponential backoff with jitter
50
+ * @param {number} attempt - Current attempt number (0-indexed)
51
+ * @param {Error} error - The error that triggered the retry
52
+ * @param {Object} config - Retry configuration
53
+ * @returns {number} Delay in milliseconds
54
+ */
55
+ function calculateRetryDelay(attempt, error, config = DEFAULT_RETRY_CONFIG) {
56
+ // Respect Retry-After header for rate limiting
57
+ if (error.response?.status === 429) {
58
+ const retryAfter = error.response.headers?.['retry-after'];
59
+ if (retryAfter) {
60
+ const retryAfterMs = parseInt(retryAfter, 10) * 1000;
61
+ if (!isNaN(retryAfterMs) && retryAfterMs > 0) {
62
+ return Math.min(retryAfterMs, config.maxDelayMs);
63
+ }
64
+ }
65
+ }
66
+
67
+ // Exponential backoff: baseDelay * 2^attempt + random jitter
68
+ const exponentialDelay = config.baseDelayMs * Math.pow(2, attempt);
69
+ const jitter = Math.random() * config.baseDelayMs * 0.5;
70
+ return Math.min(exponentialDelay + jitter, config.maxDelayMs);
71
+ }
72
+
73
+ /**
74
+ * Sleep for a specified duration
75
+ * @param {number} ms - Milliseconds to sleep
76
+ * @returns {Promise<void>}
77
+ */
78
+ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
79
+
80
+ /**
81
+ * Execute an async function with retry logic
82
+ * @param {Function} fn - Async function to execute
83
+ * @param {Object} options - Options
84
+ * @param {Object} options.retryConfig - Retry configuration (uses defaults if not provided)
85
+ * @param {Function} options.onRetry - Callback called before each retry (attempt, error, delayMs)
86
+ * @returns {Promise<*>} Result of the function
87
+ */
88
+ async function withRetry(fn, options = {}) {
89
+ const config = { ...DEFAULT_RETRY_CONFIG, ...options.retryConfig };
90
+ let lastError;
91
+
92
+ for (let attempt = 0; attempt <= config.maxRetries; attempt++) {
93
+ try {
94
+ return await fn();
95
+ } catch (error) {
96
+ lastError = error;
97
+
98
+ // Don't retry if we've exhausted attempts or error isn't retryable
99
+ if (attempt >= config.maxRetries || !isRetryableError(error, config)) {
100
+ throw error;
101
+ }
102
+
103
+ const delayMs = calculateRetryDelay(attempt, error, config);
104
+
105
+ // Call onRetry callback if provided
106
+ if (options.onRetry) {
107
+ options.onRetry(attempt + 1, error, delayMs);
108
+ }
109
+
110
+ await sleep(delayMs);
111
+ }
112
+ }
113
+
114
+ throw lastError;
115
+ }
116
+
8
117
  /**
9
118
  * Generate Sentry trace headers for distributed tracing
10
119
  * Uses the same trace ID derivation as the API (MD5 hash of session ID)
@@ -118,7 +227,19 @@ const createSDK = (emitter, config, sessionInstance) => {
118
227
  };
119
228
 
120
229
  try {
121
- let res = await axios(url, c);
230
+ let res = await withRetry(
231
+ () => axios(url, c),
232
+ {
233
+ onRetry: (attempt, error, delayMs) => {
234
+ emitter.emit(events.sdk.retry, {
235
+ path: 'auth/exchange-api-key',
236
+ attempt,
237
+ error: error.message || error.code,
238
+ delayMs,
239
+ });
240
+ },
241
+ }
242
+ );
122
243
 
123
244
  token = res.data.token;
124
245
  return token;
@@ -243,7 +364,7 @@ const createSDK = (emitter, config, sessionInstance) => {
243
364
  ...sentryHeaders, // Add Sentry distributed tracing headers
244
365
  },
245
366
  responseType: typeof onChunk === "function" ? "stream" : "json",
246
- timeout: 60000, // 60 second timeout to prevent hanging requests
367
+ timeout: 120000, // 120 second timeout to prevent hanging requests
247
368
  data: {
248
369
  ...data,
249
370
  session: sessionInstance.get(),
@@ -254,7 +375,25 @@ const createSDK = (emitter, config, sessionInstance) => {
254
375
  try {
255
376
  let response;
256
377
 
257
- response = await axios(url, c);
378
+ // Use retry logic for non-streaming requests
379
+ // Streaming requests are not retried as they involve ongoing data transfer
380
+ if (typeof onChunk !== "function") {
381
+ response = await withRetry(
382
+ () => axios(url, c),
383
+ {
384
+ onRetry: (attempt, error, delayMs) => {
385
+ emitter.emit(events.sdk.retry, {
386
+ path,
387
+ attempt,
388
+ error: error.message || error.code,
389
+ delayMs,
390
+ });
391
+ },
392
+ }
393
+ );
394
+ } else {
395
+ response = await axios(url, c);
396
+ }
258
397
 
259
398
  emitter.emit(events.sdk.response, {
260
399
  path,
@@ -56,11 +56,6 @@ const createSystem = (emitter, sandbox, config) => {
56
56
 
57
57
  await screenshot({ filename: step1, format: "png" });
58
58
 
59
- // Location of cursor image
60
- const cursorPath = path.join(__dirname, "resources", "cursor-2.png");
61
-
62
- const mousePos = await getMousePosition();
63
-
64
59
  // Load the screenshot image with Jimp
65
60
  let image = await Jimp.read(step1);
66
61
 
@@ -76,9 +71,12 @@ const createSystem = (emitter, sandbox, config) => {
76
71
  );
77
72
 
78
73
  if (mouse) {
74
+ // Only get mouse position when needed to avoid unnecessary websocket calls
75
+ const cursorPath = path.join(__dirname, "resources", "cursor-2.png");
76
+ const mousePos = await getMousePosition();
77
+
79
78
  // Load and composite the mouse cursor image
80
79
  const cursorImage = await Jimp.read(cursorPath);
81
-
82
80
  image.composite(cursorImage, mousePos.x, mousePos.y);
83
81
  }
84
82
 
@@ -0,0 +1,204 @@
1
+ ---
2
+ name: testdriver:ai
3
+ description: Execute natural language tasks using AI
4
+ ---
5
+ <!-- Generated from ai.mdx. DO NOT EDIT. -->
6
+
7
+ ## Overview
8
+
9
+ The `ai()` method allows you to execute complex tasks using natural language descriptions. TestDriver's AI will figure out the steps needed to accomplish the task.
10
+
11
+ ## Syntax
12
+
13
+ ```javascript
14
+ await testdriver.ai(task, options)
15
+ ```
16
+
17
+ ## Parameters
18
+
19
+ <ParamField path="task" type="string" required>
20
+ Natural language description of what to do
21
+ </ParamField>
22
+
23
+ <ParamField path="options" type="object">
24
+ Execution options
25
+
26
+ <Expandable title="properties">
27
+ <ParamField path="validateAndLoop" type="boolean" default="false">
28
+ Whether to validate completion and retry if incomplete
29
+ </ParamField>
30
+ </Expandable>
31
+ </ParamField>
32
+
33
+ ## Returns
34
+
35
+ `Promise<string | void>` - Final AI response if `validateAndLoop` is true
36
+
37
+ ## Examples
38
+
39
+ ### Basic Usage
40
+
41
+ ```javascript
42
+ // Simple task execution
43
+ await testdriver.ai('Click the submit button');
44
+
45
+ // Complex multi-step task
46
+ await testdriver.ai('Fill out the contact form and submit it');
47
+
48
+ // Navigation task
49
+ await testdriver.ai('Go to the settings page and enable notifications');
50
+ ```
51
+
52
+ ### With Validation
53
+
54
+ ```javascript
55
+ // AI will verify the task completed successfully
56
+ const result = await testdriver.ai('Complete the checkout process', {
57
+ validateAndLoop: true
58
+ });
59
+
60
+ console.log('Task result:', result);
61
+ ```
62
+
63
+ ### Multi-Step Workflows
64
+
65
+ ```javascript
66
+ // The AI will break down complex tasks
67
+ await testdriver.ai('Search for "laptop", add the first result to cart, and proceed to checkout');
68
+
69
+ // UI exploration
70
+ await testdriver.ai('Find and click all menu items to explore the application');
71
+ ```
72
+
73
+ ## Use Cases
74
+
75
+ <AccordionGroup>
76
+ <Accordion title="Exploratory Testing">
77
+ Use AI to explore unfamiliar applications:
78
+
79
+ ```javascript
80
+ await testdriver.ai('Explore the main navigation menu');
81
+ await testdriver.ai('Try to find the user profile settings');
82
+ ```
83
+ </Accordion>
84
+
85
+ <Accordion title="Complex Workflows">
86
+ Let AI handle multi-step processes:
87
+
88
+ ```javascript
89
+ await testdriver.ai('Complete the multi-step registration form');
90
+ await testdriver.ai('Configure all the advanced settings to default values');
91
+ ```
92
+ </Accordion>
93
+
94
+ <Accordion title="Flexible Interactions">
95
+ When exact element locations aren't critical:
96
+
97
+ ```javascript
98
+ await testdriver.ai('Close any popup dialogs');
99
+ await testdriver.ai('Accept the cookie consent if it appears');
100
+ ```
101
+ </Accordion>
102
+ </AccordionGroup>
103
+
104
+ ## Best Practices
105
+
106
+ <Check>
107
+ **Be specific but flexible**: Give enough context without over-constraining the AI
108
+
109
+ ```javascript
110
+ // ✅ Good
111
+ await testdriver.ai('Add the first product to the shopping cart');
112
+
113
+ // ❌ Too vague
114
+ await testdriver.ai('do something');
115
+
116
+ // ❌ Too specific (defeats the purpose)
117
+ await testdriver.ai('click at coordinates 500, 300');
118
+ ```
119
+ </Check>
120
+
121
+ <Check>
122
+ **Use for exploration, not precision**: For critical assertions, use explicit methods
123
+
124
+ ```javascript
125
+ // Use AI for setup
126
+ await testdriver.ai('Navigate to the login page');
127
+
128
+ // Use explicit methods for critical steps
129
+ const usernameField = await testdriver.find('username input');
130
+ await usernameField.click();
131
+ await testdriver.type('testuser');
132
+
133
+ await testdriver.assert('login page is displayed');
134
+ ```
135
+ </Check>
136
+
137
+ <Warning>
138
+ **AI tasks may be slower**: The AI needs to analyze the screen and plan actions. For performance-critical tests, use explicit methods.
139
+ </Warning>
140
+
141
+ ## When to Use AI vs Explicit Methods
142
+
143
+ ### Use `ai()` when:
144
+ - Exploring unfamiliar applications
145
+ - Handling optional UI elements (popups, cookies, etc.)
146
+ - Prototyping tests quickly
147
+ - Tasks where exact steps may vary
148
+
149
+ ### Use explicit methods when:
150
+ - Performance is critical
151
+ - You need precise control
152
+ - Making assertions
153
+ - Debugging test failures
154
+ - Repetitive, predictable actions
155
+
156
+ ## Complete Example
157
+
158
+ ```javascript
159
+ import { beforeAll, afterAll, describe, it } from 'vitest';
160
+ import TestDriver from 'testdriverai';
161
+
162
+ describe('E-commerce Flow with AI', () => {
163
+ let testdriver;
164
+
165
+ beforeAll(async () => {
166
+ client = new TestDriver(process.env.TD_API_KEY);
167
+ await testdriver.auth();
168
+ await testdriver.connect();
169
+ });
170
+
171
+ afterAll(async () => {
172
+ await testdriver.disconnect();
173
+ });
174
+
175
+ it('should complete shopping flow using AI assistance', async () => {
176
+ await testdriver.focusApplication('Google Chrome');
177
+
178
+ // Use AI for navigation and exploration
179
+ await testdriver.ai('Browse to the electronics section');
180
+ await testdriver.ai('Find and add a laptop to the cart');
181
+
182
+ // Use explicit methods for critical steps
183
+ const cartIcon = await testdriver.find('shopping cart icon');
184
+ await cartIcon.click();
185
+
186
+ const total = await testdriver.extract('the cart total amount');
187
+ console.log('Cart total:', total);
188
+
189
+ // Use AI for checkout flow
190
+ await testdriver.ai('Proceed to checkout and fill in shipping details', {
191
+ validateAndLoop: true
192
+ });
193
+
194
+ // Explicit assertion
195
+ await testdriver.assert('order confirmation page is displayed');
196
+ });
197
+ });
198
+ ```
199
+
200
+ ## Related Methods
201
+
202
+ - [`find()`](/v7/find) - Locate specific elements
203
+ - [`assert()`](/v7/assert) - Make assertions
204
+ - [`extract()`](/v7/extract) - Extract information
@@ -0,0 +1,315 @@
1
+ ---
2
+ name: testdriver:assert
3
+ description: Make AI-powered assertions about screen state
4
+ ---
5
+ <!-- Generated from assert.mdx. DO NOT EDIT. -->
6
+
7
+ ## Overview
8
+
9
+ Make AI-powered assertions about the current screen state using natural language. The AI analyzes the screen and verifies that your assertion is true.
10
+
11
+ ## Syntax
12
+
13
+ ```javascript
14
+ await testdriver.assert(assertion)
15
+ await testdriver.assert(assertion, options)
16
+ ```
17
+
18
+ ## Parameters
19
+
20
+ <ParamField path="assertion" type="string" required>
21
+ Natural language description of what should be true
22
+ </ParamField>
23
+
24
+ <ParamField path="options" type="object">
25
+ Optional configuration
26
+
27
+ <Expandable title="properties">
28
+ <ParamField path="ai" type="object">
29
+ AI sampling configuration for this assert call (overrides global `ai` config from constructor).
30
+
31
+ <Expandable title="properties">
32
+ <ParamField path="temperature" type="number">
33
+ Controls randomness. `0` = deterministic, higher = more creative. Default: model default.
34
+ </ParamField>
35
+
36
+ <ParamField path="top" type="object">
37
+ Sampling parameters
38
+
39
+ <Expandable title="properties">
40
+ <ParamField path="p" type="number">
41
+ Top-P (nucleus sampling). Range: 0-1.
42
+ </ParamField>
43
+
44
+ <ParamField path="k" type="number">
45
+ Top-K sampling. `1` = most deterministic.
46
+ </ParamField>
47
+ </Expandable>
48
+ </ParamField>
49
+ </Expandable>
50
+ </ParamField>
51
+ </Expandable>
52
+ </ParamField>
53
+
54
+ ## Returns
55
+
56
+ `Promise<boolean>` - `true` if assertion passes, throws error if assertion fails
57
+
58
+ ## Examples
59
+
60
+ ### Basic Assertions
61
+
62
+ ```javascript
63
+ // Verify page elements
64
+ await testdriver.assert('the login page is displayed');
65
+ await testdriver.assert('submit button is visible');
66
+ await testdriver.assert('error message is shown');
67
+
68
+ // Verify text content
69
+ await testdriver.assert('the page title is "Welcome"');
70
+ await testdriver.assert('username field contains "john.doe"');
71
+ await testdriver.assert('success message says "Account created"');
72
+
73
+ // Verify states
74
+ await testdriver.assert('the form is empty');
75
+ await testdriver.assert('the checkbox is checked');
76
+ await testdriver.assert('the dropdown shows "United States"');
77
+
78
+ // Verify visual appearance
79
+ await testdriver.assert('the button is blue');
80
+ await testdriver.assert('the loading spinner is displayed');
81
+ await testdriver.assert('the modal dialog is open');
82
+ ```
83
+
84
+ ## Best Practices
85
+
86
+ <Check>
87
+ **Be specific in assertions**
88
+
89
+ More specific assertions are more reliable:
90
+
91
+ ```javascript
92
+ // ❌ Too vague
93
+ await testdriver.assert('button is visible');
94
+
95
+ // ✅ Specific
96
+ await testdriver.assert('blue submit button is visible below the form');
97
+ ```
98
+ </Check>
99
+
100
+ <Check>
101
+ **Assert state changes**
102
+
103
+ Verify state before and after actions:
104
+
105
+ ```javascript
106
+ // Before
107
+ await testdriver.assert('cart is empty');
108
+
109
+ // Action
110
+ const addBtn = await testdriver.find('add to cart');
111
+ await addBtn.click();
112
+
113
+ // After
114
+ await testdriver.assert('cart contains 1 item');
115
+ ```
116
+ </Check>
117
+
118
+ <Check>
119
+ **Use with test framework assertions**
120
+
121
+ Combine AI assertions with traditional test assertions:
122
+
123
+ ```javascript
124
+ // AI assertion
125
+ const result = await testdriver.assert('success message is displayed');
126
+
127
+ // Framework assertion
128
+ expect(result).toBeTruthy();
129
+
130
+ // Extract for detailed comparison
131
+ const message = await testdriver.extract('the success message text');
132
+ expect(message).toContain('successfully');
133
+ ```
134
+ </Check>
135
+
136
+ ## Polling Assertions
137
+
138
+ For conditions that may take time to become true:
139
+
140
+ ```javascript
141
+ async function waitForAssertion(testdriver, assertion, timeout = 30000) {
142
+ const startTime = Date.now();
143
+
144
+ while (Date.now() - startTime < timeout) {
145
+ try {
146
+ await testdriver.assert(assertion);
147
+ return true; // Assertion passed
148
+ } catch (error) {
149
+ // Assertion failed, wait and retry
150
+ await new Promise(r => setTimeout(r, 1000));
151
+ }
152
+ }
153
+
154
+ throw new Error(`Assertion timeout: "${assertion}"`);
155
+ }
156
+
157
+ // Usage
158
+ await waitForAssertion(testdriver, 'page has finished loading', 30000);
159
+ await waitForAssertion(testdriver, 'results are displayed', 10000);
160
+ ```
161
+
162
+ ## Use Cases
163
+
164
+ <AccordionGroup>
165
+ <Accordion title="Form Validation">
166
+ ```javascript
167
+ // Try to submit empty form
168
+ const submitBtn = await testdriver.find('submit button');
169
+ await submitBtn.click();
170
+
171
+ // Verify validation errors
172
+ await testdriver.assert('email field shows "required" error');
173
+ await testdriver.assert('password field shows "required" error');
174
+
175
+ // Verify form not submitted
176
+ await testdriver.assert('still on the form page');
177
+ ```
178
+ </Accordion>
179
+
180
+ <Accordion title="Page Navigation">
181
+ ```javascript
182
+ const loginBtn = await testdriver.find('login button');
183
+ await loginBtn.click();
184
+
185
+ // Verify navigation
186
+ await testdriver.assert('user dashboard is displayed');
187
+ await testdriver.assert('welcome message shows user name');
188
+ await testdriver.assert('logout button is visible');
189
+ ```
190
+ </Accordion>
191
+
192
+ <Accordion title="Dynamic Content">
193
+ ```javascript
194
+ const loadBtn = await testdriver.find('load more button');
195
+ await loadBtn.click();
196
+
197
+ // Poll for content using helper
198
+ await waitForAssertion(testdriver, 'more than 10 items are shown', 10000);
199
+ await testdriver.assert('load more button is still visible');
200
+ ```
201
+ </Accordion>
202
+
203
+ <Accordion title="Visual States">
204
+ ```javascript
205
+ // Verify hover effect
206
+ const button = await testdriver.find('primary button');
207
+ await button.hover();
208
+
209
+ await testdriver.assert('button background is darker');
210
+
211
+ // Verify button is enabled
212
+ await testdriver.assert('submit button is enabled');
213
+ ```
214
+ </Accordion>
215
+
216
+ <Accordion title="Multi-Step Workflows">
217
+ ```javascript
218
+ // Step 1
219
+ await testdriver.assert('step 1 is active');
220
+ const nextBtn = await testdriver.find('next button');
221
+ await nextBtn.click();
222
+
223
+ // Step 2
224
+ await testdriver.assert('step 2 is active');
225
+ await testdriver.assert('step 1 is completed');
226
+ await nextBtn.click();
227
+
228
+ // Step 3
229
+ await testdriver.assert('step 3 is active');
230
+ await testdriver.assert('step 2 is completed');
231
+ ```
232
+ </Accordion>
233
+ </AccordionGroup>
234
+
235
+ ## Complete Example
236
+
237
+ ```javascript
238
+ import { beforeAll, afterAll, describe, it, expect } from 'vitest';
239
+ import TestDriver from 'testdriverai';
240
+
241
+ describe('Assertions', () => {
242
+ let testdriver;
243
+
244
+ beforeAll(async () => {
245
+ client = new TestDriver(process.env.TD_API_KEY);
246
+ await testdriver.auth();
247
+ await testdriver.connect();
248
+ });
249
+
250
+ afterAll(async () => {
251
+ await testdriver.disconnect();
252
+ });
253
+
254
+ it('should validate login flow', async () => {
255
+ await testdriver.focusApplication('Google Chrome');
256
+
257
+ // Verify initial state
258
+ await testdriver.assert('the login page is displayed');
259
+ await testdriver.assert('username field is empty');
260
+ await testdriver.assert('password field is empty');
261
+
262
+ // Fill form
263
+ const usernameField = await testdriver.find('username input');
264
+ await usernameField.click();
265
+ await testdriver.type('testuser');
266
+
267
+ await testdriver.pressKeys(['tab']);
268
+ await testdriver.type('password123');
269
+
270
+ // Verify fields filled
271
+ await testdriver.assert('username field contains "testuser"');
272
+ await testdriver.assert('password field is not empty');
273
+
274
+ // Submit
275
+ await testdriver.pressKeys(['enter']);
276
+
277
+ // Poll for success
278
+ await waitForAssertion(testdriver, 'user dashboard is displayed', 10000);
279
+
280
+ // Verify logged in state
281
+ await testdriver.assert('welcome message is shown');
282
+ await testdriver.assert('logout button is visible');
283
+
284
+ // Verify login page is gone
285
+ await testdriver.assert('login form is displayed', false, true);
286
+ });
287
+
288
+ it('should show validation errors', async () => {
289
+ // Try empty submission
290
+ const submitBtn = await testdriver.find('submit button');
291
+ await submitBtn.click();
292
+
293
+ // Verify multiple errors
294
+ const errors = [
295
+ 'email field shows error',
296
+ 'name field shows error',
297
+ 'password field shows error'
298
+ ];
299
+
300
+ for (const error of errors) {
301
+ const result = await testdriver.assert(error);
302
+ expect(result).toBeTruthy();
303
+ }
304
+
305
+ // Verify not submitted
306
+ await testdriver.assert('confirmation page is shown', false, true);
307
+ });
308
+ });
309
+ ```
310
+
311
+ ## Related Methods
312
+
313
+ - [`extract()`](/v7/extract) - Extract information for detailed assertions
314
+ - [`find()`](/v7/find) - Locate elements to verify
315
+ - [`ai()`](/v7/ai) - Complex AI-driven tasks