1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! # Viewpoint Test - Browser Testing Framework
//!
//! Test framework for `Viewpoint` browser automation, providing assertion APIs,
//! test harness setup, and convenient test macros for browser-based E2E tests.
//!
//! ## Features
//!
//! - **Test Harness**: Automatic browser, context, and page setup/teardown
//! - **Locator Assertions**: Wait-based assertions for elements (`expect(locator)`)
//! - **Page Assertions**: Assertions for page state (`expect_page(page)`)
//! - **Soft Assertions**: Collect multiple failures without stopping the test
//! - **Fixture Scoping**: Reuse browser/context across tests for performance
//! - **Test Macro**: Convenient `#[viewpoint::test]` attribute for test setup
//!
//! ## Quick Start
//!
//! The easiest way to write tests is using the `TestHarness`:
//!
//! ```no_run
//! use viewpoint_test::{TestHarness, expect};
//!
//! #[tokio::test]
//! async fn my_test() -> Result<(), Box<dyn std::error::Error>> {
//! let harness = TestHarness::new().await?;
//! let page = harness.page();
//!
//! page.goto("https://example.com").goto().await?;
//!
//! // Assert element is visible
//! expect(page.locator("h1")).to_be_visible().await?;
//!
//! // Assert element has text
//! expect(page.locator("h1")).to_have_text("Example Domain").await?;
//!
//! Ok(()) // harness drops and cleans up automatically
//! }
//! ```
//!
//! ## Using the Test Macro
//!
//! For even more convenience, use the `#[viewpoint::test]` attribute:
//!
//! ```text
//! use viewpoint_test::test;
//! use viewpoint_core::Page;
//!
//! #[viewpoint_test::test]
//! async fn my_test(page: &Page) -> Result<(), Box<dyn std::error::Error>> {
//! page.goto("https://example.com").goto().await?;
//! // page is automatically set up and cleaned up
//! Ok(())
//! }
//! ```
//!
//! ## Locator Assertions
//!
//! The [`expect()`] function creates assertions for locators that automatically wait:
//!
//! ```ignore
//! use viewpoint_test::{TestHarness, expect};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! # let harness = TestHarness::new().await?;
//! # let page = harness.page();
//! // Visibility assertions
//! expect(page.locator("button")).to_be_visible().await?;
//! expect(page.locator(".hidden")).to_be_hidden().await?;
//!
//! // Text content assertions
//! expect(page.locator("h1")).to_have_text("Welcome").await?;
//! expect(page.locator("p")).to_contain_text("Hello").await?;
//!
//! // Input value assertions
//! expect(page.locator("input")).to_have_value("initial value").await?;
//! expect(page.locator("input")).to_be_empty().await?;
//!
//! // State assertions
//! expect(page.locator("button")).to_be_enabled().await?;
//! expect(page.locator("input")).to_be_disabled().await?;
//! expect(page.locator("input[type=checkbox]")).to_be_checked().await?;
//!
//! // Attribute assertions
//! expect(page.locator("a")).to_have_attribute("href", "/about").await?;
//!
//! // CSS assertions
//! expect(page.locator("div")).to_have_css("display", "flex").await?;
//!
//! // Count assertions
//! expect(page.locator("li")).to_have_count(5).await?;
//!
//! // Focus assertions
//! expect(page.locator("input")).to_be_focused().await?;
//!
//! // Negation with `.not()`
//! expect(page.locator("button")).not().to_be_disabled().await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Page Assertions
//!
//! The [`expect_page`] function creates assertions for page state:
//!
//! ```ignore
//! use viewpoint_test::{TestHarness, expect_page};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! # let harness = TestHarness::new().await?;
//! # let page = harness.page();
//! // URL assertions
//! expect_page(page).to_have_url("https://example.com/").await?;
//! expect_page(page).to_have_url_matching(r"example\.com").await?;
//!
//! // Title assertions
//! expect_page(page).to_have_title("Example Domain").await?;
//! expect_page(page).to_have_title_matching(r"Example.*").await?;
//!
//! // Negation
//! expect_page(page).not().to_have_url("https://wrong.com/").await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Soft Assertions
//!
//! Soft assertions collect failures without stopping the test, useful for checking
//! multiple conditions:
//!
//! ```ignore
//! use viewpoint_test::{TestHarness, SoftAssertions};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! # let harness = TestHarness::new().await?;
//! # let page = harness.page();
//! let soft = SoftAssertions::new();
//!
//! // These won't fail immediately
//! soft.expect(page.locator("h1")).to_have_text("Welcome").await;
//! soft.expect(page.locator("nav")).to_be_visible().await;
//! soft.expect(page.locator("footer")).to_be_visible().await;
//!
//! // Assert all at once - fails if any assertion failed
//! soft.assert_all()?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Fixture Scoping
//!
//! The harness supports different scoping levels for performance optimization:
//!
//! ```no_run
//! use viewpoint_test::TestHarness;
//! use viewpoint_core::Browser;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Test-scoped (default): new browser per test
//! let harness = TestHarness::new().await?;
//!
//! // Module-scoped: reuse browser, fresh context/page per test
//! # let shared_browser = Browser::launch().headless(true).launch().await?;
//! let harness = TestHarness::from_browser(&shared_browser).await?;
//!
//! // Context-scoped: reuse context, fresh page per test
//! # let context = shared_browser.new_context().await?;
//! let harness = TestHarness::from_context(&context).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Test Configuration
//!
//! Configure tests with [`TestConfig`]:
//!
//! ```no_run
//! use viewpoint_test::{TestHarness, TestConfig};
//! use std::time::Duration;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let config = TestConfig::builder()
//! .headless(true)
//! .timeout(Duration::from_secs(60))
//! .build();
//!
//! let harness = TestHarness::with_config(config).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Error Handling
//!
//! Assertions return [`AssertionError`] on failure with detailed messages:
//!
//! ```ignore
//! use viewpoint_test::{TestHarness, expect, AssertionError};
//!
//! # async fn example() -> Result<(), AssertionError> {
//! # let harness = TestHarness::new().await.unwrap();
//! # let page = harness.page();
//! // This will fail with a descriptive message if the element doesn't have the text
//! expect(page.locator("h1"))
//! .to_have_text("Expected Title")
//! .await?;
//! # Ok(())
//! # }
//! ```
pub use ;
pub use ;
pub use ;
pub use TestHarness;
// Re-export the test macro for convenience
pub use test;
// Re-export core types for convenience
pub use ;