import puppeteer from 'brave-real-puppeteer-core'; import { Logger } from '../src/logger'; import assert from 'assert'; import * as fs from 'fs'; import * as path from 'path'; const log = new Logger(); const LOG_FILE = path.join(process.cwd(), 'execution-log.txt'); function logToFile(msg: string) { fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] ${msg}\n`); console.log(msg); // Keep console log too } async function runVisualTest() { // Clear old log if (fs.existsSync(LOG_FILE)) fs.unlinkSync(LOG_FILE); logToFile('Starting Visual Test for Brave Real Blocker...'); try { // Use brave-real-puppeteer-core for testing const browser = await puppeteer.launch({ headless: false, args: [] }); const page = await browser.newPage(); logToFile('Browser launched with Blocker enabled'); // 1. Test Bot Detection logToFile('Testing Bot Detection (SannySoft)...'); await page.goto('https://bot.sannysoft.com/'); await new Promise(r => setTimeout(r, 5000)); const failedTests = await page.evaluate(() => { const failed = document.querySelectorAll('.failed'); return Array.from(failed).map(el => el.textContent); }); if (failedTests.length === 0) { logToFile('✅ Passed SannySoft Bot Detection'); } else { logToFile(`⚠️ Failed some detection tests: ${failedTests.join(', ')}`); await page.screenshot({ path: 'sannysoft-failure.png', fullPage: true }); const html = await page.content(); fs.writeFileSync('sannysoft-debug.html', html); } // 2. Test Ad Blocking (e.g., d3ward adblock test) // 2. Test Ad Blocking log.info('Test', 'Testing Ad Blocking efficiency...'); // 2. Test Ad Blocking & Privacy logToFile('Testing Ad Blocking & Privacy...'); // Verify AdBlock Tester Score try { const site = 'https://adblock-tester.com/'; logToFile(`Visiting ${site} and extracting score...`); await page.goto(site, { waitUntil: 'domcontentloaded' }); // Wait for the score to appear (it takes time as tests run) // Wait for the score to appear try { // Wait for text "points out of 100" await page.waitForFunction(() => { return document.body.innerText.includes('points out of 100'); }, { timeout: 35000 }); } catch (e) { logToFile('Wait for score text failed, continuing check...'); } // Capture screenshot for debugging await page.screenshot({ path: 'adblock-tester.png', fullPage: true }); logToFile('Captured adblock-tester.png'); // Dump HTML for debugging const html = await page.content(); fs.writeFileSync('adblock-debug.html', html); logToFile('Dumped adblock-debug.html'); const pageData = await page.evaluate(() => { const bodyText = document.body.innerText; const match = bodyText.match(/(\d+)\s+points out of 100/); const scoreText = match ? match[0] : null; // Generic Fallback const matchGeneric = bodyText.match(/(\d+)\/100/); const genericScore = matchGeneric ? matchGeneric[0] : null; return { scoreText, genericScore, title: document.title }; }); const finalScore = pageData.scoreText || pageData.genericScore || 'Unknown'; logToFile(`AdBlock Tester Score: ${finalScore} (Title: ${pageData.title})`); // Analyze specific failures if score is not 100/100 (Optional enhancement) } catch (e) { log.error('Test', `Failed to verify adblock-tester.com: ${e}`); } // Verify CanYouBlockIt try { const site = 'https://canyoublockit.com/extreme-test/'; logToFile(`Visiting ${site}...`); await page.goto(site, { waitUntil: 'domcontentloaded' }); // In a real visual test, we might check if specific ad banners are effectively hidden // For now, we mainly load it to visually confirm during the "visual" run // checking for a known ad element that SHOULD be hidden const adVisible = await page.evaluate(() => { const ad = document.querySelector('#banner-advertisement'); // Example selector return ad && (ad as HTMLElement).offsetParent !== null; }); if (adVisible) { logToFile('⚠️ Ad banner detected on CanYouBlockIt'); } else { logToFile('✅ CanYouBlockIt seems clean (basic check)'); } } catch (e) { logToFile(`Failed to verify canyoublockit.com: ${e}`); } // Verify TurtleCute AdBlock Test (d3ward fork - 133 tests) try { const site = 'https://adblock.turtlecute.org/'; logToFile(`Visiting ${site} (TurtleCute - 133 tests)...`); await page.goto(site, { waitUntil: 'networkidle2', timeout: 60000 }); await new Promise(r => setTimeout(r, 10000)); // Wait for tests to complete const turtleResult = await page.evaluate(() => { const bodyText = document.body.innerText; // Extract blocked/not blocked counts const blockedMatch = bodyText.match(/(\d+)\s*blocked/i); const notBlockedMatch = bodyText.match(/(\d+)\s*not blocked/i); const totalMatch = bodyText.match(/Total\s*:\s*(\d+)/i); return { blocked: blockedMatch ? parseInt(blockedMatch[1]) : 0, notBlocked: notBlockedMatch ? parseInt(notBlockedMatch[1]) : 0, total: totalMatch ? parseInt(totalMatch[1]) : 133 }; }); const percentage = Math.round((turtleResult.blocked / turtleResult.total) * 100); logToFile(`TurtleCute Score: ${turtleResult.blocked}/${turtleResult.total} blocked (${percentage}%)`); if (percentage >= 80) { logToFile('✅ TurtleCute AdBlock Test: PASSED'); } else { logToFile(`⚠️ TurtleCute AdBlock Test: ${percentage}% (needs improvement)`); } await page.screenshot({ path: 'turtlecute-adblock.png', fullPage: true }); } catch (e) { logToFile(`Failed to verify adblock.turtlecute.org: ${e}`); } // Verify Obfusgated AdBlock Test (359 domains) try { const site = 'https://obfusgated.com/tools/ad-block-test'; logToFile(`Visiting ${site} (Obfusgated - 359 domains)...`); await page.goto(site, { waitUntil: 'domcontentloaded', timeout: 60000 }); await new Promise(r => setTimeout(r, 30000)); // Wait for all domain tests to complete const obfusgatedResult = await page.evaluate(() => { const bodyText = document.body.innerText; // Extract percentage const percentMatch = bodyText.match(/(\d+)%/); // Extract domains tested const domainsMatch = bodyText.match(/(\d+)\s*of\s*(\d+)\s*domains/i); // Extract blocked count const blockedMatch = bodyText.match(/(\d+)\s*blocked/i); return { percentage: percentMatch ? parseInt(percentMatch[1]) : 0, domainsTested: domainsMatch ? parseInt(domainsMatch[1]) : 0, totalDomains: domainsMatch ? parseInt(domainsMatch[2]) : 359, blocked: blockedMatch ? parseInt(blockedMatch[1]) : 0 }; }); logToFile(`Obfusgated Score: ${obfusgatedResult.percentage}% (${obfusgatedResult.domainsTested}/${obfusgatedResult.totalDomains} domains tested)`); if (obfusgatedResult.percentage >= 70) { logToFile('✅ Obfusgated AdBlock Test: PASSED'); } else { logToFile(`⚠️ Obfusgated AdBlock Test: ${obfusgatedResult.percentage}% (needs improvement)`); } await page.screenshot({ path: 'obfusgated-adblock.png', fullPage: true }); } catch (e) { logToFile(`Failed to verify obfusgated.com: ${e}`); } // 3. Test Fingerprinting logToFile('Testing Fingerprinting protection...'); logToFile('Visiting Canvas Fingerprinting (https://browserleaks.com/canvas)...'); await page.goto('https://browserleaks.com/canvas', { waitUntil: 'networkidle2' }); await new Promise(r => setTimeout(r, 3000)); try { const canvasResult = await page.evaluate(() => { const signature = document.querySelector('#crc')?.textContent || 'Unknown'; const unique = document.querySelector('#uniqueness')?.textContent || 'Unknown'; return { signature, unique }; }); logToFile(` Canvas Signature: ${canvasResult.signature}`); logToFile(` Uniqueness: ${canvasResult.unique}`); } catch (e) { logToFile(' ⚠️ Could not extract Canvas details'); } logToFile('Visiting AmIUnique (https://amiunique.org/fingerprint)...'); await page.goto('https://amiunique.org/fingerprint', { waitUntil: 'domcontentloaded' }); // Click the button to start fingerprinting if needed, or wait for auto-scann try { const btn = await page.$('#start_fingerprint'); // Selector might vary if (btn) await btn.click(); } catch (e) { } await new Promise(r => setTimeout(r, 10000)); // Wait for analysis try { // Updated selectors for AmIUnique new UI const amiResult = await page.evaluate(() => { const uniqueText = document.body.innerText.includes('You are unique') ? 'Yes' : 'No'; return uniqueText; }); logToFile(` AmIUnique Status: ${amiResult === 'Yes' ? '⚠️ Unique (Trackable)' : '✅ Not Unique (Blends in)'}`); } catch (e) { logToFile(' ⚠️ Could not extract AmIUnique details'); } // Fix Permissions: Override permissions to ensure prompts don't fail tests // This is crucial for "Permission prompt" tests which expect auto-block/deny const context = browser.defaultBrowserContext(); await context.overridePermissions('https://rowserleaks.com', []); // Deny all await context.overridePermissions('https://adblock-tester.com', []); // 4. Verify Scriptlet Injection logToFile('Verifying Scriptlet Injection...'); const isWindowOpenWrapped = await page.evaluate(() => { // Our wrapper returns null for blocked popups // Test by trying to open about:blank (which should be blocked) const testResult = window.open('about:blank', '_blank'); const isBlocked = testResult === null; if (testResult) testResult.close(); // Also check if the function has been modified const fnStr = window.open.toString(); const isWrapped = !fnStr.includes('[native code]') || fnStr.length > 50; return isBlocked || isWrapped; }); if (isWindowOpenWrapped) { logToFile('✅ Window.open is wrapped/intercepted'); } else { logToFile('⚠️ Window.open is NOT wrapped'); } await new Promise(r => setTimeout(r, 2000)); await browser.close(); logToFile('Visual Test Completed Successfully'); process.exit(0); } catch (error) { logToFile(`Test Failed: ${error}`); process.exit(1); } } runVisualTest();