#![cfg(feature = "integration")]
use std::sync::Once;
use std::time::Duration;
use viewpoint_core::Browser;
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();
});
}
#[tokio::test]
async fn test_aria_snapshot_simple_page() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("Failed to launch browser");
let context = browser
.new_context()
.await
.expect("Failed to create context");
let page = context.new_page().await.expect("Failed to create page");
page.set_content(
r#"
<html><body>
<h1>Main Title</h1>
<button>Click me</button>
</body></html>
"#,
)
.set()
.await
.expect("Failed to set content");
let snapshot = page.aria_snapshot().await.expect("Failed to get snapshot");
let yaml = snapshot.to_yaml();
println!("Snapshot YAML:\n{}", yaml);
assert!(
yaml.contains("heading") || yaml.contains("button"),
"Snapshot should contain heading or button, got: {}",
yaml
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_aria_snapshot_iframe_boundary_detection() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("Failed to launch browser");
let context = browser
.new_context()
.await
.expect("Failed to create context");
let page = context.new_page().await.expect("Failed to create page");
page.set_content(
r#"
<html><body>
<h1>Main Page</h1>
<iframe name="myframe" title="Widget Frame" src="about:blank"></iframe>
</body></html>
"#,
)
.set()
.await
.expect("Failed to set content");
tokio::time::sleep(Duration::from_millis(200)).await;
let snapshot = page.aria_snapshot().await.expect("Failed to get snapshot");
let yaml = snapshot.to_yaml();
println!("Snapshot with iframe:\n{}", yaml);
assert!(
yaml.contains("iframe") || yaml.contains("[frame-boundary]"),
"Snapshot should contain iframe or frame-boundary marker, got: {}",
yaml
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_aria_snapshot_iframe_refs_collection() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("Failed to launch browser");
let context = browser
.new_context()
.await
.expect("Failed to create context");
let page = context.new_page().await.expect("Failed to create page");
page.set_content(
r#"
<html><body>
<h1>Multi-Frame Page</h1>
<iframe name="frame1" src="about:blank"></iframe>
<iframe name="frame2" src="about:blank"></iframe>
</body></html>
"#,
)
.set()
.await
.expect("Failed to set content");
tokio::time::sleep(Duration::from_millis(200)).await;
let snapshot = page.aria_snapshot().await.expect("Failed to get snapshot");
println!("Collected iframe_refs: {:?}", snapshot.iframe_refs);
assert!(
snapshot.iframe_refs.len() >= 2,
"Should have at least 2 iframe refs, got: {}",
snapshot.iframe_refs.len()
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_frame_aria_snapshot() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("Failed to launch browser");
let context = browser
.new_context()
.await
.expect("Failed to create context");
let page = context.new_page().await.expect("Failed to create page");
page.set_content(
r#"
<html><body>
<h1>Main Page</h1>
<iframe name="contentframe" srcdoc="<html><body><h2>Frame Content</h2><button>Frame Button</button></body></html>"></iframe>
</body></html>
"#,
)
.set()
.await
.expect("Failed to set content");
tokio::time::sleep(Duration::from_millis(300)).await;
let frames = page.frames().await.expect("Failed to get frames");
assert!(
frames.len() >= 2,
"Should have at least 2 frames (main + iframe)"
);
let content_frame = frames
.iter()
.find(|f| f.name() == "contentframe")
.expect("Should find content frame");
let frame_snapshot = content_frame
.aria_snapshot()
.await
.expect("Failed to get frame snapshot");
let yaml = frame_snapshot.to_yaml();
println!("Frame snapshot:\n{}", yaml);
assert!(
yaml.contains("button") || yaml.contains("heading"),
"Frame snapshot should contain button or heading, got: {}",
yaml
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_aria_snapshot_with_frames() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("Failed to launch browser");
let context = browser
.new_context()
.await
.expect("Failed to create context");
let page = context.new_page().await.expect("Failed to create page");
page.set_content(
r#"
<html><body>
<h1>Main Page Title</h1>
<iframe name="widget" srcdoc="<html><body><button>Widget Button</button></body></html>"></iframe>
</body></html>
"#,
)
.set()
.await
.expect("Failed to set content");
tokio::time::sleep(Duration::from_millis(300)).await;
let snapshot = page
.aria_snapshot_with_frames()
.await
.expect("Failed to get multi-frame snapshot");
let yaml = snapshot.to_yaml();
println!("Multi-frame snapshot:\n{}", yaml);
assert!(
yaml.contains("heading") || yaml.contains("button"),
"Multi-frame snapshot should contain page elements, got: {}",
yaml
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_aria_snapshot_nested_frames() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("Failed to launch browser");
let context = browser
.new_context()
.await
.expect("Failed to create context");
let page = context.new_page().await.expect("Failed to create page");
page.set_content(
r#"
<html><body>
<h1>Level 0</h1>
<iframe name="level1" srcdoc="<html><body><h2>Level 1</h2><iframe name='level2' srcdoc='<html><body><h3>Level 2</h3></body></html>'></iframe></body></html>"></iframe>
</body></html>
"#,
)
.set()
.await
.expect("Failed to set content");
tokio::time::sleep(Duration::from_millis(500)).await;
let frames = page.frames().await.expect("Failed to get frames");
println!("Total frames: {}", frames.len());
let snapshot = page
.aria_snapshot_with_frames()
.await
.expect("Failed to get snapshot");
let yaml = snapshot.to_yaml();
println!("Nested frames snapshot:\n{}", yaml);
assert!(
yaml.contains("heading") || yaml.contains("Level"),
"Snapshot should contain heading content"
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_locator_aria_snapshot() {
init_tracing();
let browser = Browser::launch()
.headless(true)
.launch()
.await
.expect("Failed to launch browser");
let context = browser
.new_context()
.await
.expect("Failed to create context");
let page = context.new_page().await.expect("Failed to create page");
page.set_content(
r#"
<html><body>
<form id="myform">
<label for="name">Name:</label>
<input id="name" type="text" />
<button type="submit">Submit</button>
</form>
</body></html>
"#,
)
.set()
.await
.expect("Failed to set content");
let form_snapshot = page
.locator("#myform")
.aria_snapshot()
.await
.expect("Failed to get form snapshot");
let yaml = form_snapshot.to_yaml();
println!("Form snapshot:\n{}", yaml);
assert!(
yaml.contains("form") || yaml.contains("textbox") || yaml.contains("button"),
"Form snapshot should contain form, textbox, or button, got: {}",
yaml
);
browser.close().await.expect("Failed to close browser");
}