/** * Test script for intelligent search selector discovery * This script tests the enhanced search functionality with intelligent selector fallbacks */ const { chromium } = require('playwright'); async function testIntelligentSearchSelectors() { console.log('๐งช Testing Intelligent Search Selector Discovery...'); const browser = await chromium.launch({ headless: false, args: ['--disable-web-security', '--disable-features=VizDisplayCompositor'] }); const context = await browser.newContext(); const page = await context.newPage(); try { // Navigate to Google console.log('๐ Navigating to Google...'); await page.goto('https://www.google.com'); await page.waitForLoadState('networkidle'); // Perform a search console.log('๐ Performing search...'); const searchBox = await page.locator('textarea[name="q"], input[name="q"]').first(); await searchBox.fill('intelligent selector discovery test'); await searchBox.press('Enter'); // Wait for results await page.waitForLoadState('networkidle'); await page.waitForTimeout(3000); // Inject our enhanced search helper console.log('๐ Injecting enhanced search helper...'); await page.addScriptTag({ path: './app/chrome-extension/inject-scripts/enhanced-search-helper.js' }); // Test the intelligent selector discovery console.log('๐ง Testing intelligent selector discovery...'); const results = await page.evaluate(async () => { // Call the extractSearchResults function from our injected script if (typeof extractSearchResults === 'function') { return await extractSearchResults(5); } else { throw new Error('extractSearchResults function not found'); } }); console.log('๐ Results:', JSON.stringify(results, null, 2)); // Validate results if (results.success && results.results.length > 0) { console.log('โ SUCCESS: Intelligent selector discovery found', results.results.length, 'results'); console.log('๐ฏ Selector used:', results.selectorUsed); console.log('๐ง Method:', results.method); // Display first few results results.results.slice(0, 3).forEach((result, index) => { console.log(`\n๐ Result ${index + 1}:`); console.log(` Title: ${result.title}`); console.log(` URL: ${result.url}`); console.log(` Snippet: ${result.snippet.substring(0, 100)}...`); }); } else { console.log('โ FAILED: No results found'); console.log('Error:', results.error); } // Test with a page that might have different structure console.log('\n๐ Testing with DuckDuckGo for different page structure...'); await page.goto('https://duckduckgo.com'); await page.waitForLoadState('networkidle'); const ddgSearchBox = await page.locator('input[name="q"]').first(); await ddgSearchBox.fill('intelligent selector test'); await ddgSearchBox.press('Enter'); await page.waitForLoadState('networkidle'); await page.waitForTimeout(3000); const ddgResults = await page.evaluate(async () => { if (typeof extractSearchResults === 'function') { return await extractSearchResults(3); } else { throw new Error('extractSearchResults function not found'); } }); console.log('๐ DuckDuckGo Results:', JSON.stringify(ddgResults, null, 2)); if (ddgResults.success && ddgResults.results.length > 0) { console.log('โ SUCCESS: Intelligent discovery works on DuckDuckGo too!'); console.log('๐ฏ Selector used:', ddgResults.selectorUsed); } else { console.log('โ ๏ธ DuckDuckGo test failed, but this might be expected due to different structure'); } } catch (error) { console.error('โ Test failed:', error); } finally { await browser.close(); } } async function testSelectorValidation() { console.log('\n๐งช Testing Selector Validation Functions...'); const browser = await chromium.launch({ headless: true }); const context = await browser.newContext(); const page = await context.newPage(); try { // Create a test page with various elements await page.setContent(`