|
| 1 | +/** |
| 2 | + * Example: Semantic waitFor using query DSL |
| 3 | + * Demonstrates waiting for elements using semantic selectors |
| 4 | + */ |
| 5 | + |
| 6 | +import { SentienceBrowser, snapshot, find, waitFor, click } from '../src/index'; |
| 7 | + |
| 8 | +async function main() { |
| 9 | + // Get API key from environment variable (optional - uses free tier if not set) |
| 10 | + const apiKey = process.env.SENTIENCE_API_KEY as string | undefined; |
| 11 | + |
| 12 | + const browser = new SentienceBrowser(apiKey, undefined, false); |
| 13 | + |
| 14 | + try { |
| 15 | + await browser.start(); |
| 16 | + |
| 17 | + // Navigate to example.com |
| 18 | + await browser.getPage().goto('https://example.com', { waitUntil: 'domcontentloaded' }); |
| 19 | + |
| 20 | + console.log('=== Semantic waitFor Demo ===\n'); |
| 21 | + |
| 22 | + // Example 1: Wait for element by role |
| 23 | + console.log('1. Waiting for link element (role=link)'); |
| 24 | + let waitResult = await waitFor(browser, 'role=link', 5000); |
| 25 | + if (waitResult.found) { |
| 26 | + console.log(` ✅ Found after ${waitResult.duration_ms}ms`); |
| 27 | + console.log(` Element: '${waitResult.element?.text}' (id: ${waitResult.element?.id})`); |
| 28 | + } else { |
| 29 | + console.log(` ❌ Not found (timeout: ${waitResult.timeout})`); |
| 30 | + } |
| 31 | + console.log(); |
| 32 | + |
| 33 | + // Example 2: Wait for element by role and text |
| 34 | + console.log('2. Waiting for link with specific text'); |
| 35 | + waitResult = await waitFor(browser, 'role=link text~"Example"', 5000); |
| 36 | + if (waitResult.found) { |
| 37 | + console.log(` ✅ Found after ${waitResult.duration_ms}ms`); |
| 38 | + console.log(` Element text: '${waitResult.element?.text}'`); |
| 39 | + } else { |
| 40 | + console.log(' ❌ Not found'); |
| 41 | + } |
| 42 | + console.log(); |
| 43 | + |
| 44 | + // Example 3: Wait for clickable element |
| 45 | + console.log('3. Waiting for clickable element'); |
| 46 | + waitResult = await waitFor(browser, 'clickable=true', 5000); |
| 47 | + if (waitResult.found) { |
| 48 | + console.log(` ✅ Found clickable element after ${waitResult.duration_ms}ms`); |
| 49 | + console.log(` Role: ${waitResult.element?.role}`); |
| 50 | + console.log(` Text: '${waitResult.element?.text}'`); |
| 51 | + console.log(` Is clickable: ${waitResult.element?.visual_cues.is_clickable}`); |
| 52 | + } else { |
| 53 | + console.log(' ❌ Not found'); |
| 54 | + } |
| 55 | + console.log(); |
| 56 | + |
| 57 | + // Example 4: Wait for element with importance threshold |
| 58 | + console.log('4. Waiting for important element (importance > 100)'); |
| 59 | + waitResult = await waitFor(browser, 'importance>100', 5000); |
| 60 | + if (waitResult.found) { |
| 61 | + console.log(` ✅ Found important element after ${waitResult.duration_ms}ms`); |
| 62 | + console.log(` Importance: ${waitResult.element?.importance}`); |
| 63 | + console.log(` Role: ${waitResult.element?.role}`); |
| 64 | + } else { |
| 65 | + console.log(' ❌ Not found'); |
| 66 | + } |
| 67 | + console.log(); |
| 68 | + |
| 69 | + // Example 5: Wait and then click |
| 70 | + console.log('5. Wait for element, then click it'); |
| 71 | + waitResult = await waitFor(browser, 'role=link', 5000); |
| 72 | + if (waitResult.found && waitResult.element) { |
| 73 | + console.log(' ✅ Found element, clicking...'); |
| 74 | + const clickResult = await click(browser, waitResult.element.id); |
| 75 | + console.log(` Click result: success=${clickResult.success}, outcome=${clickResult.outcome}`); |
| 76 | + if (clickResult.url_changed) { |
| 77 | + console.log(` ✅ Navigation occurred: ${browser.getPage().url()}`); |
| 78 | + } |
| 79 | + } else { |
| 80 | + console.log(' ❌ Element not found, cannot click'); |
| 81 | + } |
| 82 | + console.log(); |
| 83 | + |
| 84 | + // Example 6: Using local extension (fast polling) |
| 85 | + console.log('6. Using local extension with auto-optimized interval'); |
| 86 | + console.log(' When useApi=false, interval auto-adjusts to 250ms (fast)'); |
| 87 | + waitResult = await waitFor(browser, 'role=link', 5000, undefined, false); |
| 88 | + if (waitResult.found) { |
| 89 | + console.log(` ✅ Found after ${waitResult.duration_ms}ms`); |
| 90 | + console.log(' (Used local extension, polled every 250ms)'); |
| 91 | + } |
| 92 | + console.log(); |
| 93 | + |
| 94 | + // Example 7: Using remote API (slower polling) |
| 95 | + console.log('7. Using remote API with auto-optimized interval'); |
| 96 | + console.log(' When useApi=true, interval auto-adjusts to 1500ms (network-friendly)'); |
| 97 | + if (apiKey) { |
| 98 | + waitResult = await waitFor(browser, 'role=link', 5000, undefined, true); |
| 99 | + if (waitResult.found) { |
| 100 | + console.log(` ✅ Found after ${waitResult.duration_ms}ms`); |
| 101 | + console.log(' (Used remote API, polled every 1500ms)'); |
| 102 | + } |
| 103 | + } else { |
| 104 | + console.log(' ⚠️ Skipped (no API key set)'); |
| 105 | + } |
| 106 | + console.log(); |
| 107 | + |
| 108 | + // Example 8: Custom interval override |
| 109 | + console.log('8. Custom interval override (manual control)'); |
| 110 | + console.log(' You can still specify custom interval if needed'); |
| 111 | + waitResult = await waitFor(browser, 'role=link', 5000, 500, false); |
| 112 | + if (waitResult.found) { |
| 113 | + console.log(` ✅ Found after ${waitResult.duration_ms}ms`); |
| 114 | + console.log(' (Custom interval: 500ms)'); |
| 115 | + } |
| 116 | + console.log(); |
| 117 | + |
| 118 | + // Example 9: Wait for visible element (not occluded) |
| 119 | + console.log('9. Waiting for visible element (not occluded)'); |
| 120 | + waitResult = await waitFor(browser, 'role=link visible=true', 5000); |
| 121 | + if (waitResult.found) { |
| 122 | + console.log(` ✅ Found visible element after ${waitResult.duration_ms}ms`); |
| 123 | + console.log(` Is occluded: ${waitResult.element?.is_occluded}`); |
| 124 | + console.log(` In viewport: ${waitResult.element?.in_viewport}`); |
| 125 | + } |
| 126 | + console.log(); |
| 127 | + |
| 128 | + console.log('✅ Semantic waitFor demo complete!'); |
| 129 | + console.log('\nNote: waitFor uses the semantic query DSL to find elements.'); |
| 130 | + console.log('This is more robust than CSS selectors because it understands'); |
| 131 | + console.log('the semantic meaning of elements (role, text, clickability, etc.).'); |
| 132 | + } catch (e: any) { |
| 133 | + console.error(`❌ Error: ${e.message}`); |
| 134 | + console.error(e.stack); |
| 135 | + } finally { |
| 136 | + await browser.close(); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +main().catch(console.error); |
| 141 | + |
0 commit comments