-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomprehensive_playwright_test.js
More file actions
595 lines (496 loc) · 19.5 KB
/
comprehensive_playwright_test.js
File metadata and controls
595 lines (496 loc) · 19.5 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/env node
/**
* Comprehensive Playwright MCP Test for Vercel AI SDK
* Tests AG UI components, security, and functionality with extensive debug logs
*/
import { chromium } from 'playwright';
import { performance } from 'perf_hooks';
console.log('🎭 Starting Comprehensive Playwright MCP Tests for Vercel AI SDK...\n');
// Debug configuration
const DEBUG_MODE = true;
const LOG_PERFORMANCE = true;
const LOG_NETWORK = true;
async function runComprehensiveTests() {
let browser;
let page;
let context;
const testStartTime = performance.now();
const testResults = {
passed: 0,
failed: 0,
warnings: 0,
performanceMetrics: {},
networkRequests: [],
errors: []
};
try {
// Launch browser with debug options
console.log('🚀 Launching browser with debug configuration...');
browser = await chromium.launch({
headless: false,
slowMo: DEBUG_MODE ? 50 : 0,
devtools: DEBUG_MODE,
args: [
'--disable-web-security',
'--allow-running-insecure-content',
'--window-size=1280,800'
]
});
context = await browser.newContext({
viewport: { width: 1280, height: 800 },
javaScriptEnabled: true,
ignoreHTTPSErrors: true,
bypassCSP: true
});
// Enable network logging
if (LOG_NETWORK) {
await context.route('**', route => {
const url = route.request().url();
const method = route.request().method();
testResults.networkRequests.push({
timestamp: new Date().toISOString(),
url,
method,
type: route.request().resourceType()
});
route.continue();
});
}
page = await context.newPage();
// Test 1: Application Launch and Initial Load
console.log('\n1️⃣ Testing Application Launch and Initial Load...');
const loadStartTime = performance.now();
try {
await page.goto('http://localhost:3000', {
timeout: 30000,
waitUntil: 'networkidle'
});
const loadEndTime = performance.now();
testResults.performanceMetrics.initialLoad = (loadEndTime - loadStartTime) / 1000;
console.log('✅ Application launched successfully');
console.log(`📊 Initial load time: ${testResults.performanceMetrics.initialLoad.toFixed(2)}s`);
testResults.passed++;
// Verify page title
const title = await page.title();
console.log(`📋 Page title: "${title}"`);
// Take screenshot for visual verification
await page.screenshot({
path: '/home/aparna/Desktop/vercel-ai-sdk/test-screenshots/initial-load.png',
fullPage: true
});
console.log('📸 Initial load screenshot captured');
} catch (error) {
console.error('❌ Application launch failed:', error.message);
testResults.failed++;
testResults.errors.push({
test: 'Application Launch',
error: error.message,
stack: error.stack
});
}
// Test 2: UI Component Verification
console.log('\n2️⃣ Testing UI Component Verification...');
try {
// Check for main header
const header = await page.$('header');
if (header) {
console.log('✅ Main header component found');
} else {
console.warn('⚠️ Main header component not found');
testResults.warnings++;
}
// Check for product grid
const productGrid = await page.$('.grid.grid-cols-1');
if (productGrid) {
console.log('✅ Product grid component found');
} else {
console.warn('⚠️ Product grid component not found');
testResults.warnings++;
}
// Check for chat button
const chatButton = await page.$('button:has-text("Chat Support")');
if (chatButton) {
console.log('✅ Chat support button found');
} else {
console.warn('⚠️ Chat support button not found');
testResults.warnings++;
}
// Check for search input
const searchInput = await page.$('input[placeholder="Search products..."]');
if (searchInput) {
console.log('✅ Search input component found');
} else {
console.warn('⚠️ Search input component not found');
testResults.warnings++;
}
testResults.passed++;
} catch (error) {
console.error('❌ UI component verification failed:', error.message);
testResults.failed++;
testResults.errors.push({
test: 'UI Component Verification',
error: error.message,
stack: error.stack
});
}
// Test 3: Chat Interface Functionality
console.log('\n3️⃣ Testing Chat Interface Functionality...');
try {
// Open chat interface
const chatButton = await page.$('button:has-text("Chat Support")');
if (chatButton) {
await chatButton.click();
console.log('✅ Chat interface opened');
// Wait for chat interface to appear
await page.waitForSelector('.fixed.bottom-4.right-4.w-96', { timeout: 5000 });
console.log('✅ Chat interface visible');
// Check for chat input
const chatInput = await page.$('input[placeholder="Ask about orders, products, or support..."]');
if (chatInput) {
console.log('✅ Chat input field found');
// Test typing in chat
await chatInput.type('Hello, can you help me?');
console.log('✅ Chat input typing successful');
// Take screenshot of chat interface
await page.screenshot({
path: '/home/aparna/Desktop/vercel-ai-sdk/test-screenshots/chat-interface.png',
clip: { x: 0, y: 0, width: 1280, height: 800 }
});
console.log('📸 Chat interface screenshot captured');
} else {
console.warn('⚠️ Chat input field not found');
testResults.warnings++;
}
// Close chat interface
const closeButton = await page.$('button[title="Close Chat"]');
if (closeButton) {
await closeButton.click();
console.log('✅ Chat interface closed successfully');
}
} else {
console.warn('⚠️ Chat button not found, skipping chat interface test');
testResults.warnings++;
}
testResults.passed++;
} catch (error) {
console.error('❌ Chat interface functionality test failed:', error.message);
testResults.failed++;
testResults.errors.push({
test: 'Chat Interface Functionality',
error: error.message,
stack: error.stack
});
}
// Test 4: Product Search and Filtering
console.log('\n4️⃣ Testing Product Search and Filtering...');
try {
const searchInput = await page.$('input[placeholder="Search products..."]');
if (searchInput) {
// Test search functionality
await searchInput.type('laptop');
console.log('✅ Search input typed');
// Wait for search results
await page.waitForTimeout(1000);
// Check if products are filtered
const products = await page.$$('.grid.grid-cols-1 .bg-white');
console.log(`📊 Found ${products.length} products after search`);
// Clear search
await searchInput.fill('');
console.log('✅ Search cleared');
// Test category filtering
const categoryButtons = await page.$$('button:has-text("Computers")');
if (categoryButtons.length > 0) {
await categoryButtons[0].click();
console.log('✅ Category filter clicked');
await page.waitForTimeout(1000);
const filteredProducts = await page.$$('.grid.grid-cols-1 .bg-white');
console.log(`📊 Found ${filteredProducts.length} products after category filter`);
}
testResults.passed++;
} else {
console.warn('⚠️ Search input not found, skipping search test');
testResults.warnings++;
}
} catch (error) {
console.error('❌ Product search and filtering test failed:', error.message);
testResults.failed++;
testResults.errors.push({
test: 'Product Search and Filtering',
error: error.message,
stack: error.stack
});
}
// Test 5: API Endpoint Testing
console.log('\n5️⃣ Testing API Endpoint Functionality...');
try {
// Test API endpoint directly
const apiResponse = await page.evaluate(async () => {
try {
const response = await fetch('/api/chat/route-ollama', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
messages: [
{
role: 'user',
content: 'Hello, can you check my orders for alice@example.com?'
}
]
})
});
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
const data = await response.text();
return { success: true, data };
} catch (error) {
return { success: false, error: error.message };
}
});
if (apiResponse.success) {
console.log('✅ API endpoint test successful');
console.log('📊 API response received');
testResults.passed++;
} else {
console.error('❌ API endpoint test failed:', apiResponse.error);
testResults.failed++;
testResults.errors.push({
test: 'API Endpoint',
error: apiResponse.error
});
}
} catch (error) {
console.error('❌ API endpoint test failed:', error.message);
testResults.failed++;
testResults.errors.push({
test: 'API Endpoint',
error: error.message,
stack: error.stack
});
}
// Test 6: Security Features Verification
console.log('\n6️⃣ Testing Security Features...');
try {
// Check for security headers
const response = await page.goto('http://localhost:3000', {
waitUntil: 'networkidle'
});
const headers = response.headers();
console.log('📋 Security headers check:');
console.log(` - X-Powered-By: ${headers['x-powered-by'] || 'Not present'}`);
console.log(` - Cache-Control: ${headers['cache-control'] || 'Not present'}`);
// Check for CSP (Content Security Policy)
const csp = headers['content-security-policy'];
if (csp) {
console.log('✅ Content Security Policy found');
} else {
console.warn('⚠️ Content Security Policy not found');
testResults.warnings++;
}
// Check for secure cookies
const cookies = await context.cookies();
const secureCookies = cookies.filter(cookie => cookie.secure);
console.log(`📊 Secure cookies: ${secureCookies.length}/${cookies.length}`);
testResults.passed++;
} catch (error) {
console.error('❌ Security features test failed:', error.message);
testResults.failed++;
testResults.errors.push({
test: 'Security Features',
error: error.message,
stack: error.stack
});
}
// Test 7: Performance Testing
console.log('\n7️⃣ Testing Performance Metrics...');
try {
const performanceMetrics = await page.evaluate(() => {
return {
memory: window.performance.memory ? window.performance.memory.usedJSHeapSize / 1024 / 1024 : 'N/A',
timing: {
loadEventEnd: window.performance.timing ? window.performance.timing.loadEventEnd : 'N/A',
domContentLoadedEventEnd: window.performance.timing ? window.performance.timing.domContentLoadedEventEnd : 'N/A'
}
};
});
console.log('📊 Performance metrics:');
console.log(` - Memory usage: ${performanceMetrics.memory} MB`);
console.log(` - DOM Content Loaded: ${performanceMetrics.timing.domContentLoadedEventEnd}ms`);
console.log(` - Load Event End: ${performanceMetrics.timing.loadEventEnd}ms`);
testResults.performanceMetrics.memoryUsage = performanceMetrics.memory;
testResults.performanceMetrics.domContentLoaded = performanceMetrics.timing.domContentLoadedEventEnd;
testResults.performanceMetrics.loadEventEnd = performanceMetrics.timing.loadEventEnd;
testResults.passed++;
} catch (error) {
console.error('❌ Performance test failed:', error.message);
testResults.failed++;
testResults.errors.push({
test: 'Performance Testing',
error: error.message,
stack: error.stack
});
}
// Test 8: Error Handling
console.log('\n8️⃣ Testing Error Handling...');
try {
// Test invalid API call
const errorResponse = await page.evaluate(async () => {
try {
const response = await fetch('/api/nonexistent', {
method: 'GET'
});
return { success: false, status: response.status };
} catch (error) {
return { success: false, error: error.message };
}
});
if (errorResponse.status === 404) {
console.log('✅ Error handling test successful (404 response)');
testResults.passed++;
} else {
console.warn('⚠️ Unexpected error response:', errorResponse);
testResults.warnings++;
}
} catch (error) {
console.error('❌ Error handling test failed:', error.message);
testResults.failed++;
testResults.errors.push({
test: 'Error Handling',
error: error.message,
stack: error.stack
});
}
// Test 9: Accessibility Testing
console.log('\n9️⃣ Testing Accessibility Features...');
try {
// Check for ARIA attributes
const ariaElements = await page.$$('[aria-label], [aria-labelledby], [aria-describedby]');
console.log(`📊 ARIA elements found: ${ariaElements.length}`);
// Check for semantic HTML
const semanticElements = await page.$$('header, nav, main, footer, article, section');
console.log(`📊 Semantic HTML elements found: ${semanticElements.length}`);
// Check for alt text on images
const images = await page.$$('img');
const imagesWithAlt = await page.$$('img[alt]');
console.log(`📊 Images with alt text: ${imagesWithAlt.length}/${images.length}`);
testResults.passed++;
} catch (error) {
console.error('❌ Accessibility test failed:', error.message);
testResults.failed++;
testResults.errors.push({
test: 'Accessibility Testing',
error: error.message,
stack: error.stack
});
}
// Test 10: Database Tool Integration (Simulated)
console.log('\n🔟 Testing Database Tool Integration (Simulated)...');
try {
// Simulate database tool call
const dbToolCall = {
type: 'order',
userEmail: 'alice@example.com',
identifiers: [{ email: 'alice@example.com' }]
};
console.log('✅ Database tool call structure validated');
console.log('📊 Simulated tool call:', JSON.stringify(dbToolCall, null, 2));
// Verify security parameters
if (dbToolCall.userEmail && dbToolCall.identifiers.length > 0) {
console.log('✅ Security parameters present (userEmail and identifiers)');
} else {
console.warn('⚠️ Missing security parameters');
testResults.warnings++;
}
testResults.passed++;
} catch (error) {
console.error('❌ Database tool integration test failed:', error.message);
testResults.failed++;
testResults.errors.push({
test: 'Database Tool Integration',
error: error.message,
stack: error.stack
});
}
// Final summary
const testEndTime = performance.now();
const totalTestTime = (testEndTime - testStartTime) / 1000;
console.log('\n🎉 All Playwright MCP tests completed!');
console.log('\n📊 Test Summary:');
console.log(` ✅ Tests Passed: ${testResults.passed}`);
console.log(` ❌ Tests Failed: ${testResults.failed}`);
console.log(` ⚠️ Warnings: ${testResults.warnings}`);
console.log(` ⏱️ Total Test Time: ${totalTestTime.toFixed(2)}s`);
if (LOG_PERFORMANCE) {
console.log('\n📈 Performance Metrics:');
console.log(` - Initial Load Time: ${testResults.performanceMetrics.initialLoad?.toFixed(2) || 'N/A'}s`);
console.log(` - Memory Usage: ${testResults.performanceMetrics.memoryUsage || 'N/A'} MB`);
console.log(` - DOM Content Loaded: ${testResults.performanceMetrics.domContentLoaded || 'N/A'}ms`);
console.log(` - Load Event End: ${testResults.performanceMetrics.loadEventEnd || 'N/A'}ms`);
}
if (LOG_NETWORK) {
console.log(`\n🌐 Network Requests: ${testResults.networkRequests.length}`);
const apiRequests = testResults.networkRequests.filter(req => req.url.includes('/api/'));
console.log(` - API Requests: ${apiRequests.length}`);
const staticRequests = testResults.networkRequests.filter(req => req.type === 'stylesheet' || req.type === 'script' || req.type === 'image');
console.log(` - Static Assets: ${staticRequests.length}`);
}
if (testResults.errors.length > 0) {
console.log('\n❌ Errors Encountered:');
testResults.errors.forEach((error, index) => {
console.log(` ${index + 1}. ${error.test}: ${error.error}`);
});
}
// Generate detailed test report
const report = {
timestamp: new Date().toISOString(),
testDuration: totalTestTime,
environment: {
browser: 'Chromium',
headless: false,
debugMode: DEBUG_MODE
},
results: {
passed: testResults.passed,
failed: testResults.failed,
warnings: testResults.warnings,
errors: testResults.errors
},
performance: testResults.performanceMetrics,
network: {
totalRequests: testResults.networkRequests.length,
apiRequests: testResults.networkRequests.filter(req => req.url.includes('/api/')).length,
staticAssets: testResults.networkRequests.filter(req => req.type === 'stylesheet' || req.type === 'script' || req.type === 'image').length
}
};
// Save report to file
const fs = await import('fs');
const path = await import('path');
const reportsDir = '/home/aparna/Desktop/vercel-ai-sdk/test-reports';
if (!fs.existsSync(reportsDir)) {
fs.mkdirSync(reportsDir, { recursive: true });
}
const reportPath = path.join(reportsDir, `test-report-${new Date().toISOString().replace(/[:.]/g, '-')}.json`);
fs.writeFileSync(reportPath, JSON.stringify(report, null, 2));
console.log(`\n📄 Detailed test report saved to: ${reportPath}`);
// Close browser
await browser.close();
console.log('\n🔌 Browser closed');
return testResults.failed === 0;
} catch (error) {
console.error('💥 Playwright test crashed:', error.message);
console.error('Stack:', error.stack);
if (browser) {
await browser.close();
}
return false;
}
}
// Run tests
runComprehensiveTests().then(success => {
process.exit(success ? 0 : 1);
}).catch(error => {
console.error('💥 Playwright test crashed:', error.message);
process.exit(1);
});