viewpoint_test/expect/mod.rs
1//! Assertion API for browser automation tests.
2//!
3//! The `expect` function creates assertion builders for locators and pages,
4//! enabling fluent async assertions.
5//!
6//! # Example
7//!
8//! ```ignore
9//! use viewpoint_test::expect;
10//!
11//! // Assert element is visible
12//! expect(&locator).to_be_visible().await?;
13//!
14//! // Assert text content
15//! expect(&locator).to_have_text("Hello").await?;
16//!
17//! // Assert page URL
18//! expect(&page).to_have_url("https://example.com").await?;
19//! ```
20
21mod locator;
22mod page;
23
24pub use locator::LocatorAssertions;
25pub use page::PageAssertions;
26
27use viewpoint_core::{Locator, Page};
28
29/// Create assertions for a locator.
30///
31/// # Example
32///
33/// ```ignore
34/// use viewpoint_test::expect;
35///
36/// expect(&locator).to_be_visible().await?;
37/// expect(&locator).to_have_text("Hello").await?;
38/// ```
39pub fn expect<'a>(locator: &'a Locator<'a>) -> LocatorAssertions<'a> {
40 LocatorAssertions::new(locator)
41}
42
43/// Create assertions for a page.
44///
45/// # Example
46///
47/// ```ignore
48/// use viewpoint_test::expect_page;
49///
50/// expect_page(&page).to_have_url("https://example.com").await?;
51/// expect_page(&page).to_have_title("Example").await?;
52/// ```
53pub fn expect_page(page: &Page) -> PageAssertions<'_> {
54 PageAssertions::new(page)
55}
56
57/// Trait for creating assertions from different types.
58///
59/// This enables a unified `expect()` function that works with both
60/// locators and pages.
61pub trait Expectable<'a> {
62 /// The assertion builder type for this value.
63 type Assertions;
64
65 /// Create an assertion builder for this value.
66 fn assertions(&'a self) -> Self::Assertions;
67}
68
69impl<'a> Expectable<'a> for Locator<'a> {
70 type Assertions = LocatorAssertions<'a>;
71
72 fn assertions(&'a self) -> Self::Assertions {
73 LocatorAssertions::new(self)
74 }
75}
76
77impl<'a> Expectable<'a> for Page {
78 type Assertions = PageAssertions<'a>;
79
80 fn assertions(&'a self) -> Self::Assertions {
81 PageAssertions::new(self)
82 }
83}