/** * 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(`

Test Result 1

Example Link

This is a test snippet with substantial content to validate.

Short

Test Result 2

Test Link
Another test snippet with enough content to pass validation checks.
`); // Inject our helper functions await page.addScriptTag({ path: './app/chrome-extension/inject-scripts/enhanced-search-helper.js' }); // Test validation function const validationResults = await page.evaluate(() => { const elements = document.querySelectorAll('div'); const results = []; elements.forEach((el, index) => { const isValid = validateSearchResultElement(el); const extracted = extractResultFromElement(el, index + 1); results.push({ className: el.className, isValid, extracted }); }); return results; }); console.log('๐Ÿ” Validation Results:'); validationResults.forEach((result, index) => { console.log(`Element ${index + 1} (${result.className}):`); console.log(` Valid: ${result.isValid}`); console.log(` Extracted: ${result.extracted ? 'Yes' : 'No'}`); if (result.extracted) { console.log(` Title: ${result.extracted.title}`); console.log(` URL: ${result.extracted.url}`); } }); } catch (error) { console.error('โŒ Validation test failed:', error); } finally { await browser.close(); } } // Run tests async function runAllTests() { console.log('๐Ÿš€ Starting Intelligent Search Selector Tests...\n'); await testIntelligentSearchSelectors(); await testSelectorValidation(); console.log('\nโœจ All tests completed!'); } // Check if this script is being run directly if (require.main === module) { runAllTests().catch(console.error); } module.exports = { testIntelligentSearchSelectors, testSelectorValidation, runAllTests };