viewpoint-core 0.3.6

High-level browser automation API for Viewpoint
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#![cfg(feature = "integration")]

//! Context-related tests.
//!
//! Tests for browser context configuration, cookies, permissions, storage state,
//! geolocation, emulation, and other context-level features.

mod common;

use std::collections::HashMap;
use std::time::Duration;
use viewpoint_core::{Browser, Cookie, DocumentLoadState, Permission, SameSite};

/// Helper function to launch browser and get a page.
async fn setup() -> (
    Browser,
    viewpoint_core::BrowserContext,
    viewpoint_core::Page,
) {
    common::launch_with_page().await
}

/// Test adding cookies.
#[tokio::test]
async fn test_context_add_cookies() {
    common::init_tracing();

    let (browser, context, page) = setup().await;

    // Navigate to a page first (cookies need a domain)
    page.goto("https://example.com")
        .wait_until(DocumentLoadState::DomContentLoaded)
        .goto()
        .await
        .expect("Failed to navigate");

    // Add cookies
    context
        .add_cookies(vec![
            Cookie::new("session", "abc123")
                .domain("example.com")
                .path("/")
                .secure(true)
                .http_only(true),
            Cookie::new("user_pref", "dark_mode")
                .domain("example.com")
                .path("/")
                .same_site(SameSite::Lax),
        ])
        .await
        .expect("Failed to add cookies");

    // Get all cookies
    let cookies = context.cookies().await.expect("Failed to get cookies");
    assert!(!cookies.is_empty(), "Should have at least one cookie");

    browser.close().await.expect("Failed to close browser");
}

/// Test clearing cookies.
#[tokio::test]
async fn test_context_clear_cookies() {
    common::init_tracing();

    let (browser, context, page) = setup().await;

    // Navigate to a page first
    page.goto("https://example.com")
        .wait_until(DocumentLoadState::DomContentLoaded)
        .goto()
        .await
        .expect("Failed to navigate");

    // Add cookies
    context
        .add_cookies(vec![Cookie::new("test", "value").domain("example.com")])
        .await
        .expect("Failed to add cookies");

    // Clear all cookies
    context
        .clear_cookies()
        .await
        .expect("Failed to clear cookies");

    // Verify cookies are cleared
    let cookies = context.cookies().await.expect("Failed to get cookies");
    let _ = cookies;

    browser.close().await.expect("Failed to close browser");
}

/// Test granting permissions.
#[tokio::test]
async fn test_context_grant_permissions() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");

    // Grant permissions
    context
        .grant_permissions(vec![Permission::Geolocation, Permission::Notifications])
        .await
        .expect("Failed to grant permissions");

    // Clear permissions
    context
        .clear_permissions()
        .await
        .expect("Failed to clear permissions");

    browser.close().await.expect("Failed to close browser");
}

/// Test setting geolocation.
#[tokio::test]
async fn test_context_set_geolocation() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let _page = context.new_page().await.expect("Failed to create page");

    // Set geolocation (San Francisco)
    context
        .set_geolocation(37.7749, -122.4194)
        .accuracy(10.0)
        .await
        .expect("Failed to set geolocation");

    // Clear geolocation
    context
        .clear_geolocation()
        .await
        .expect("Failed to clear geolocation");

    browser.close().await.expect("Failed to close browser");
}

/// Test setting offline mode.
#[tokio::test]
async fn test_context_offline_mode() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let _page = context.new_page().await.expect("Failed to create page");

    // Go offline
    context
        .set_offline(true)
        .await
        .expect("Failed to go offline");

    // Go back online
    context
        .set_offline(false)
        .await
        .expect("Failed to go online");

    browser.close().await.expect("Failed to close browser");
}

/// Test setting extra HTTP headers.
#[tokio::test]
async fn test_context_extra_http_headers() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let _page = context.new_page().await.expect("Failed to create page");

    // Set extra headers
    let mut headers = HashMap::new();
    headers.insert("X-Custom-Header".to_string(), "test-value".to_string());
    headers.insert("Authorization".to_string(), "Bearer token123".to_string());

    context
        .set_extra_http_headers(headers)
        .await
        .expect("Failed to set extra headers");

    browser.close().await.expect("Failed to close browser");
}

/// Test default timeout configuration.
#[tokio::test]
async fn test_context_timeout_configuration() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let mut context = browser
        .new_context()
        .await
        .expect("Failed to create context");

    // Get default timeouts
    let default = context.default_timeout();
    assert_eq!(default, Duration::from_secs(30));

    // Set custom timeouts
    context.set_default_timeout(Duration::from_secs(60));
    context.set_default_navigation_timeout(Duration::from_secs(120));

    assert_eq!(context.default_timeout(), Duration::from_secs(60));
    assert_eq!(
        context.default_navigation_timeout(),
        Duration::from_secs(120)
    );

    browser.close().await.expect("Failed to close browser");
}

/// Test creating context with builder/options.
#[tokio::test]
async fn test_context_with_options() {
    common::init_tracing();

    let browser = common::launch_browser().await;

    // Create context with options
    let context = browser
        .new_context_builder()
        .geolocation(40.7128, -74.0060) // New York
        .permissions(vec![Permission::Geolocation])
        .has_touch(true)
        .locale("en-US")
        .timezone_id("America/New_York")
        .default_timeout(Duration::from_secs(45))
        .build()
        .await
        .expect("Failed to create context with options");

    // Verify timeout was set
    assert_eq!(context.default_timeout(), Duration::from_secs(45));

    browser.close().await.expect("Failed to close browser");
}

