cbrowser 6.1.0 → 6.2.0

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/dist/browser.js CHANGED
@@ -21,6 +21,10 @@ exports.parseNLTestSuite = parseNLTestSuite;
21
21
  exports.runNLTestSuite = runNLTestSuite;
22
22
  exports.formatNLTestReport = formatNLTestReport;
23
23
  exports.runNLTestFile = runNLTestFile;
24
+ exports.repairTest = repairTest;
25
+ exports.repairTestSuite = repairTestSuite;
26
+ exports.formatRepairReport = formatRepairReport;
27
+ exports.exportRepairedTest = exportRepairedTest;
24
28
  const playwright_1 = require("playwright");
25
29
  const fs_1 = require("fs");
26
30
  const path_1 = require("path");
@@ -3297,4 +3301,521 @@ async function runNLTestFile(filepath, options = {}) {
3297
3301
  const suite = parseNLTestSuite(content, suiteName);
3298
3302
  return runNLTestSuite(suite, options);
3299
3303
  }
3304
+ // =========================================================================
3305
+ // AI Test Repair (v6.2.0)
3306
+ // =========================================================================
3307
+ /**
3308
+ * Classify the type of failure from an error message.
3309
+ */
3310
+ function classifyFailure(error, step) {
3311
+ const lowerError = error.toLowerCase();
3312
+ if (lowerError.includes("not found") || lowerError.includes("no element") || lowerError.includes("failed to click")) {
3313
+ return "selector_not_found";
3314
+ }
3315
+ if (lowerError.includes("assertion") || lowerError.includes("verify") || lowerError.includes("expected")) {
3316
+ return "assertion_failed";
3317
+ }
3318
+ if (lowerError.includes("timeout") || lowerError.includes("timed out")) {
3319
+ return "timeout";
3320
+ }
3321
+ if (lowerError.includes("navigation") || lowerError.includes("navigate") || lowerError.includes("url")) {
3322
+ return "navigation_failed";
3323
+ }
3324
+ if (lowerError.includes("not interactable") || lowerError.includes("disabled") || lowerError.includes("hidden")) {
3325
+ return "element_not_interactable";
3326
+ }
3327
+ return "unknown";
3328
+ }
3329
+ /**
3330
+ * Find alternative selectors for a target on the current page.
3331
+ */
3332
+ async function findAlternatives(browser, originalTarget) {
3333
+ const page = browser.page;
3334
+ if (!page)
3335
+ return [];
3336
+ try {
3337
+ const alternatives = await page.evaluate((target) => {
3338
+ const results = [];
3339
+ const lowerTarget = target.toLowerCase();
3340
+ // Find buttons with similar text
3341
+ document.querySelectorAll("button, [role='button'], input[type='submit'], input[type='button']").forEach((el) => {
3342
+ const text = el.textContent?.trim() || el.value || "";
3343
+ if (text && (text.toLowerCase().includes(lowerTarget) || lowerTarget.includes(text.toLowerCase()))) {
3344
+ results.push(`button: "${text}"`);
3345
+ }
3346
+ });
3347
+ // Find links with similar text
3348
+ document.querySelectorAll("a").forEach((el) => {
3349
+ const text = el.textContent?.trim() || "";
3350
+ if (text && (text.toLowerCase().includes(lowerTarget) || lowerTarget.includes(text.toLowerCase()))) {
3351
+ results.push(`link: "${text}"`);
3352
+ }
3353
+ });
3354
+ // Find inputs with similar labels/placeholders
3355
+ document.querySelectorAll("input, textarea, select").forEach((el) => {
3356
+ const input = el;
3357
+ const placeholder = input.placeholder || "";
3358
+ const label = document.querySelector(`label[for="${input.id}"]`)?.textContent?.trim() || "";
3359
+ const name = input.name || "";
3360
+ if (placeholder.toLowerCase().includes(lowerTarget) || lowerTarget.includes(placeholder.toLowerCase())) {
3361
+ results.push(`input with placeholder "${placeholder}"`);
3362
+ }
3363
+ if (label.toLowerCase().includes(lowerTarget) || lowerTarget.includes(label.toLowerCase())) {
3364
+ results.push(`input labeled "${label}"`);
3365
+ }
3366
+ if (name.toLowerCase().includes(lowerTarget)) {
3367
+ results.push(`input named "${name}"`);
3368
+ }
3369
+ });
3370
+ // Find elements by aria-label
3371
+ document.querySelectorAll("[aria-label]").forEach((el) => {
3372
+ const label = el.getAttribute("aria-label") || "";
3373
+ if (label.toLowerCase().includes(lowerTarget) || lowerTarget.includes(label.toLowerCase())) {
3374
+ results.push(`aria:${el.tagName.toLowerCase()}/"${label}"`);
3375
+ }
3376
+ });
3377
+ return [...new Set(results)].slice(0, 10);
3378
+ }, originalTarget);
3379
+ return alternatives;
3380
+ }
3381
+ catch {
3382
+ return [];
3383
+ }
3384
+ }
3385
+ /**
3386
+ * Get page context for failure analysis.
3387
+ */
3388
+ async function getPageContext(browser) {
3389
+ const page = browser.page;
3390
+ if (!page)
3391
+ return { url: "", title: "", visibleText: [] };
3392
+ try {
3393
+ const context = await page.evaluate(() => {
3394
+ const visibleText = [];
3395
+ // Get visible button/link text
3396
+ document.querySelectorAll("button, a, [role='button']").forEach((el) => {
3397
+ const text = el.textContent?.trim();
3398
+ if (text && text.length < 50) {
3399
+ visibleText.push(text);
3400
+ }
3401
+ });
3402
+ return {
3403
+ url: window.location.href,
3404
+ title: document.title,
3405
+ visibleText: [...new Set(visibleText)].slice(0, 20),
3406
+ };
3407
+ });
3408
+ return context;
3409
+ }
3410
+ catch {
3411
+ return { url: "", title: "", visibleText: [] };
3412
+ }
3413
+ }
3414
+ /**
3415
+ * Generate repair suggestions for a failed step.
3416
+ */
3417
+ async function generateRepairSuggestions(browser, step, error, failureType, alternatives, pageContext) {
3418
+ const suggestions = [];
3419
+ switch (failureType) {
3420
+ case "selector_not_found": {
3421
+ // Suggest alternative selectors
3422
+ for (const alt of alternatives.slice(0, 3)) {
3423
+ const newInstruction = step.instruction.replace(step.target || "", alt.replace(/^(button|link|input|aria):\s*/, "").replace(/"/g, "'"));
3424
+ suggestions.push({
3425
+ type: "selector_update",
3426
+ confidence: 0.7,
3427
+ description: `Update selector to "${alt}"`,
3428
+ originalInstruction: step.instruction,
3429
+ suggestedInstruction: `click ${alt.replace(/^(button|link|input|aria):\s*/, "")}`,
3430
+ reasoning: `Found similar element on page: ${alt}`,
3431
+ });
3432
+ }
3433
+ // Suggest adding a wait
3434
+ if (suggestions.length === 0) {
3435
+ suggestions.push({
3436
+ type: "add_wait",
3437
+ confidence: 0.5,
3438
+ description: "Add wait before this step",
3439
+ originalInstruction: step.instruction,
3440
+ suggestedInstruction: `wait 2 seconds\n${step.instruction}`,
3441
+ reasoning: "Element might not be loaded yet - adding wait may help",
3442
+ });
3443
+ }
3444
+ break;
3445
+ }
3446
+ case "assertion_failed": {
3447
+ // Suggest updating the assertion based on page content
3448
+ if (step.action === "assert" && pageContext.visibleText.length > 0) {
3449
+ const possibleText = pageContext.visibleText.find((t) => t.length > 3 && t.length < 30);
3450
+ if (possibleText) {
3451
+ suggestions.push({
3452
+ type: "assertion_update",
3453
+ confidence: 0.6,
3454
+ description: `Update assertion to check for visible text`,
3455
+ originalInstruction: step.instruction,
3456
+ suggestedInstruction: `verify page contains "${possibleText}"`,
3457
+ reasoning: `Page contains "${possibleText}" which might be the intended check`,
3458
+ });
3459
+ }
3460
+ }
3461
+ // Suggest checking URL instead
3462
+ if (pageContext.url) {
3463
+ const urlPath = new URL(pageContext.url).pathname;
3464
+ suggestions.push({
3465
+ type: "assertion_update",
3466
+ confidence: 0.5,
3467
+ description: "Assert URL instead of content",
3468
+ originalInstruction: step.instruction,
3469
+ suggestedInstruction: `verify url contains "${urlPath}"`,
3470
+ reasoning: `Current URL is ${pageContext.url}`,
3471
+ });
3472
+ }
3473
+ break;
3474
+ }
3475
+ case "timeout": {
3476
+ suggestions.push({
3477
+ type: "add_wait",
3478
+ confidence: 0.7,
3479
+ description: "Increase wait time",
3480
+ originalInstruction: step.instruction,
3481
+ suggestedInstruction: `wait 5 seconds\n${step.instruction}`,
3482
+ reasoning: "Operation timed out - page may need more time to load",
3483
+ });
3484
+ break;
3485
+ }
3486
+ case "element_not_interactable": {
3487
+ suggestions.push({
3488
+ type: "add_wait",
3489
+ confidence: 0.6,
3490
+ description: "Wait for element to become interactive",
3491
+ originalInstruction: step.instruction,
3492
+ suggestedInstruction: `wait 2 seconds\n${step.instruction}`,
3493
+ reasoning: "Element exists but is not interactable - may need to wait",
3494
+ });
3495
+ // Suggest scrolling
3496
+ suggestions.push({
3497
+ type: "change_action",
3498
+ confidence: 0.5,
3499
+ description: "Scroll element into view first",
3500
+ originalInstruction: step.instruction,
3501
+ suggestedInstruction: `scroll down\n${step.instruction}`,
3502
+ reasoning: "Element might be outside viewport",
3503
+ });
3504
+ break;
3505
+ }
3506
+ default: {
3507
+ // Generic suggestion to skip
3508
+ suggestions.push({
3509
+ type: "skip_step",
3510
+ confidence: 0.3,
3511
+ description: "Skip this step",
3512
+ originalInstruction: step.instruction,
3513
+ suggestedInstruction: `// SKIPPED: ${step.instruction}`,
3514
+ reasoning: "Unable to determine a fix - consider removing this step",
3515
+ });
3516
+ }
3517
+ }
3518
+ // Sort by confidence
3519
+ return suggestions.sort((a, b) => b.confidence - a.confidence);
3520
+ }
3521
+ /**
3522
+ * Analyze a failed test step and suggest repairs.
3523
+ */
3524
+ async function analyzeFailure(browser, step, error) {
3525
+ const failureType = classifyFailure(error, step);
3526
+ const alternatives = step.target ? await findAlternatives(browser, step.target) : [];
3527
+ const pageContext = await getPageContext(browser);
3528
+ const suggestions = await generateRepairSuggestions(browser, step, error, failureType, alternatives, pageContext);
3529
+ return {
3530
+ step,
3531
+ error,
3532
+ failureType,
3533
+ targetSelector: step.target,
3534
+ alternativeSelectors: alternatives,
3535
+ pageContext,
3536
+ suggestions,
3537
+ };
3538
+ }
3539
+ /**
3540
+ * Run a test, analyze failures, and suggest/apply repairs.
3541
+ */
3542
+ async function repairTest(test, options = {}) {
3543
+ const { headless = true, autoApply = false, verifyRepairs = true, maxRetries = 3, } = options;
3544
+ const browser = new CBrowser({ headless });
3545
+ const failureAnalyses = [];
3546
+ const repairedSteps = [];
3547
+ let failedSteps = 0;
3548
+ console.log(`\n🔧 Analyzing test: ${test.name}`);
3549
+ console.log(` Steps: ${test.steps.length}`);
3550
+ console.log(` Auto-apply: ${autoApply}`);
3551
+ console.log("");
3552
+ try {
3553
+ await browser.launch();
3554
+ for (const step of test.steps) {
3555
+ console.log(` → ${step.instruction}`);
3556
+ let stepPassed = false;
3557
+ let lastError = "";
3558
+ let attempts = 0;
3559
+ while (!stepPassed && attempts < maxRetries) {
3560
+ attempts++;
3561
+ try {
3562
+ // Execute the step
3563
+ switch (step.action) {
3564
+ case "navigate":
3565
+ await browser.navigate(step.target || "");
3566
+ stepPassed = true;
3567
+ break;
3568
+ case "click":
3569
+ const clickResult = await browser.smartClick(step.target || "");
3570
+ stepPassed = clickResult.success;
3571
+ if (!stepPassed)
3572
+ lastError = `Failed to click: ${step.target}`;
3573
+ break;
3574
+ case "fill":
3575
+ await browser.fill(step.target || "", step.value || "");
3576
+ stepPassed = true;
3577
+ break;
3578
+ case "assert":
3579
+ const assertResult = await browser.assert(step.instruction);
3580
+ stepPassed = assertResult.passed;
3581
+ if (!stepPassed)
3582
+ lastError = assertResult.message;
3583
+ break;
3584
+ case "wait":
3585
+ if (step.target) {
3586
+ const page = browser.page;
3587
+ if (page) {
3588
+ await page.waitForSelector(`text=${step.target}`, { timeout: 10000 });
3589
+ }
3590
+ }
3591
+ else {
3592
+ const ms = parseFloat(step.value || "1") * 1000;
3593
+ await new Promise((r) => setTimeout(r, ms));
3594
+ }
3595
+ stepPassed = true;
3596
+ break;
3597
+ case "scroll":
3598
+ const direction = step.target?.toLowerCase() === "up" ? -500 : 500;
3599
+ const page = browser.page;
3600
+ if (page) {
3601
+ await page.evaluate((d) => window.scrollBy(0, d), direction);
3602
+ }
3603
+ stepPassed = true;
3604
+ break;
3605
+ case "screenshot":
3606
+ await browser.screenshot();
3607
+ stepPassed = true;
3608
+ break;
3609
+ default:
3610
+ // Try as click
3611
+ const unknownResult = await browser.smartClick(step.target || step.instruction);
3612
+ stepPassed = unknownResult.success;
3613
+ if (!stepPassed)
3614
+ lastError = `Could not interpret: ${step.instruction}`;
3615
+ }
3616
+ }
3617
+ catch (e) {
3618
+ lastError = e.message;
3619
+ stepPassed = false;
3620
+ }
3621
+ if (!stepPassed && attempts < maxRetries) {
3622
+ console.log(` ⚠️ Attempt ${attempts} failed, retrying...`);
3623
+ await new Promise((r) => setTimeout(r, 500));
3624
+ }
3625
+ }
3626
+ if (stepPassed) {
3627
+ console.log(` ✓ Passed`);
3628
+ repairedSteps.push(step);
3629
+ }
3630
+ else {
3631
+ console.log(` ✗ Failed: ${lastError}`);
3632
+ failedSteps++;
3633
+ // Analyze the failure
3634
+ const analysis = await analyzeFailure(browser, step, lastError);
3635
+ failureAnalyses.push(analysis);
3636
+ if (autoApply && analysis.suggestions.length > 0) {
3637
+ const bestSuggestion = analysis.suggestions[0];
3638
+ console.log(` 🔧 Auto-applying: ${bestSuggestion.description}`);
3639
+ // Parse the suggested instruction into a step
3640
+ const repairedStep = parseNLInstruction(bestSuggestion.suggestedInstruction.split("\n").pop() || "");
3641
+ repairedSteps.push(repairedStep);
3642
+ }
3643
+ else {
3644
+ // Keep original step
3645
+ repairedSteps.push(step);
3646
+ if (analysis.suggestions.length > 0) {
3647
+ console.log(` 💡 Suggestions:`);
3648
+ for (const suggestion of analysis.suggestions.slice(0, 2)) {
3649
+ console.log(` - ${suggestion.description} (${Math.round(suggestion.confidence * 100)}%)`);
3650
+ console.log(` → ${suggestion.suggestedInstruction}`);
3651
+ }
3652
+ }
3653
+ }
3654
+ }
3655
+ }
3656
+ // Create repaired test
3657
+ const repairedTest = {
3658
+ name: test.name,
3659
+ description: test.description,
3660
+ steps: repairedSteps,
3661
+ };
3662
+ // Optionally verify the repaired test
3663
+ let repairedTestPasses;
3664
+ if (verifyRepairs && autoApply && failedSteps > 0) {
3665
+ console.log(`\n 🔄 Verifying repaired test...`);
3666
+ await browser.close();
3667
+ const verifyBrowser = new CBrowser({ headless });
3668
+ try {
3669
+ await verifyBrowser.launch();
3670
+ let allPassed = true;
3671
+ for (const step of repairedSteps) {
3672
+ try {
3673
+ switch (step.action) {
3674
+ case "navigate":
3675
+ await verifyBrowser.navigate(step.target || "");
3676
+ break;
3677
+ case "click":
3678
+ const result = await verifyBrowser.smartClick(step.target || "");
3679
+ if (!result.success)
3680
+ allPassed = false;
3681
+ break;
3682
+ case "fill":
3683
+ await verifyBrowser.fill(step.target || "", step.value || "");
3684
+ break;
3685
+ case "assert":
3686
+ const assertResult = await verifyBrowser.assert(step.instruction);
3687
+ if (!assertResult.passed)
3688
+ allPassed = false;
3689
+ break;
3690
+ case "wait":
3691
+ if (!step.target) {
3692
+ const ms = parseFloat(step.value || "1") * 1000;
3693
+ await new Promise((r) => setTimeout(r, ms));
3694
+ }
3695
+ break;
3696
+ }
3697
+ }
3698
+ catch {
3699
+ allPassed = false;
3700
+ }
3701
+ }
3702
+ repairedTestPasses = allPassed;
3703
+ console.log(` ${allPassed ? "✅" : "❌"} Repaired test ${allPassed ? "PASSES" : "still FAILS"}`);
3704
+ }
3705
+ finally {
3706
+ await verifyBrowser.close();
3707
+ }
3708
+ }
3709
+ return {
3710
+ originalTest: test,
3711
+ repairedTest: failedSteps > 0 ? repairedTest : undefined,
3712
+ failedSteps,
3713
+ repairedSteps: autoApply ? failedSteps : 0,
3714
+ failureAnalyses,
3715
+ repairedTestPasses,
3716
+ };
3717
+ }
3718
+ finally {
3719
+ await browser.close();
3720
+ }
3721
+ }
3722
+ /**
3723
+ * Run test repair on a full suite.
3724
+ */
3725
+ async function repairTestSuite(suite, options = {}) {
3726
+ const startTime = Date.now();
3727
+ const testResults = [];
3728
+ console.log(`\n🔧 Repairing Test Suite: ${suite.name}`);
3729
+ console.log(` Tests: ${suite.tests.length}`);
3730
+ console.log("");
3731
+ for (const test of suite.tests) {
3732
+ const result = await repairTest(test, options);
3733
+ testResults.push(result);
3734
+ }
3735
+ const testsWithFailures = testResults.filter((r) => r.failedSteps > 0).length;
3736
+ const testsRepaired = testResults.filter((r) => r.repairedSteps > 0).length;
3737
+ const totalFailedSteps = testResults.reduce((sum, r) => sum + r.failedSteps, 0);
3738
+ const totalRepairedSteps = testResults.reduce((sum, r) => sum + r.repairedSteps, 0);
3739
+ return {
3740
+ suiteName: suite.name,
3741
+ timestamp: new Date().toISOString(),
3742
+ duration: Date.now() - startTime,
3743
+ testResults,
3744
+ summary: {
3745
+ totalTests: suite.tests.length,
3746
+ testsWithFailures,
3747
+ testsRepaired,
3748
+ totalFailedSteps,
3749
+ totalRepairedSteps,
3750
+ repairSuccessRate: totalFailedSteps > 0 ? (totalRepairedSteps / totalFailedSteps) * 100 : 100,
3751
+ },
3752
+ };
3753
+ }
3754
+ /**
3755
+ * Format a repair result as a report.
3756
+ */
3757
+ function formatRepairReport(result) {
3758
+ const lines = [];
3759
+ lines.push("");
3760
+ lines.push("╔══════════════════════════════════════════════════════════════════════════════╗");
3761
+ lines.push("║ AI TEST REPAIR REPORT ║");
3762
+ lines.push("╚══════════════════════════════════════════════════════════════════════════════╝");
3763
+ lines.push("");
3764
+ lines.push(`📋 Suite: ${result.suiteName}`);
3765
+ lines.push(`⏱️ Duration: ${(result.duration / 1000).toFixed(1)}s`);
3766
+ lines.push(`📅 Timestamp: ${result.timestamp}`);
3767
+ lines.push("");
3768
+ // Summary
3769
+ lines.push("📊 SUMMARY");
3770
+ lines.push("─".repeat(60));
3771
+ lines.push(` Total Tests: ${result.summary.totalTests}`);
3772
+ lines.push(` Tests with Failures: ${result.summary.testsWithFailures}`);
3773
+ lines.push(` Tests Repaired: ${result.summary.testsRepaired}`);
3774
+ lines.push(` Total Failed Steps: ${result.summary.totalFailedSteps}`);
3775
+ lines.push(` Total Repaired Steps: ${result.summary.totalRepairedSteps}`);
3776
+ lines.push(` Repair Success Rate: ${result.summary.repairSuccessRate.toFixed(0)}%`);
3777
+ lines.push("");
3778
+ // Per-test details
3779
+ for (const testResult of result.testResults) {
3780
+ if (testResult.failedSteps === 0)
3781
+ continue;
3782
+ lines.push(`\n🔧 ${testResult.originalTest.name}`);
3783
+ lines.push(` Failed Steps: ${testResult.failedSteps}`);
3784
+ lines.push(` Repaired: ${testResult.repairedSteps}`);
3785
+ for (const analysis of testResult.failureAnalyses) {
3786
+ lines.push(`\n ❌ ${analysis.step.instruction}`);
3787
+ lines.push(` Error: ${analysis.error}`);
3788
+ lines.push(` Type: ${analysis.failureType}`);
3789
+ if (analysis.suggestions.length > 0) {
3790
+ lines.push(` 💡 Suggestions:`);
3791
+ for (const s of analysis.suggestions.slice(0, 2)) {
3792
+ lines.push(` - ${s.description} (${Math.round(s.confidence * 100)}%)`);
3793
+ lines.push(` → ${s.suggestedInstruction}`);
3794
+ }
3795
+ }
3796
+ }
3797
+ if (testResult.repairedTestPasses !== undefined) {
3798
+ lines.push(`\n ${testResult.repairedTestPasses ? "✅" : "❌"} Repaired test ${testResult.repairedTestPasses ? "PASSES" : "still FAILS"}`);
3799
+ }
3800
+ }
3801
+ lines.push("");
3802
+ return lines.join("\n");
3803
+ }
3804
+ /**
3805
+ * Export repaired test to file.
3806
+ */
3807
+ function exportRepairedTest(result) {
3808
+ if (!result.repairedTest) {
3809
+ return `# Test: ${result.originalTest.name}\n# No repairs needed\n${result.originalTest.steps.map((s) => s.instruction).join("\n")}`;
3810
+ }
3811
+ const lines = [];
3812
+ lines.push(`# Test: ${result.repairedTest.name} (Repaired)`);
3813
+ lines.push(`# Original failures: ${result.failedSteps}`);
3814
+ lines.push(`# Repairs applied: ${result.repairedSteps}`);
3815
+ lines.push("");
3816
+ for (const step of result.repairedTest.steps) {
3817
+ lines.push(step.instruction);
3818
+ }
3819
+ return lines.join("\n");
3820
+ }
3300
3821
  //# sourceMappingURL=browser.js.map