#![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_iframe_content_execution_context() {
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 Frame Content</h1>
<iframe name="test-frame" srcdoc="<html><head><title>Iframe Title</title></head><body><h1>Iframe Heading</h1><p>Iframe paragraph content.</p></body></html>"></iframe>
</body></html>
"##,
)
.set()
.await
.expect("Failed to set content");
tokio::time::sleep(Duration::from_millis(300)).await;
let iframe = page
.frame("test-frame")
.await
.expect("Failed to get frames")
.expect("Should find test-frame");
let content = iframe.content().await.expect("Failed to get iframe content");
assert!(
content.contains("Iframe Heading"),
"Content should contain iframe heading, got: {}",
content
);
assert!(
content.contains("Iframe paragraph content"),
"Content should contain iframe paragraph, got: {}",
content
);
assert!(
!content.contains("Main Frame Content"),
"Content should NOT contain main frame content, got: {}",
content
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_iframe_title_execution_context() {
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>
<head><title>Main Page Title</title></head>
<body>
<iframe name="titled-frame" srcdoc="<html><head><title>Iframe Document Title</title></head><body>Content</body></html>"></iframe>
</body>
</html>
"##,
)
.set()
.await
.expect("Failed to set content");
tokio::time::sleep(Duration::from_millis(300)).await;
let iframe = page
.frame("titled-frame")
.await
.expect("Failed to get frames")
.expect("Should find titled-frame");
let title = iframe.title().await.expect("Failed to get iframe title");
assert_eq!(
title, "Iframe Document Title",
"Title should be the iframe's title"
);
let main_frame = page.main_frame().await.expect("Failed to get main frame");
let main_title = main_frame
.title()
.await
.expect("Failed to get main frame title");
assert_eq!(
main_title, "Main Page Title",
"Main frame title should be different"
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_iframe_aria_snapshot_execution_context() {
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>
<button id="main-button">Main Button</button>
<iframe name="aria-frame" srcdoc="<html><body><button id='iframe-button'>Iframe Button</button><input type='text' placeholder='Iframe Input' /></body></html>"></iframe>
</body></html>
"##,
)
.set()
.await
.expect("Failed to set content");
tokio::time::sleep(Duration::from_millis(300)).await;
let iframe = page
.frame("aria-frame")
.await
.expect("Failed to get frames")
.expect("Should find aria-frame");
let snapshot = iframe
.aria_snapshot()
.await
.expect("Failed to get iframe aria snapshot");
let yaml = snapshot.to_yaml();
println!("Iframe aria snapshot:\n{}", yaml);
assert!(
yaml.contains("Iframe Button") || yaml.contains("button"),
"Snapshot should contain iframe button, got: {}",
yaml
);
assert!(
!yaml.contains("Main Button"),
"Snapshot should NOT contain main frame button, got: {}",
yaml
);
browser.close().await.expect("Failed to close browser");
}