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 count;
22mod locator;
23mod locator_helpers;
24mod page;
25mod soft;
26mod soft_locator;
27mod soft_page;
28mod state;
29mod text;
30
31#[cfg(test)]
32mod tests;
33
34pub use locator::LocatorAssertions;
35pub use page::PageAssertions;
36pub use soft::{SoftAssertionError, SoftAssertions};
37pub use soft_locator::SoftLocatorAssertions;
38pub use soft_page::SoftPageAssertions;
39
40use viewpoint_core::{Locator, Page};
41
42/// Create assertions for a locator.
43///
44/// # Example
45///
46/// ```ignore
47/// use viewpoint_test::expect;
48///
49/// expect(&locator).to_be_visible().await?;
50/// expect(&locator).to_have_text("Hello").await?;
51/// ```
52pub fn expect<'a>(locator: &'a Locator<'a>) -> LocatorAssertions<'a> {
53 LocatorAssertions::new(locator)
54}
55
56/// Create assertions for a page.
57///
58/// # Example
59///
60/// ```ignore
61/// use viewpoint_test::expect_page;
62///
63/// expect_page(&page).to_have_url("https://example.com").await?;
64/// expect_page(&page).to_have_title("Example").await?;
65/// ```
66pub fn expect_page(page: &Page) -> PageAssertions<'_> {
67 PageAssertions::new(page)
68}
69
70/// Trait for creating assertions from different types.
71///
72/// This enables a unified `expect()` function that works with both
73/// locators and pages.
74pub trait Expectable<'a> {
75 /// The assertion builder type for this value.
76 type Assertions;
77
78 /// Create an assertion builder for this value.
79 fn assertions(&'a self) -> Self::Assertions;
80}
81
82impl<'a> Expectable<'a> for Locator<'a> {
83 type Assertions = LocatorAssertions<'a>;
84
85 fn assertions(&'a self) -> Self::Assertions {
86 LocatorAssertions::new(self)
87 }
88}
89
90impl<'a> Expectable<'a> for Page {
91 type Assertions = PageAssertions<'a>;
92
93 fn assertions(&'a self) -> Self::Assertions {
94 PageAssertions::new(self)
95 }
96}