|
| 1 | +/** |
| 2 | + * E2E test for annotation button functionality |
| 3 | + * This test captures browser console errors and validates the button appears |
| 4 | + */ |
| 5 | + |
| 6 | +import { test, expect } from '@playwright/test'; |
| 7 | +import { loadExtension, getExtensionId } from '../helpers/extension'; |
| 8 | + |
| 9 | +test.describe('Annotation Button Feature', () => { |
| 10 | + let extensionId: string; |
| 11 | + let consoleErrors: string[] = []; |
| 12 | + let consoleWarnings: string[] = []; |
| 13 | + |
| 14 | + test.beforeEach(async ({ context }) => { |
| 15 | + // Load extension |
| 16 | + extensionId = await loadExtension(context); |
| 17 | + |
| 18 | + // Capture console errors and warnings |
| 19 | + context.on('page', (page) => { |
| 20 | + page.on('console', (msg) => { |
| 21 | + const text = msg.text(); |
| 22 | + if (msg.type() === 'error') { |
| 23 | + consoleErrors.push(text); |
| 24 | + console.error(`[Browser Error] ${text}`); |
| 25 | + } else if (msg.type() === 'warning') { |
| 26 | + consoleWarnings.push(text); |
| 27 | + console.warn(`[Browser Warning] ${text}`); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + // Capture uncaught exceptions |
| 32 | + page.on('pageerror', (error) => { |
| 33 | + consoleErrors.push(`Uncaught Exception: ${error.message}`); |
| 34 | + console.error(`[Uncaught Exception] ${error.message}\n${error.stack}`); |
| 35 | + }); |
| 36 | + |
| 37 | + // Capture failed requests |
| 38 | + page.on('requestfailed', (request) => { |
| 39 | + const failure = request.failure(); |
| 40 | + if (failure) { |
| 41 | + consoleErrors.push(`Request Failed: ${request.url()} - ${failure.errorText}`); |
| 42 | + } |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + consoleErrors = []; |
| 47 | + consoleWarnings = []; |
| 48 | + }); |
| 49 | + |
| 50 | + test('annotation button should appear when text is selected', async ({ context, page }) => { |
| 51 | + // Navigate to a test page |
| 52 | + await page.goto('https://example.com', { waitUntil: 'networkidle' }); |
| 53 | + |
| 54 | + // Wait for content script to load and check for errors |
| 55 | + await page.waitForTimeout(2000); |
| 56 | + |
| 57 | + // Check if content script loaded |
| 58 | + const contentScriptLoaded = await page.evaluate(() => { |
| 59 | + return typeof window !== 'undefined' && |
| 60 | + (window as any).__graphitiContentScriptLoaded === true; |
| 61 | + }); |
| 62 | + |
| 63 | + if (!contentScriptLoaded) { |
| 64 | + console.error('❌ Content script did not load'); |
| 65 | + if (consoleErrors.length > 0) { |
| 66 | + console.error('Errors that may have prevented loading:'); |
| 67 | + consoleErrors.forEach(err => console.error(` - ${err}`)); |
| 68 | + } |
| 69 | + } else { |
| 70 | + console.log('✅ Content script loaded'); |
| 71 | + } |
| 72 | + |
| 73 | + // Check if annotations are enabled |
| 74 | + const annotationsEnabled = await page.evaluate(async () => { |
| 75 | + return new Promise((resolve) => { |
| 76 | + if (typeof chrome !== 'undefined' && chrome.storage) { |
| 77 | + chrome.storage.local.get('annotationsEnabled', (result) => { |
| 78 | + resolve(result.annotationsEnabled !== false); |
| 79 | + }); |
| 80 | + } else { |
| 81 | + resolve(true); // Default to enabled |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + console.log(`Annotations enabled: ${annotationsEnabled}`); |
| 87 | + |
| 88 | + // Select text on the page - use a more reliable method |
| 89 | + await page.evaluate(() => { |
| 90 | + const selection = window.getSelection(); |
| 91 | + const range = document.createRange(); |
| 92 | + const p = document.querySelector('p'); |
| 93 | + if (p && p.firstChild) { |
| 94 | + range.setStart(p.firstChild, 0); |
| 95 | + range.setEnd(p.firstChild, Math.min(10, p.textContent?.length || 0)); |
| 96 | + selection?.removeAllRanges(); |
| 97 | + selection?.addRange(range); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + // Trigger mouseup event to simulate selection |
| 102 | + await page.evaluate(() => { |
| 103 | + const event = new MouseEvent('mouseup', { |
| 104 | + bubbles: true, |
| 105 | + cancelable: true, |
| 106 | + view: window, |
| 107 | + clientX: 100, |
| 108 | + clientY: 100, |
| 109 | + pageX: 100, |
| 110 | + pageY: 100, |
| 111 | + }); |
| 112 | + document.dispatchEvent(event); |
| 113 | + }); |
| 114 | + |
| 115 | + // Wait for annotation button to appear |
| 116 | + const annotationButton = page.locator('.pubky-annotation-button'); |
| 117 | + |
| 118 | + try { |
| 119 | + await expect(annotationButton).toBeVisible({ timeout: 5000 }); |
| 120 | + console.log('✅ Annotation button appeared'); |
| 121 | + } catch (error) { |
| 122 | + // Button didn't appear - check for errors |
| 123 | + console.error('❌ Annotation button did NOT appear'); |
| 124 | + |
| 125 | + // Log all console errors |
| 126 | + if (consoleErrors.length > 0) { |
| 127 | + console.error('\n📋 Browser Console Errors:'); |
| 128 | + consoleErrors.forEach((err, i) => { |
| 129 | + console.error(` ${i + 1}. ${err}`); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + // Debug: Check what's in the DOM |
| 134 | + const hasButton = await page.locator('.pubky-annotation-button').count(); |
| 135 | + console.log(`Button elements found: ${hasButton}`); |
| 136 | + |
| 137 | + // Check if event listener is set up |
| 138 | + const hasListeners = await page.evaluate(() => { |
| 139 | + // Check if AnnotationManager is initialized |
| 140 | + return (window as any).__graphitiContentScriptLoaded === true; |
| 141 | + }); |
| 142 | + console.log(`Content script loaded: ${hasListeners}`); |
| 143 | + |
| 144 | + throw error; |
| 145 | + } |
| 146 | + |
| 147 | + // Verify no critical errors |
| 148 | + const criticalErrors = consoleErrors.filter(err => |
| 149 | + err.includes('window is not defined') || |
| 150 | + err.includes('Cannot use import statement') || |
| 151 | + err.includes('chrome.storage') && err.includes('undefined') |
| 152 | + ); |
| 153 | + |
| 154 | + if (criticalErrors.length > 0) { |
| 155 | + console.error('\n❌ Critical errors found:'); |
| 156 | + criticalErrors.forEach(err => console.error(` - ${err}`)); |
| 157 | + throw new Error(`Critical errors detected: ${criticalErrors.join('; ')}`); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + test('should capture and report all browser errors', async ({ context, page }) => { |
| 162 | + await page.goto('https://example.com', { waitUntil: 'networkidle' }); |
| 163 | + await page.waitForTimeout(2000); // Wait for extension to initialize |
| 164 | + |
| 165 | + // Try to trigger annotation flow |
| 166 | + try { |
| 167 | + await page.locator('body').selectText(); |
| 168 | + await page.waitForSelector('.pubky-annotation-button', { timeout: 3000 }); |
| 169 | + } catch { |
| 170 | + // Button didn't appear, but we want to capture errors |
| 171 | + } |
| 172 | + |
| 173 | + // Report all errors found |
| 174 | + if (consoleErrors.length > 0) { |
| 175 | + console.log('\n📋 All Browser Errors Captured:'); |
| 176 | + consoleErrors.forEach((err, i) => { |
| 177 | + console.log(` ${i + 1}. ${err}`); |
| 178 | + }); |
| 179 | + |
| 180 | + // Write errors to file for inspection |
| 181 | + const fs = require('fs'); |
| 182 | + const path = require('path'); |
| 183 | + const errorFile = path.join(__dirname, '../../browser-errors.log'); |
| 184 | + fs.writeFileSync(errorFile, consoleErrors.join('\n\n')); |
| 185 | + console.log(`\n💾 Errors saved to: ${errorFile}`); |
| 186 | + } else { |
| 187 | + console.log('✅ No console errors detected'); |
| 188 | + } |
| 189 | + |
| 190 | + // Fail test if there are critical errors |
| 191 | + const hasCriticalErrors = consoleErrors.some(err => |
| 192 | + err.includes('window is not defined') || |
| 193 | + err.includes('Cannot use import statement') || |
| 194 | + err.includes('chrome.storage') && err.includes('undefined') |
| 195 | + ); |
| 196 | + |
| 197 | + if (hasCriticalErrors) { |
| 198 | + throw new Error('Critical browser errors detected - see browser-errors.log'); |
| 199 | + } |
| 200 | + }); |
| 201 | + |
| 202 | + test.afterEach(async () => { |
| 203 | + // Log summary |
| 204 | + if (consoleErrors.length > 0 || consoleWarnings.length > 0) { |
| 205 | + console.log(`\n📊 Summary: ${consoleErrors.length} errors, ${consoleWarnings.length} warnings`); |
| 206 | + } |
| 207 | + }); |
| 208 | +}); |
0 commit comments