/// Test storage state.
#[tokio::test]
async fn test_storage_state() {
    common::init_tracing();

    let (browser, context, page) = setup().await;

    // Navigate to add some state
    page.goto("https://example.com")
        .wait_until(DocumentLoadState::DomContentLoaded)
        .goto()
        .await
        .expect("Failed to navigate");

    // Get storage state
    let state = context
        .storage_state()
        .await
        .expect("Failed to get storage state");

    // Should have a structure (cookies and origins)
    let _ = state.cookies;
    let _ = state.origins;

    browser.close().await.expect("Failed to close browser");
}

/// Test custom test ID attribute.
#[tokio::test]
async fn test_custom_test_id_attribute() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");

    // Set custom test ID attribute
    context.set_test_id_attribute("data-test").await;

    // Verify it was set
    let attr = context.test_id_attribute().await;
    assert_eq!(attr, "data-test");

    browser.close().await.expect("Failed to close browser");
}

/// Test default test ID attribute.
#[tokio::test]
async fn test_default_test_id_attribute() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");

    // Check default test ID attribute
    let attr = context.test_id_attribute().await;
    assert_eq!(attr, "data-testid");

    browser.close().await.expect("Failed to close browser");
}

/// Test context geolocation.
#[tokio::test]
async fn test_context_geolocation() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let _page = context.new_page().await.expect("Failed to create page");

    // Set geolocation
    context
        .set_geolocation(51.5074, -0.1278) // London
        .await
        .expect("Failed to set geolocation");

    browser.close().await.expect("Failed to close browser");
}

/// Test clearing geolocation.
#[tokio::test]
async fn test_context_geolocation_clear() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let _page = context.new_page().await.expect("Failed to create page");

    // Set then clear
    context
        .set_geolocation(51.5074, -0.1278)
        .await
        .expect("Failed to set");
    context.clear_geolocation().await.expect("Failed to clear");

    browser.close().await.expect("Failed to close browser");
}

/// Test timezone emulation.
#[tokio::test]
async fn test_timezone_emulation() {
    common::init_tracing();

    let browser = common::launch_browser().await;

    // Create context with timezone
    let _context = browser
        .new_context_builder()
        .timezone_id("Europe/London")
        .build()
        .await
        .expect("Failed to create context with timezone");

    browser.close().await.expect("Failed to close browser");
}

/// Test locale emulation.
#[tokio::test]
async fn test_locale_emulation() {
    common::init_tracing();

    let browser = common::launch_browser().await;

    // Create context with locale
    let _context = browser
        .new_context_builder()
        .locale("fr-FR")
        .build()
        .await
        .expect("Failed to create context with locale");

    browser.close().await.expect("Failed to close browser");
}

/// Test that storage_state() succeeds after closing a page.
/// This verifies that the internal pages tracking list is properly maintained
/// when pages are closed, preventing stale session errors.
#[tokio::test]
async fn test_storage_state_succeeds_after_page_close() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");

    // Create a page
    let mut page = context.new_page().await.expect("Failed to create page");

    // Navigate to a page
    page.goto("https://example.com")
        .wait_until(DocumentLoadState::DomContentLoaded)
        .goto()
        .await
        .expect("Failed to navigate");

    // Close the page
    page.close().await.expect("Failed to close page");

    // Call storage_state() - this should succeed without "session not found" error
    // because the closed page was removed from the internal tracking list
    let state = context
        .storage_state()
        .await
        .expect("storage_state() should succeed after closing page");

    // Verify we got a valid state
    let _ = state.cookies;
    let _ = state.origins;

    browser.close().await.expect("Failed to close browser");
}

/// Test that storage_state() collects cookies even when no pages remain open.
#[tokio::test]
async fn test_storage_state_cookies_after_all_pages_closed() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");

    // Create a page and navigate to set cookies
    let mut page = context.new_page().await.expect("Failed to create page");
    page.goto("https://example.com")
        .wait_until(DocumentLoadState::DomContentLoaded)
        .goto()
        .await
        .expect("Failed to navigate");

    // Add a cookie
    context
        .add_cookies(vec![Cookie::new("test_cookie", "test_value").domain("example.com")])
        .await
        .expect("Failed to add cookie");

    // Close the page
    page.close().await.expect("Failed to close page");

    // Call storage_state() - should succeed and include cookies
    let state = context
        .storage_state()
        .await
        .expect("storage_state() should succeed after closing page");

    // Verify we got cookies (cookies are fetched from browser, not from pages)
    assert!(!state.cookies.is_empty(), "Should have cookies");

    browser.close().await.expect("Failed to close browser");
}

/// Test that storage_state() works correctly with multiple pages where some are closed.
/// This verifies the fix for stale session issues where closed pages would cause
/// "session not found" errors when iterating over the internal pages list.
#[tokio::test]
async fn test_storage_state_with_multiple_pages_some_closed() {
    common::init_tracing();

    let browser = common::launch_browser().await;
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");

    // Create first page and navigate
    let mut page1 = context.new_page().await.expect("Failed to create page 1");
    page1.goto("https://example.com")
        .wait_until(DocumentLoadState::DomContentLoaded)
        .goto()
        .await
        .expect("Failed to navigate page 1");

    // Create second page and navigate
    let page2 = context.new_page().await.expect("Failed to create page 2");
    page2.goto("https://httpbin.org/html")
        .wait_until(DocumentLoadState::DomContentLoaded)
        .goto()
        .await
        .expect("Failed to navigate page 2");

    // Close first page - this should remove it from the internal tracking list
    page1.close().await.expect("Failed to close page 1");

    // Call storage_state() - should succeed without "session not found" error
    // because the closed page was removed from the internal tracking list
    let state = context
        .storage_state()
        .await
        .expect("storage_state() should succeed with remaining pages");

    // Verify we got a valid state
    let _ = state.cookies;
    let _ = state.origins;

    browser.close().await.expect("Failed to close browser");
}