Repository navigation
Expand file tree
/
Copy pathtest-harness.js
More file actions
executable file
·318 lines (276 loc) · 9.48 KB
/
Copy pathtest-harness.js
File metadata and controls
executable file
·318 lines (276 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env node
/**
* Automated Test Harness for React Input Bug Detection
*
* Runs click/type tests repeatedly to catch intermittent failures.
*
* Usage:
* node test-harness.js [iterations] [testUrl]
*
* Examples:
* node test-harness.js # 50 iterations on default test page
* node test-harness.js 100 # 100 iterations
* node test-harness.js 50 http://localhost:8080/settings # Test specific URL
*
* Requirements:
* - Chrome running with --remote-debugging-port=9222
* - Test page loaded (or provide URL)
*/
const path = require('path');
const chromeWs = require('./skills/browsing/chrome-ws-lib.js');
// Test configuration
const DEFAULT_ITERATIONS = 50;
const DEFAULT_TEST_URL = `file://${path.join(__dirname, 'test-react-inputs.html')}`;
// Test results tracking
const results = {
passed: 0,
failed: 0,
errors: []
};
// Colors for console output
const colors = {
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
cyan: '\x1b[36m',
reset: '\x1b[0m'
};
function log(color, msg) {
console.log(`${colors[color]}${msg}${colors.reset}`);
}
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Test: Click on a selector and verify click was registered
*/
async function testClick(tabIndex, selector, expectedChange) {
try {
await chromeWs.click(tabIndex, selector);
await sleep(50); // Brief wait for React to update
return { success: true };
} catch (e) {
return { success: false, error: e.message };
}
}
/**
* Test: Type into a selector and verify value changed
*/
async function testType(tabIndex, selector, text) {
try {
await chromeWs.fill(tabIndex, text, selector);
await sleep(50);
// Verify the value was set
const value = await chromeWs.eval(tabIndex,
`document.querySelector(${JSON.stringify(selector)})?.value || ''`
);
// For transformed inputs, just check it's not empty
if (value && value.length > 0) {
return { success: true, value };
}
return { success: false, error: `Value not set. Expected text, got: "${value}"` };
} catch (e) {
return { success: false, error: e.message };
}
}
/**
* Test: Type and submit with Enter key
*/
async function testTypeAndEnter(tabIndex, selector, text) {
try {
await chromeWs.fill(tabIndex, text + '\\n', selector);
await sleep(100);
return { success: true };
} catch (e) {
return { success: false, error: e.message };
}
}
/**
* Test: XPath selector click (tests the text() fallback)
*/
async function testXPathClick(tabIndex, xpath) {
try {
await chromeWs.click(tabIndex, xpath);
await sleep(50);
return { success: true };
} catch (e) {
return { success: false, error: e.message };
}
}
/**
* Clear all inputs on test page
*/
async function clearTestPage(tabIndex) {
await chromeWs.eval(tabIndex, `
document.querySelectorAll('input, textarea').forEach(el => {
el.value = '';
el.dispatchEvent(new Event('input', { bubbles: true }));
});
`);
}
/**
* Run a single test iteration
*/
async function runIteration(tabIndex, iteration) {
const tests = [
{ name: 'Click button', fn: () => testClick(tabIndex, '#test-button') },
{ name: 'Type in controlled input', fn: () => testType(tabIndex, '#controlled-input', `test${iteration}`) },
{ name: 'Type in uncontrolled input', fn: () => testType(tabIndex, '#uncontrolled-input', `test${iteration}`) },
{ name: 'Type in validated input', fn: () => testType(tabIndex, '#validated-input', 'abc') },
{ name: 'Type in nested input', fn: () => testType(tabIndex, '#nested-input', `test${iteration}`) },
{ name: 'Type in transform input', fn: () => testType(tabIndex, '#transform-input', 'hello') },
{ name: 'Type in debounced input', fn: () => testType(tabIndex, '#debounced-input', `test${iteration}`) },
{ name: 'Type in email input', fn: () => testType(tabIndex, '#email-input', 'test@example.com') },
{ name: 'Type in textarea', fn: () => testType(tabIndex, '#controlled-textarea', `line1\\nline2`) },
];
const iterationResults = [];
for (const test of tests) {
const result = await test.fn();
iterationResults.push({ name: test.name, ...result });
if (!result.success) {
results.failed++;
results.errors.push({
iteration,
test: test.name,
error: result.error
});
} else {
results.passed++;
}
}
return iterationResults;
}
/**
* Run tests on a live app (like Brooks)
*/
async function runLiveAppTests(tabIndex, baseUrl, iteration) {
const tests = [];
// Navigate to settings
try {
await chromeWs.navigate(tabIndex, `${baseUrl}/settings`);
await sleep(500);
tests.push({ name: 'Navigate to settings', success: true });
} catch (e) {
tests.push({ name: 'Navigate to settings', success: false, error: e.message });
return tests;
}
// Click New Organization (test XPath with mixed content)
try {
await chromeWs.click(tabIndex, "//a[text()='New Organization'] | //button[text()='New Organization']");
await sleep(200);
tests.push({ name: 'Click New Organization (XPath)', success: true });
} catch (e) {
tests.push({ name: 'Click New Organization (XPath)', success: false, error: e.message });
}
// Type in the form
try {
await chromeWs.fill(tabIndex, `TestOrg${iteration}`, 'form input[type="text"]');
await sleep(100);
tests.push({ name: 'Type org name', success: true });
} catch (e) {
tests.push({ name: 'Type org name', success: false, error: e.message });
}
// Cancel to avoid creating many orgs
try {
await chromeWs.click(tabIndex, "button[type='button']");
await sleep(100);
tests.push({ name: 'Click Cancel', success: true });
} catch (e) {
tests.push({ name: 'Click Cancel', success: false, error: e.message });
}
return tests;
}
/**
* Main test runner
*/
async function main() {
const args = process.argv.slice(2);
const iterations = parseInt(args[0]) || DEFAULT_ITERATIONS;
const testUrl = args[1] || DEFAULT_TEST_URL;
const isLiveApp = testUrl.startsWith('http://localhost');
log('cyan', `\n🧪 React Input Test Harness`);
log('cyan', `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`);
log('cyan', `Iterations: ${iterations}`);
log('cyan', `Test URL: ${testUrl}`);
log('cyan', `Mode: ${isLiveApp ? 'Live App' : 'Test Page'}`);
log('cyan', `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`);
// Initialize
let tabIndex;
try {
tabIndex = await chromeWs.initializeSession();
log('green', `✓ Connected to Chrome (tab index: ${tabIndex})`);
} catch (e) {
log('red', `✗ Failed to connect to Chrome: ${e.message}`);
log('yellow', ` Make sure Chrome is running with --remote-debugging-port=9222`);
process.exit(1);
}
// Navigate to test page
try {
await chromeWs.navigate(tabIndex, testUrl);
await sleep(1000); // Wait for React to initialize
log('green', `✓ Loaded test page`);
} catch (e) {
log('red', `✗ Failed to load test page: ${e.message}`);
process.exit(1);
}
// Run iterations
const startTime = Date.now();
for (let i = 1; i <= iterations; i++) {
process.stdout.write(`\rRunning iteration ${i}/${iterations}...`);
if (isLiveApp) {
const iterResults = await runLiveAppTests(tabIndex, testUrl.replace(/\/[^/]*$/, ''), i);
for (const r of iterResults) {
if (r.success) results.passed++;
else {
results.failed++;
results.errors.push({ iteration: i, test: r.name, error: r.error });
}
}
} else {
// Clear inputs before each iteration
await clearTestPage(tabIndex);
await sleep(50);
await runIteration(tabIndex, i);
}
// Brief pause between iterations
await sleep(100);
}
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
// Print results
console.log('\n');
log('cyan', `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`);
log('cyan', `Results (${elapsed}s)`);
log('cyan', `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`);
log('green', `✓ Passed: ${results.passed}`);
if (results.failed > 0) {
log('red', `✗ Failed: ${results.failed}`);
log('red', `\nFailures:`);
// Group errors by test name
const errorsByTest = {};
for (const err of results.errors) {
const key = err.test;
if (!errorsByTest[key]) errorsByTest[key] = [];
errorsByTest[key].push(err);
}
for (const [test, errors] of Object.entries(errorsByTest)) {
log('red', `\n ${test}: ${errors.length} failures`);
// Show first 3 examples
for (const err of errors.slice(0, 3)) {
log('yellow', ` - Iteration ${err.iteration}: ${err.error}`);
}
if (errors.length > 3) {
log('yellow', ` ... and ${errors.length - 3} more`);
}
}
} else {
log('green', `\n🎉 All tests passed!`);
}
const failRate = ((results.failed / (results.passed + results.failed)) * 100).toFixed(1);
log('cyan', `\nFailure rate: ${failRate}%`);
process.exit(results.failed > 0 ? 1 : 0);
}
main().catch(e => {
log('red', `Fatal error: ${e.message}`);
console.error(e);
process.exit(1);
});