#![cfg(feature = "integration")]
mod common;
use std::collections::HashMap;
use std::time::Duration;
use viewpoint_core::{Browser, Cookie, DocumentLoadState, Permission, SameSite};
async fn setup() -> (
Browser,
viewpoint_core::BrowserContext,
viewpoint_core::Page,
) {
common::launch_with_page().await
}
#[tokio::test]
async fn test_context_add_cookies() {
common::init_tracing();
let (browser, context, page) = setup().await;
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await
.expect("Failed to navigate");
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");
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");
}
#[tokio::test]
async fn test_context_clear_cookies() {
common::init_tracing();
let (browser, context, page) = setup().await;
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await
.expect("Failed to navigate");
context
.add_cookies(vec![Cookie::new("test", "value").domain("example.com")])
.await
.expect("Failed to add cookies");
context
.clear_cookies()
.await
.expect("Failed to clear cookies");
let cookies = context.cookies().await.expect("Failed to get cookies");
let _ = cookies;
browser.close().await.expect("Failed to close browser");
}
#[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");
context
.grant_permissions(vec![Permission::Geolocation, Permission::Notifications])
.await
.expect("Failed to grant permissions");
context
.clear_permissions()
.await
.expect("Failed to clear permissions");
browser.close().await.expect("Failed to close browser");
}
#[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");
context
.set_geolocation(37.7749, -122.4194)
.accuracy(10.0)
.await
.expect("Failed to set geolocation");
context
.clear_geolocation()
.await
.expect("Failed to clear geolocation");
browser.close().await.expect("Failed to close browser");
}
#[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");
context
.set_offline(true)
.await
.expect("Failed to go offline");
context
.set_offline(false)
.await
.expect("Failed to go online");
browser.close().await.expect("Failed to close browser");
}
#[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");
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");
}
#[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");
let default = context.default_timeout();
assert_eq!(default, Duration::from_secs(30));
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");
}
#[tokio::test]
async fn test_context_with_options() {
common::init_tracing();
let browser = common::launch_browser().await;
let context = browser
.new_context_builder()
.geolocation(40.7128, -74.0060) .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");
assert_eq!(context.default_timeout(), Duration::from_secs(45));
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_storage_state() {
common::init_tracing();
let (browser, context, page) = setup().await;
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await
.expect("Failed to navigate");
let state = context
.storage_state()
.await
.expect("Failed to get storage state");
let _ = state.cookies;
let _ = state.origins;
browser.close().await.expect("Failed to close browser");
}
#[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");
context.set_test_id_attribute("data-test").await;
let attr = context.test_id_attribute().await;
assert_eq!(attr, "data-test");
browser.close().await.expect("Failed to close browser");
}
#[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");
let attr = context.test_id_attribute().await;
assert_eq!(attr, "data-testid");
browser.close().await.expect("Failed to close browser");
}
#[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");
context
.set_geolocation(51.5074, -0.1278) .await
.expect("Failed to set geolocation");
browser.close().await.expect("Failed to close browser");
}
#[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");
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");
}
#[tokio::test]
async fn test_timezone_emulation() {
common::init_tracing();
let browser = common::launch_browser().await;
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");
}
#[tokio::test]
async fn test_locale_emulation() {
common::init_tracing();
let browser = common::launch_browser().await;
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");
}