#![cfg(feature = "integration")]
use std::sync::Once;
use std::time::Duration;
use viewpoint_core::DocumentLoadState;
use viewpoint_test::{Browser, TestHarness, expect_page};
#[allow(unused_imports)]
use viewpoint_test::{BrowserContext, Page};
static TRACING_INIT: Once = Once::new();
fn init_tracing() {
TRACING_INIT.call_once(|| {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::from_default_env()
.add_directive(tracing::Level::INFO.into()),
)
.with_test_writer()
.try_init()
.ok();
});
}
#[viewpoint_test::test]
async fn test_macro_page_fixture(page: Page) {
init_tracing();
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await
.expect("should navigate");
expect_page(page)
.to_have_title("Example Domain")
.await
.expect("should have correct title");
}
#[viewpoint_test::test]
async fn test_macro_multiple_fixtures(page: Page, context: BrowserContext) {
init_tracing();
assert!(!page.is_closed(), "page should not be closed");
assert!(!context.is_closed(), "context should not be closed");
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await
.expect("should navigate");
}
#[viewpoint_test::test(headless = true)]
async fn test_macro_headless_configuration(page: Page) {
init_tracing();
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await
.expect("should navigate in headless mode");
}
#[viewpoint_test::test(timeout = 60000)]
async fn test_macro_timeout_configuration(page: Page) {
init_tracing();
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await
.expect("should navigate with custom timeout");
}
#[viewpoint_test::test(headless = true, timeout = 45000)]
async fn test_macro_combined_configuration(page: Page) {
init_tracing();
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await
.expect("should navigate with combined config");
}
#[tokio::test]
async fn test_harness_browser_scope() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("should launch browser");
let harness = TestHarness::from_browser(&browser)
.await
.expect("should create harness from browser");
let page = harness.page();
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await
.expect("should navigate");
drop(harness);
browser.close().await.expect("should close browser");
}
#[tokio::test]
async fn test_harness_context_scope() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("should launch browser");
let context = browser.new_context().await.expect("should create context");
let harness = TestHarness::from_context(&context)
.await
.expect("should create harness from context");
let page = harness.page();
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await
.expect("should navigate");
drop(harness);
}
#[tokio::test]
async fn test_harness_from_browser_ownership() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("should launch browser");
let harness = TestHarness::from_browser(&browser)
.await
.expect("should create harness");
assert!(
harness.browser().is_none(),
"harness should not own browser"
);
assert!(harness.context().is_some(), "context should be available");
assert!(!harness.page().is_closed(), "page should be open");
drop(harness);
browser.close().await.expect("should close browser");
}
#[tokio::test]
async fn test_harness_from_context_ownership() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("should launch browser");
let context = browser.new_context().await.expect("should create context");
let harness = TestHarness::from_context(&context)
.await
.expect("should create harness");
assert!(
harness.browser().is_none(),
"harness should not own browser"
);
assert!(
harness.context().is_none(),
"harness should not own context"
);
assert!(!harness.page().is_closed(), "page should be open");
drop(harness);
}
#[tokio::test]
async fn test_default_harness_is_headless() {
init_tracing();
let harness = TestHarness::new().await.expect("should create harness");
assert!(harness.config().headless, "default should be headless");
assert_eq!(
harness.config().timeout,
Duration::from_secs(30),
"default timeout should be 30 seconds"
);
}