#![cfg(feature = "integration")]
mod common;
use viewpoint_core::{AriaSnapshot, Browser};
#[tokio::test]
async fn test_query_methods_via_ref() {
common::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>
<input type="checkbox" id="mycheck" checked />
<p id="para" data-info="test-info">Hello World</p>
<input type="text" id="myinput" value="initial value" />
</body></html>
"#,
)
.set()
.await
.expect("Failed to set content");
let snapshot = page.aria_snapshot().await.expect("Failed to get snapshot");
let checkbox_ref =
common::find_ref_by_role(&snapshot, "checkbox", None).expect("Should find checkbox ref");
let checkbox_locator = page.locator_from_ref(&checkbox_ref);
let is_checked = checkbox_locator
.is_checked()
.await
.expect("Failed to check is_checked via ref");
assert!(is_checked, "Checkbox should be checked");
fn find_paragraph_ref(snapshot: &AriaSnapshot) -> Option<String> {
if snapshot.role.as_deref() == Some("paragraph") {
return snapshot.node_ref.clone();
}
for child in &snapshot.children {
if let Some(r) = find_paragraph_ref(child) {
return Some(r);
}
}
None
}
let para_ref = find_paragraph_ref(&snapshot).expect("Should find paragraph ref");
let para_locator = page.locator_from_ref(¶_ref);
let inner_text = para_locator
.inner_text()
.await
.expect("Failed to get inner_text via ref");
assert_eq!(inner_text, "Hello World", "Should get correct inner text");
let attr = para_locator
.get_attribute("data-info")
.await
.expect("Failed to get_attribute via ref");
assert_eq!(
attr.as_deref(),
Some("test-info"),
"Should get correct attribute"
);
let input_ref =
common::find_ref_by_role(&snapshot, "textbox", None).expect("Should find input ref");
let input_locator = page.locator_from_ref(&input_ref);
let value = input_locator
.input_value()
.await
.expect("Failed to get input_value via ref");
assert_eq!(value, "initial value", "Should get correct input value");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_aria_snapshot_on_locator_via_ref() {
common::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>
<div role="navigation">
<button>Home</button>
<button>About</button>
</div>
</body></html>
"#,
)
.set()
.await
.expect("Failed to set content");
let snapshot = page.aria_snapshot().await.expect("Failed to get snapshot");
let nav_ref = common::find_ref_by_role(&snapshot, "navigation", None)
.expect("Should find navigation ref");
let locator = page.locator_from_ref(&nav_ref);
let nested_snapshot = locator
.aria_snapshot()
.await
.expect("Failed to get aria_snapshot via ref");
let yaml = nested_snapshot.to_yaml();
println!("Nested snapshot:\n{yaml}");
assert!(yaml.contains("Home"), "Should contain Home button");
assert!(yaml.contains("About"), "Should contain About button");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_all_texts_via_ref() {
common::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>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body></html>
"#,
)
.set()
.await
.expect("Failed to set content");
let snapshot = page.aria_snapshot().await.expect("Failed to get snapshot");
let listitem_ref =
common::find_ref_by_role(&snapshot, "listitem", None).expect("Should find listitem ref");
let locator = page.locator_from_ref(&listitem_ref);
let inner_texts = locator
.all_inner_texts()
.await
.expect("Failed to get all_inner_texts via ref");
assert_eq!(inner_texts.len(), 1, "Should have 1 text (single element)");
assert!(inner_texts[0].contains("Item"), "Should contain 'Item'");
let text_contents = locator
.all_text_contents()
.await
.expect("Failed to get all_text_contents via ref");
assert_eq!(text_contents.len(), 1, "Should have 1 text content");
assert!(text_contents[0].contains("Item"), "Should contain 'Item'");
browser.close().await.expect("Failed to close browser");
}