|
2 | 2 | * Tests for actions (click, type, press, clickRect) |
3 | 3 | */ |
4 | 4 |
|
5 | | -import { SentienceBrowser, click, typeText, press, clickRect, snapshot, find, BBox } from '../src'; |
| 5 | +import { |
| 6 | + SentienceBrowser, |
| 7 | + click, |
| 8 | + typeText, |
| 9 | + press, |
| 10 | + scrollTo, |
| 11 | + clickRect, |
| 12 | + snapshot, |
| 13 | + find, |
| 14 | + BBox, |
| 15 | +} from '../src'; |
6 | 16 | import { createTestBrowser, getPageOrThrow } from './test-utils'; |
7 | 17 |
|
8 | 18 | describe('Actions', () => { |
@@ -119,6 +129,121 @@ describe('Actions', () => { |
119 | 129 | }, 60000); |
120 | 130 | }); |
121 | 131 |
|
| 132 | + describe('scrollTo', () => { |
| 133 | + it('should scroll an element into view', async () => { |
| 134 | + const browser = await createTestBrowser(); |
| 135 | + |
| 136 | + try { |
| 137 | + const page = getPageOrThrow(browser); |
| 138 | + await page.goto('https://example.com'); |
| 139 | + await page.waitForLoadState('networkidle', { timeout: 10000 }); |
| 140 | + |
| 141 | + const snap = await snapshot(browser); |
| 142 | + // Find an element to scroll to |
| 143 | + const elements = snap.elements.filter(el => el.role === 'link'); |
| 144 | + |
| 145 | + if (elements.length > 0) { |
| 146 | + // Get the last element which might be out of viewport |
| 147 | + const element = elements.length > 1 ? elements[elements.length - 1] : elements[0]; |
| 148 | + const result = await scrollTo(browser, element.id); |
| 149 | + expect(result.success).toBe(true); |
| 150 | + expect(result.duration_ms).toBeGreaterThan(0); |
| 151 | + expect(['navigated', 'dom_updated']).toContain(result.outcome); |
| 152 | + } |
| 153 | + } finally { |
| 154 | + await browser.close(); |
| 155 | + } |
| 156 | + }, 60000); |
| 157 | + |
| 158 | + it('should scroll with instant behavior', async () => { |
| 159 | + const browser = await createTestBrowser(); |
| 160 | + |
| 161 | + try { |
| 162 | + const page = getPageOrThrow(browser); |
| 163 | + await page.goto('https://example.com'); |
| 164 | + await page.waitForLoadState('networkidle', { timeout: 10000 }); |
| 165 | + |
| 166 | + const snap = await snapshot(browser); |
| 167 | + const elements = snap.elements.filter(el => el.role === 'link'); |
| 168 | + |
| 169 | + if (elements.length > 0) { |
| 170 | + const element = elements[0]; |
| 171 | + const result = await scrollTo(browser, element.id, 'instant', 'start'); |
| 172 | + expect(result.success).toBe(true); |
| 173 | + expect(result.duration_ms).toBeGreaterThan(0); |
| 174 | + } |
| 175 | + } finally { |
| 176 | + await browser.close(); |
| 177 | + } |
| 178 | + }, 60000); |
| 179 | + |
| 180 | + it('should take snapshot after scroll when requested', async () => { |
| 181 | + const browser = await createTestBrowser(); |
| 182 | + |
| 183 | + try { |
| 184 | + const page = getPageOrThrow(browser); |
| 185 | + await page.goto('https://example.com'); |
| 186 | + await page.waitForLoadState('networkidle', { timeout: 10000 }); |
| 187 | + |
| 188 | + const snap = await snapshot(browser); |
| 189 | + const elements = snap.elements.filter(el => el.role === 'link'); |
| 190 | + |
| 191 | + if (elements.length > 0) { |
| 192 | + const element = elements[0]; |
| 193 | + const result = await scrollTo(browser, element.id, 'smooth', 'center', true); |
| 194 | + expect(result.success).toBe(true); |
| 195 | + expect(result.snapshot_after).toBeDefined(); |
| 196 | + expect(result.snapshot_after?.status).toBe('success'); |
| 197 | + } |
| 198 | + } finally { |
| 199 | + await browser.close(); |
| 200 | + } |
| 201 | + }, 60000); |
| 202 | + |
| 203 | + it('should fail for invalid element ID', async () => { |
| 204 | + const browser = await createTestBrowser(); |
| 205 | + |
| 206 | + try { |
| 207 | + const page = getPageOrThrow(browser); |
| 208 | + await page.goto('https://example.com'); |
| 209 | + await page.waitForLoadState('networkidle', { timeout: 10000 }); |
| 210 | + |
| 211 | + // Try to scroll to non-existent element |
| 212 | + const result = await scrollTo(browser, 99999); |
| 213 | + expect(result.success).toBe(false); |
| 214 | + expect(result.error).toBeDefined(); |
| 215 | + expect(result.error?.code).toBe('scroll_failed'); |
| 216 | + } finally { |
| 217 | + await browser.close(); |
| 218 | + } |
| 219 | + }, 60000); |
| 220 | + }); |
| 221 | + |
| 222 | + describe('typeText with delay', () => { |
| 223 | + it('should type text with human-like delay', async () => { |
| 224 | + const browser = await createTestBrowser(); |
| 225 | + |
| 226 | + try { |
| 227 | + const page = getPageOrThrow(browser); |
| 228 | + await page.goto('https://example.com'); |
| 229 | + await page.waitForLoadState('networkidle', { timeout: 10000 }); |
| 230 | + |
| 231 | + const snap = await snapshot(browser); |
| 232 | + const textbox = find(snap, 'role=textbox'); |
| 233 | + |
| 234 | + if (textbox) { |
| 235 | + // Test with 10ms delay between keystrokes |
| 236 | + const result = await typeText(browser, textbox.id, 'hello', false, 10); |
| 237 | + expect(result.success).toBe(true); |
| 238 | + // Duration should be longer due to delays (at least 5 chars * 10ms = 50ms) |
| 239 | + expect(result.duration_ms).toBeGreaterThanOrEqual(50); |
| 240 | + } |
| 241 | + } finally { |
| 242 | + await browser.close(); |
| 243 | + } |
| 244 | + }, 60000); |
| 245 | + }); |
| 246 | + |
122 | 247 | describe('clickRect', () => { |
123 | 248 | it('should click at rectangle center using rect dict', async () => { |
124 | 249 | const browser = await createTestBrowser(); |
|
0 commit comments