#![cfg(feature = "integration")]
mod common;
use viewpoint_core::{Browser, DocumentLoadState};
async fn setup() -> (
Browser,
viewpoint_core::BrowserContext,
viewpoint_core::Page,
) {
common::launch_with_page().await
}
#[tokio::test]
async fn test_locator_fill_and_type() {
common::init_tracing();
let (browser, _context, page) = setup().await;
let html = r#"data:text/html,
<html><body>
<form>
<input type="text" name="custname" placeholder="Customer Name">
<input type="tel" name="custtel" placeholder="Phone">
</form>
</body></html>
"#;
page.goto(html)
.wait_until(DocumentLoadState::Load)
.goto()
.await
.expect("Failed to navigate");
let customer_name = page.locator("input[name='custname']");
customer_name
.fill("John Doe")
.await
.expect("Failed to fill");
let phone = page.locator("input[name='custtel']");
phone.click().await.expect("Failed to click phone field");
phone.type_text("555-1234").await.expect("Failed to type");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_locator_press() {
common::init_tracing();
let (browser, _context, page) = setup().await;
let html = r#"data:text/html,
<html><body>
<form>
<input type="text" name="custname" placeholder="Customer Name">
<input type="tel" name="custtel" placeholder="Phone">
</form>
</body></html>
"#;
page.goto(html)
.wait_until(DocumentLoadState::Load)
.goto()
.await
.expect("Failed to navigate");
let input = page.locator("input[name='custname']");
input.focus().await.expect("Failed to focus");
input.press("Tab").await.expect("Failed to press Tab");
input
.press("Control+a")
.await
.expect("Failed to press Ctrl+A");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_locator_clear() {
common::init_tracing();
let (browser, _context, page) = setup().await;
let html = r#"data:text/html,
<html><body>
<form>
<input type="text" name="custname" placeholder="Customer Name">
</form>
</body></html>
"#;
page.goto(html)
.wait_until(DocumentLoadState::Load)
.goto()
.await
.expect("Failed to navigate");
let input = page.locator("input[name='custname']");
input.fill("Some text").await.expect("Failed to fill");
input.clear().await.expect("Failed to clear");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_locator_check_uncheck() {
common::init_tracing();
let (browser, _context, page) = setup().await;
let html = r#"data:text/html,
<html><body>
<form>
<label>
<input type="checkbox" id="cheese" value="cheese" name="topping">
Cheese
</label>
</form>
</body></html>
"#;
page.goto(html)
.wait_until(DocumentLoadState::Load)
.goto()
.await
.expect("Failed to navigate");
let checkbox = page.locator("input[type='checkbox'][value='cheese']");
checkbox.check().await.expect("Failed to check");
let checked = checkbox
.is_checked()
.await
.expect("Failed to get checked state");
assert!(checked);
checkbox.uncheck().await.expect("Failed to uncheck");
let unchecked = !checkbox
.is_checked()
.await
.expect("Failed to get checked state");
assert!(unchecked);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_locator_select_option() {
common::init_tracing();
let (browser, _context, page) = setup().await;
let html = r#"data:text/html,
<html><body>
<select id="size" name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
</body></html>
"#;
page.goto(html)
.wait_until(DocumentLoadState::Load)
.goto()
.await
.expect("Failed to navigate");
let size_select = page.locator("select#size");
size_select
.select_option()
.value("medium")
.await
.expect("Failed to select by value");
size_select
.select_option()
.label("Large")
.await
.expect("Failed to select by text");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_locator_input_value() {
common::init_tracing();
let (browser, _context, page) = setup().await;
let html = r#"data:text/html,
<html><body>
<form>
<input type="text" name="custname" placeholder="Customer Name">
</form>
</body></html>
"#;
page.goto(html)
.wait_until(DocumentLoadState::Load)
.goto()
.await
.expect("Failed to navigate");
let input = page.locator("input[name='custname']");
input.fill("Test Value").await.expect("Failed to fill");
let value = input
.input_value()
.await
.expect("Failed to get input value");
assert_eq!(value, "Test Value");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_locator_focus() {
common::init_tracing();
let (browser, _context, page) = setup().await;
let html = r#"data:text/html,
<html><body>
<form>
<input type="text" name="custname" placeholder="Customer Name">
</form>
</body></html>
"#;
page.goto(html)
.wait_until(DocumentLoadState::Load)
.goto()
.await
.expect("Failed to navigate");
let input = page.locator("input[name='custname']");
input.focus().await.expect("Failed to focus");
browser.close().await.expect("Failed to close browser");
}