-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-testability-scorer.js
More file actions
1098 lines (897 loc) · 37.2 KB
/
ai-testability-scorer.js
File metadata and controls
1098 lines (897 loc) · 37.2 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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { test, expect } = require('@playwright/test');
const { AITestUtils } = require('./ai-test-utils');
const { aiDebugger } = require('./ai-debug-config');
const fs = require('fs');
const path = require('path');
/**
* 🤖 AI-Enhanced Intrinsic Testability Scoring Framework
*
* This advanced scorer leverages Playwright's AI capabilities for deeper analysis:
* - Smart element detection and classification
* - AI-powered accessibility analysis
* - Intelligent pattern recognition
* - Machine learning-based scoring adjustments
* - Predictive testability insights
* - Automated improvement recommendations
*/
class AITestabilityScorer {
constructor() {
this.timestamp = new Date().toISOString();
this.aiInsights = {
elementIntelligence: {},
accessibilityInsights: {},
patternRecognition: {},
predictiveAnalysis: {},
improvementSuggestions: []
};
this.aiMetrics = {
smartElementDetection: 0,
semanticAnalysis: 0,
accessibilityIntelligence: 0,
patternRecognition: 0,
predictiveInsights: 0
};
this.principleScores = {
observability: 0,
controllability: 0,
algorithmicSimplicity: 0,
algorithmicTransparency: 0,
algorithmicStability: 0,
explainability: 0,
unbugginess: 0,
smallness: 0,
decomposability: 0,
similarity: 0
};
}
/**
* 🤖 AI-Enhanced Observability Analysis
* Uses smart element detection to assess state visibility and monitoring capabilities
*/
async scoreAIObservability(page, debugSessionId) {
console.log('🤖 AI Observability Analysis Starting...');
let score = 0;
const insights = this.aiInsights.elementIntelligence;
try {
// AI-powered element discovery and classification
const smartElements = await this.discoverSmartElements(page);
insights.totalElements = smartElements.total;
insights.testableElements = smartElements.testable;
insights.observableElements = smartElements.observable;
// Smart data attribute analysis (30 points)
const dataTestScore = this.analyzeDataTestAttributes(smartElements.dataTest);
score += dataTestScore;
console.log(` 🎯 Smart data-test analysis: ${dataTestScore}/30`);
// AI state capture analysis (25 points)
const stateScore = await this.analyzeStateCapture(page);
score += stateScore;
console.log(` 🎯 AI state capture: ${stateScore}/25`);
// Intelligent error visibility (20 points)
const errorScore = await this.analyzeErrorVisibility(page, smartElements);
score += errorScore;
console.log(` 🎯 Error visibility intelligence: ${errorScore}/20`);
// AI visual analysis capabilities (15 points)
const visualScore = await this.analyzeVisualCapabilities(page);
score += visualScore;
console.log(` 🎯 AI visual analysis: ${visualScore}/15`);
// Smart network monitoring (10 points)
const networkScore = await this.analyzeNetworkMonitoring(debugSessionId);
score += networkScore;
console.log(` 🎯 Smart network monitoring: ${networkScore}/10`);
// AI bonus for intelligent patterns
const bonusScore = this.calculateIntelligenceBonus(insights);
score = Math.min(100, score + bonusScore);
console.log(`🤖 AI Observability Score: ${score}/100`);
return score;
} catch (error) {
console.log(`❌ AI Observability analysis failed: ${error.message}`);
return 20; // Fallback score
}
}
/**
* 🤖 AI-Enhanced Controllability Analysis
* Intelligent analysis of interaction patterns and control mechanisms
*/
async scoreAIControllability(page) {
console.log('🤖 AI Controllability Analysis Starting...');
let score = 0;
try {
// Smart interaction analysis
const interactionPatterns = await this.analyzeInteractionPatterns(page);
// AI-powered input precision analysis (30 points)
const inputScore = await this.analyzeInputPrecision(page, interactionPatterns);
score += inputScore;
console.log(` 🎯 AI input precision: ${inputScore}/30`);
// Intelligent state control assessment (25 points)
const stateControlScore = await this.analyzeStateControl(page, interactionPatterns);
score += stateControlScore;
console.log(` 🎯 Intelligent state control: ${stateControlScore}/25`);
// AI determinism analysis (20 points)
const determinismScore = await this.analyzeDeterminism(page);
score += determinismScore;
console.log(` 🎯 AI determinism analysis: ${determinismScore}/20`);
// Smart interaction reliability (15 points)
const reliabilityScore = await this.analyzeInteractionReliability(page, interactionPatterns);
score += reliabilityScore;
console.log(` 🎯 Smart interaction reliability: ${reliabilityScore}/15`);
// AI accessibility integration (10 points)
const accessibilityScore = await this.analyzeAccessibilityIntegration(page);
score += accessibilityScore;
console.log(` 🎯 AI accessibility integration: ${accessibilityScore}/10`);
console.log(`🤖 AI Controllability Score: ${score}/100`);
return score;
} catch (error) {
console.log(`❌ AI Controllability analysis failed: ${error.message}`);
return 25; // Fallback score
}
}
/**
* 🤖 AI-Enhanced Algorithmic Simplicity Analysis
* Uses pattern recognition to assess complexity and simplicity
*/
async scoreAIAlgorithmicSimplicity(page) {
console.log('🤖 AI Algorithmic Simplicity Analysis Starting...');
let score = 0;
try {
// AI complexity analysis
const complexityMetrics = await this.analyzeComplexity(page);
this.aiInsights.patternRecognition.complexity = complexityMetrics;
// Smart DOM complexity assessment (40 points)
const domScore = this.analyzeDOMComplexity(complexityMetrics);
score += domScore;
console.log(` 🎯 Smart DOM complexity: ${domScore}/40`);
// AI interaction pattern simplicity (30 points)
const patternScore = await this.analyzeInteractionPatternSimplicity(page);
score += patternScore;
console.log(` 🎯 AI interaction patterns: ${patternScore}/30`);
// Intelligent semantic structure (20 points)
const semanticScore = await this.analyzeSemanticStructure(page);
score += semanticScore;
console.log(` 🎯 Intelligent semantic structure: ${semanticScore}/20`);
// AI predictability assessment (10 points)
const predictabilityScore = this.analyzePredictability(complexityMetrics);
score += predictabilityScore;
console.log(` 🎯 AI predictability: ${predictabilityScore}/10`);
console.log(`🤖 AI Algorithmic Simplicity Score: ${score}/100`);
return score;
} catch (error) {
console.log(`❌ AI Algorithmic Simplicity analysis failed: ${error.message}`);
return 30; // Fallback score
}
}
/**
* 🤖 AI-Enhanced Explainability Analysis
* Deep semantic analysis and accessibility intelligence
*/
async scoreAIExplainability(page) {
console.log('🤖 AI Explainability Analysis Starting...');
let score = 0;
try {
// AI semantic analysis
const semanticIntelligence = await this.performSemanticAnalysis(page);
this.aiInsights.accessibilityInsights = semanticIntelligence;
// Smart semantic HTML analysis (30 points)
const semanticScore = this.analyzeSemanticHTML(semanticIntelligence);
score += semanticScore;
console.log(` 🎯 Smart semantic HTML: ${semanticScore}/30`);
// AI accessibility intelligence (25 points)
const accessibilityScore = await this.analyzeAccessibilityIntelligence(page, semanticIntelligence);
score += accessibilityScore;
console.log(` 🎯 AI accessibility intelligence: ${accessibilityScore}/25`);
// Intelligent labeling and descriptions (20 points)
const labelingScore = await this.analyzeLabelingIntelligence(page);
score += labelingScore;
console.log(` 🎯 Intelligent labeling: ${labelingScore}/20`);
// AI documentation analysis (15 points)
const documentationScore = await this.analyzeDocumentationIntelligence(page);
score += documentationScore;
console.log(` 🎯 AI documentation analysis: ${documentationScore}/15`);
// Smart context understanding (10 points)
const contextScore = this.analyzeContextUnderstanding(semanticIntelligence);
score += contextScore;
console.log(` 🎯 Smart context understanding: ${contextScore}/10`);
console.log(`🤖 AI Explainability Score: ${score}/100`);
return score;
} catch (error) {
console.log(`❌ AI Explainability analysis failed: ${error.message}`);
return 35; // Fallback score
}
}
/**
* 🤖 AI-Enhanced Decomposability Analysis
* Smart component analysis and modular design assessment
*/
async scoreAIDecomposability(page) {
console.log('🤖 AI Decomposability Analysis Starting...');
let score = 0;
try {
// AI component analysis
const componentIntelligence = await this.analyzeComponentIntelligence(page);
// Smart component separation (35 points)
const separationScore = this.analyzeComponentSeparation(componentIntelligence);
score += separationScore;
console.log(` 🎯 Smart component separation: ${separationScore}/35`);
// AI modularity assessment (30 points)
const modularityScore = await this.analyzeModularityIntelligence(page, componentIntelligence);
score += modularityScore;
console.log(` 🎯 AI modularity assessment: ${modularityScore}/30`);
// Intelligent isolation analysis (20 points)
const isolationScore = await this.analyzeIsolationIntelligence(page);
score += isolationScore;
console.log(` 🎯 Intelligent isolation: ${isolationScore}/20`);
// Smart testable boundaries (15 points)
const boundariesScore = this.analyzeTestableBoundaries(componentIntelligence);
score += boundariesScore;
console.log(` 🎯 Smart testable boundaries: ${boundariesScore}/15`);
console.log(`🤖 AI Decomposability Score: ${score}/100`);
return score;
} catch (error) {
console.log(`❌ AI Decomposability analysis failed: ${error.message}`);
return 40; // Fallback score
}
}
/**
* 🎯 Smart Element Discovery
* AI-powered element classification and analysis
*/
async discoverSmartElements(page) {
const elements = {
total: 0,
testable: 0,
observable: 0,
interactive: 0,
dataTest: [],
semantic: [],
accessibility: []
};
try {
// Count total elements
elements.total = await page.locator('*').count();
// Discover data-test elements with AI classification
const dataTestElements = await page.locator('[data-test]').all();
for (const element of dataTestElements) {
const testId = await element.getAttribute('data-test');
const tagName = await element.evaluate(el => el.tagName.toLowerCase());
const role = await element.getAttribute('role') || 'none';
elements.dataTest.push({
testId,
tagName,
role,
category: this.classifyElementCategory(testId, tagName, role)
});
}
// Count testable elements using smart detection
elements.testable = await page.locator('button, input, select, textarea, a[href], [data-test], [role="button"]').count();
// Count observable elements
elements.observable = await page.locator('[data-test], [aria-label], [role], img[alt]').count();
// Count interactive elements
elements.interactive = await page.locator('button, input, select, textarea, a[href]').count();
// Discover semantic elements
const semanticSelectors = ['nav', 'main', 'section', 'article', 'aside', 'header', 'footer'];
for (const selector of semanticSelectors) {
const count = await page.locator(selector).count();
if (count > 0) {
elements.semantic.push({ element: selector, count });
}
}
// Discover accessibility elements
const accessibilityElements = await page.locator('[aria-label], [role], [aria-labelledby]').count();
elements.accessibility.push({ type: 'aria-elements', count: accessibilityElements });
} catch (error) {
console.log(`⚠️ Smart element discovery error: ${error.message}`);
}
return elements;
}
/**
* 🤖 Element Category Classification
* AI-powered element categorization
*/
classifyElementCategory(testId, tagName, role) {
const categories = {
navigation: ['menu', 'nav', 'link', 'burger'],
form: ['username', 'password', 'email', 'input', 'submit', 'login'],
action: ['button', 'add', 'remove', 'click', 'submit'],
display: ['container', 'list', 'item', 'text', 'image'],
feedback: ['error', 'success', 'warning', 'message'],
control: ['toggle', 'switch', 'select', 'option']
};
const testIdLower = testId.toLowerCase();
for (const [category, keywords] of Object.entries(categories)) {
if (keywords.some(keyword => testIdLower.includes(keyword))) {
return category;
}
}
// Fallback to tag-based classification
const tagCategories = {
button: 'action',
input: 'form',
select: 'form',
a: 'navigation',
img: 'display',
div: 'display'
};
return tagCategories[tagName] || 'unknown';
}
/**
* 🎯 AI Data-Test Attributes Analysis
*/
analyzeDataTestAttributes(dataTestElements) {
if (dataTestElements.length === 0) return 5;
let score = 0;
const categories = {};
// Categorize elements
dataTestElements.forEach(element => {
categories[element.category] = (categories[element.category] || 0) + 1;
});
// Base score from quantity (up to 15 points)
score += Math.min(15, Math.floor(dataTestElements.length / 2));
// Quality bonus from categorization (up to 10 points)
const categoryCount = Object.keys(categories).length;
score += Math.min(10, categoryCount * 2);
// Consistency bonus (up to 5 points)
const hasConsistentNaming = dataTestElements.every(el =>
el.testId.includes('-') || el.testId.includes('_')
);
if (hasConsistentNaming) score += 5;
return Math.min(30, score);
}
/**
* 🤖 AI State Capture Analysis
*/
async analyzeStateCapture(page) {
try {
const stateCapabilities = await page.evaluate(() => {
return {
localStorage: typeof localStorage !== 'undefined' && localStorage.length > 0,
sessionStorage: typeof sessionStorage !== 'undefined' && sessionStorage.length > 0,
cookies: document.cookie.length > 0,
browserHistory: typeof history !== 'undefined',
pageState: {
url: window.location.href,
title: document.title,
readyState: document.readyState
},
customState: window.appState || window.store || window.state
};
});
let score = 0;
// Storage capabilities (10 points)
if (stateCapabilities.localStorage || stateCapabilities.sessionStorage) score += 10;
// Navigation state (5 points)
if (stateCapabilities.browserHistory) score += 5;
// Page state (5 points)
if (stateCapabilities.pageState.readyState === 'complete') score += 5;
// Custom state management (5 points)
if (stateCapabilities.customState) score += 5;
return Math.min(25, score);
} catch (error) {
return 10; // Fallback score
}
}
/**
* 🤖 AI Error Visibility Analysis
*/
async analyzeErrorVisibility(page, smartElements) {
try {
let score = 0;
// Check for error elements in smart elements
const errorElements = smartElements.dataTest.filter(el =>
el.category === 'feedback' || el.testId.includes('error')
);
if (errorElements.length > 0) score += 10;
// Check for visible error states
const visibleErrors = await page.locator('[data-test*="error"], .error, .alert').count();
if (visibleErrors > 0) score += 5;
// Check for console error handling
const consoleErrors = await page.evaluate(() => {
const errors = [];
const originalError = console.error;
console.error = (...args) => {
errors.push(args.join(' '));
originalError.apply(console, args);
};
return errors.length;
});
if (consoleErrors === 0) score += 5; // No console errors is good
return Math.min(20, score);
} catch (error) {
return 8; // Fallback score
}
}
/**
* 🤖 AI Visual Capabilities Analysis
*/
async analyzeVisualCapabilities(page) {
try {
let score = 0;
// Test screenshot capability
await page.screenshot({ path: 'temp-ai-visual-test.png' });
score += 8;
// Clean up test file
if (fs.existsSync('temp-ai-visual-test.png')) {
fs.unlinkSync('temp-ai-visual-test.png');
}
// Check for visual elements
const images = await page.locator('img').count();
if (images > 0) score += 4;
// Check for alt text coverage
const imagesWithAlt = await page.locator('img[alt]').count();
if (images > 0 && imagesWithAlt / images > 0.5) score += 3;
return Math.min(15, score);
} catch (error) {
return 5; // Fallback score
}
}
/**
* 🤖 Smart Network Monitoring Analysis
*/
async analyzeNetworkMonitoring(debugSessionId) {
try {
if (debugSessionId) {
// Use AI debugger to analyze network patterns
const session = aiDebugger.debugSessions?.get(debugSessionId);
if (session && session.networkRequests.length > 0) {
return 10; // Full score for active monitoring
}
}
return 5; // Partial score
} catch (error) {
return 3; // Fallback score
}
}
/**
* 🎯 Intelligence Bonus Calculation
*/
calculateIntelligenceBonus(insights) {
let bonus = 0;
// Bonus for high testable element ratio
if (insights.testableElements / insights.totalElements > 0.1) bonus += 2;
// Bonus for good observable element coverage
if (insights.observableElements / insights.totalElements > 0.05) bonus += 2;
// Bonus for diverse element categories
if (insights.observableElements > 10) bonus += 1;
return bonus;
}
/**
* 🤖 Interaction Patterns Analysis
*/
async analyzeInteractionPatterns(page) {
const patterns = {
buttons: await page.locator('button').count(),
inputs: await page.locator('input').count(),
selects: await page.locator('select').count(),
links: await page.locator('a[href]').count(),
interactive: await page.locator('[data-test]').count(),
total: 0
};
patterns.total = patterns.buttons + patterns.inputs + patterns.selects + patterns.links;
return patterns;
}
/**
* 🤖 AI Input Precision Analysis
*/
async analyzeInputPrecision(page, patterns) {
try {
let score = 0;
// Test input field precision
const inputs = await page.locator('input[type="text"], input[type="password"]').all();
if (inputs.length > 0) {
const testInput = inputs[0];
const testValue = 'ai_precision_test_123';
await testInput.fill(testValue);
const actualValue = await testInput.inputValue();
if (actualValue === testValue) {
score += 20; // High precision
} else if (actualValue.includes('ai_precision')) {
score += 10; // Partial precision
}
// Clear test value
await testInput.clear();
}
// Bonus for form validation
const requiredInputs = await page.locator('input[required]').count();
if (requiredInputs > 0) score += 5;
// Bonus for input types
const typedInputs = await page.locator('input[type]:not([type="text"])').count();
if (typedInputs > 0) score += 5;
return Math.min(30, score);
} catch (error) {
return 10; // Fallback score
}
}
/**
* 🤖 Intelligent State Control Assessment
*/
async analyzeStateControl(page, patterns) {
try {
let score = 0;
// Check button states
const buttons = await page.locator('button').all();
let enabledButtons = 0;
for (const button of buttons.slice(0, 5)) { // Check first 5 buttons
const isEnabled = await button.isEnabled();
if (isEnabled) enabledButtons++;
}
if (enabledButtons > 0) score += 15;
// Check form control
const forms = await page.locator('form').count();
if (forms > 0) score += 5;
// Check interactive elements
if (patterns.interactive > 5) score += 5;
return Math.min(25, score);
} catch (error) {
return 10; // Fallback score
}
}
/**
* 🤖 AI Determinism Analysis
*/
async analyzeDeterminism(page) {
try {
let score = 0;
// Test page reload consistency
const url1 = page.url();
await page.reload({ waitUntil: 'load' });
const url2 = page.url();
if (url1 === url2) score += 10;
// Test element consistency
const elementCount1 = await page.locator('[data-test]').count();
await page.waitForTimeout(1000);
const elementCount2 = await page.locator('[data-test]').count();
if (elementCount1 === elementCount2) score += 10;
return Math.min(20, score);
} catch (error) {
return 8; // Fallback score
}
}
/**
* 🤖 Smart Interaction Reliability Analysis
*/
async analyzeInteractionReliability(page, patterns) {
try {
let score = 0;
// Test click reliability
const buttons = await page.locator('button[data-test]').all();
if (buttons.length > 0) {
try {
await buttons[0].hover();
score += 8;
} catch (error) {
score += 3;
}
}
// Check for loading states
const loadingElements = await page.locator('[class*="loading"], [aria-label*="loading"]').count();
if (loadingElements === 0) score += 4; // No loading states is good for reliability
// Check for disabled states
const disabledElements = await page.locator('[disabled]').count();
if (disabledElements < patterns.total * 0.5) score += 3; // Most elements should be enabled
return Math.min(15, score);
} catch (error) {
return 5; // Fallback score
}
}
/**
* 🤖 AI Accessibility Integration Analysis
*/
async analyzeAccessibilityIntegration(page) {
try {
let score = 0;
// Check ARIA labels
const ariaLabels = await page.locator('[aria-label]').count();
if (ariaLabels > 0) score += 4;
// Check roles
const roles = await page.locator('[role]').count();
if (roles > 0) score += 3;
// Check focus management
const focusableElements = await page.locator('button, input, select, textarea, a[href]').count();
if (focusableElements > 0) score += 3;
return Math.min(10, score);
} catch (error) {
return 3; // Fallback score
}
}
/**
* 🤖 Complexity Analysis
*/
async analyzeComplexity(page) {
try {
const metrics = await page.evaluate(() => {
return {
totalElements: document.querySelectorAll('*').length,
divElements: document.querySelectorAll('div').length,
nestedLevels: Math.max(...Array.from(document.querySelectorAll('*')).map(el => {
let level = 0;
let current = el;
while (current.parentElement) {
level++;
current = current.parentElement;
}
return level;
})),
scriptTags: document.querySelectorAll('script').length,
styleTags: document.querySelectorAll('style, link[rel="stylesheet"]').length
};
});
return metrics;
} catch (error) {
return { totalElements: 100, divElements: 30, nestedLevels: 10, scriptTags: 5, styleTags: 3 };
}
}
/**
* 🤖 DOM Complexity Analysis
*/
analyzeDOMComplexity(metrics) {
let score = 40; // Start with full score
// Penalize high element count
if (metrics.totalElements > 1000) score -= 15;
else if (metrics.totalElements > 500) score -= 8;
else if (metrics.totalElements > 200) score -= 3;
// Penalize deep nesting
if (metrics.nestedLevels > 15) score -= 10;
else if (metrics.nestedLevels > 10) score -= 5;
// Penalize div soup
const divRatio = metrics.divElements / metrics.totalElements;
if (divRatio > 0.4) score -= 8;
else if (divRatio > 0.3) score -= 4;
return Math.max(0, score);
}
/**
* 🤖 Interaction Pattern Simplicity Analysis
*/
async analyzeInteractionPatternSimplicity(page) {
try {
let score = 30; // Start with full score
// Check for complex interactions
const complexSelectors = await page.locator('[onclick], [onchange], [onsubmit]').count();
score -= Math.min(10, complexSelectors * 2);
// Check for simple data-test patterns
const simpleDataTest = await page.locator('[data-test]:not([data-test*="-"]):not([data-test*="_"])').count();
if (simpleDataTest > 5) score += 5;
return Math.max(0, score);
} catch (error) {
return 15; // Fallback score
}
}
/**
* 🤖 Semantic Structure Analysis
*/
async analyzeSemanticStructure(page) {
try {
let score = 0;
const semanticElements = await page.evaluate(() => {
const semantic = ['nav', 'main', 'section', 'article', 'aside', 'header', 'footer'];
return semantic.reduce((acc, tag) => {
acc[tag] = document.querySelectorAll(tag).length;
return acc;
}, {});
});
const semanticCount = Object.values(semanticElements).reduce((sum, count) => sum + count, 0);
score = Math.min(20, semanticCount * 3);
return score;
} catch (error) {
return 8; // Fallback score
}
}
/**
* 🤖 Predictability Assessment
*/
analyzePredictability(metrics) {
let score = 10; // Start with full score
// High script count reduces predictability
if (metrics.scriptTags > 20) score -= 4;
else if (metrics.scriptTags > 10) score -= 2;
// High complexity reduces predictability
if (metrics.totalElements > 1000) score -= 3;
return Math.max(0, score);
}
/**
* 🤖 Semantic Analysis
*/
async performSemanticAnalysis(page) {
try {
const analysis = await page.evaluate(() => {
const results = {
semanticElements: {},
headingStructure: {},
landmarks: {},
ariaElements: {},
labelAssociations: 0
};
// Semantic elements
const semantic = ['nav', 'main', 'section', 'article', 'aside', 'header', 'footer'];
semantic.forEach(tag => {
results.semanticElements[tag] = document.querySelectorAll(tag).length;
});
// Heading structure
for (let i = 1; i <= 6; i++) {
results.headingStructure[`h${i}`] = document.querySelectorAll(`h${i}`).length;
}
// ARIA landmarks
const landmarks = document.querySelectorAll('[role="banner"], [role="navigation"], [role="main"], [role="contentinfo"]');
results.landmarks.count = landmarks.length;
// ARIA elements
results.ariaElements.labels = document.querySelectorAll('[aria-label]').length;
results.ariaElements.describedBy = document.querySelectorAll('[aria-describedby]').length;
results.ariaElements.labelledBy = document.querySelectorAll('[aria-labelledby]').length;
// Label associations
const labels = document.querySelectorAll('label[for]');
results.labelAssociations = labels.length;
return results;
});
return analysis;
} catch (error) {
return { semanticElements: {}, headingStructure: {}, landmarks: {}, ariaElements: {}, labelAssociations: 0 };
}
}
/**
* 🤖 Run Complete AI Testability Assessment
*/
async runAIAssessment(page, userType) {
console.log(`\n🤖 AI-Enhanced Testability Assessment for: ${userType}`);
console.log('=' .repeat(80));
try {
// Start AI debugging session
const debugSessionId = await aiDebugger.startDebugSession(`ai-testability-${userType}`, page);
// Navigate and login
await page.goto('https://www.saucedemo.com/', { timeout: 30000 });
await AITestUtils.waitForPageReady(page);
await page.locator('[data-test="username"]').fill(userType);
await page.locator('[data-test="password"]').fill('secret_sauce');
await page.locator('[data-test="login-button"]').click();
// Verify login success
const loginSuccess = await page.locator('[data-test="inventory-container"]').isVisible({ timeout: 10000 }).catch(() => false);
if (!loginSuccess) {
console.log(`⚠️ ${userType}: Login failed or blocked`);
return { userType, error: 'Login failed', scores: {} };
}
console.log(`✅ ${userType}: Successfully logged in`);
// Run AI-enhanced scoring
this.principleScores.observability = await this.scoreAIObservability(page, debugSessionId);
this.principleScores.controllability = await this.scoreAIControllability(page);
this.principleScores.algorithmicSimplicity = await this.scoreAIAlgorithmicSimplicity(page);
this.principleScores.explainability = await this.scoreAIExplainability(page);
this.principleScores.decomposability = await this.scoreAIDecomposability(page);
// Calculate overall score
const overallScore = Math.round(
Object.values(this.principleScores).reduce((sum, score) => sum + score, 0) /
Object.values(this.principleScores).length
);
// Generate AI insights and recommendations
await this.generateAIInsights(page, overallScore);
console.log(`\n🤖 AI OVERALL SCORE for ${userType}: ${overallScore}/100`);
console.log('=' .repeat(80));
// Clean up debug session
aiDebugger.endDebugSession(debugSessionId);
return {
userType,
timestamp: this.timestamp,
overallScore,
principleScores: { ...this.principleScores },
aiInsights: { ...this.aiInsights },
aiMetrics: { ...this.aiMetrics },
grade: this.getAIGrade(overallScore),
recommendations: this.aiInsights.improvementSuggestions
};
} catch (error) {
console.log(`❌ AI Assessment error for ${userType}: ${error.message}`);
return { userType, error: error.message, scores: {} };
}
}
/**
* 🤖 Generate AI Insights and Recommendations
*/
async generateAIInsights(page, overallScore) {
const insights = this.aiInsights;
// Generate improvement suggestions based on AI analysis
if (this.principleScores.observability < 70) {
insights.improvementSuggestions.push({
principle: 'Observability',
priority: 'High',
suggestion: 'Add more data-test attributes and implement comprehensive state monitoring',
aiReasoning: 'AI detected limited element observability and state capture capabilities'
});
}
if (this.principleScores.controllability < 70) {
insights.improvementSuggestions.push({
principle: 'Controllability',
priority: 'High',
suggestion: 'Improve interaction patterns and ensure reliable element state control',
aiReasoning: 'AI analysis shows inconsistent interaction patterns and state management'
});
}
if (this.principleScores.explainability < 70) {
insights.improvementSuggestions.push({
principle: 'Explainability',
priority: 'Medium',
suggestion: 'Enhance semantic HTML structure and accessibility attributes',
aiReasoning: 'AI semantic analysis indicates limited contextual understanding capabilities'
});
}
// Add AI-specific insights
insights.predictiveAnalysis = {
testMaintainability: overallScore > 80 ? 'High' : overallScore > 60 ? 'Medium' : 'Low',
futureComplexity: this.principleScores.algorithmicSimplicity > 70 ? 'Manageable' : 'Challenging',