pub fn expect(locator: Locator) -> ExpectationExpand description
Creates an expectation for a locator with auto-retry behavior.
Assertions will retry until they pass or timeout (default: 5 seconds).
§Example
ⓘ
use playwright_rs::{expect, protocol::Playwright};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let playwright = Playwright::launch().await?;
let browser = playwright.chromium().launch().await?;
let page = browser.new_page().await?;
// Test to_be_visible and to_be_hidden
page.goto("data:text/html,<button id='btn'>Click me</button><div id='hidden' style='display:none'>Hidden</div>", None).await?;
expect(page.locator("#btn").await).to_be_visible().await?;
expect(page.locator("#hidden").await).to_be_hidden().await?;
// Test not() negation
expect(page.locator("#btn").await).not().to_be_hidden().await?;
expect(page.locator("#hidden").await).not().to_be_visible().await?;
// Test with_timeout()
page.goto("data:text/html,<div id='element'>Visible</div>", None).await?;
expect(page.locator("#element").await)
.with_timeout(Duration::from_secs(10))
.to_be_visible()
.await?;
// Test to_be_enabled and to_be_disabled
page.goto("data:text/html,<button id='enabled'>Enabled</button><button id='disabled' disabled>Disabled</button>", None).await?;
expect(page.locator("#enabled").await).to_be_enabled().await?;
expect(page.locator("#disabled").await).to_be_disabled().await?;
// Test to_be_checked and to_be_unchecked
page.goto("data:text/html,<input type='checkbox' id='checked' checked><input type='checkbox' id='unchecked'>", None).await?;
expect(page.locator("#checked").await).to_be_checked().await?;
expect(page.locator("#unchecked").await).to_be_unchecked().await?;
// Test to_be_editable
page.goto("data:text/html,<input type='text' id='editable'>", None).await?;
expect(page.locator("#editable").await).to_be_editable().await?;
// Test to_be_focused
page.goto("data:text/html,<input type='text' id='input'>", None).await?;
page.evaluate::<(), ()>("document.getElementById('input').focus()", None).await?;
expect(page.locator("#input").await).to_be_focused().await?;
// Test to_contain_text
page.goto("data:text/html,<div id='content'>Hello World</div>", None).await?;
expect(page.locator("#content").await).to_contain_text("Hello").await?;
expect(page.locator("#content").await).to_contain_text("World").await?;
// Test to_have_text
expect(page.locator("#content").await).to_have_text("Hello World").await?;
// Test to_have_value
page.goto("data:text/html,<input type='text' id='input' value='test value'>", None).await?;
expect(page.locator("#input").await).to_have_value("test value").await?;
browser.close().await?;
Ok(())
}