-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpect.py
More file actions
188 lines (134 loc) · 6.03 KB
/
Copy pathexpect.py
File metadata and controls
188 lines (134 loc) · 6.03 KB
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
"""
Expect/Assert functionality
"""
import asyncio
import time
from .browser import AsyncSentienceBrowser, SentienceBrowser
from .models import Element
from .query import query
from .wait import wait_for, wait_for_async
class Expectation:
"""Assertion helper for element expectations"""
def __init__(self, browser: SentienceBrowser, selector: str | dict):
self.browser = browser
self.selector = selector
def to_be_visible(self, timeout: float = 10.0) -> Element:
"""Assert element is visible (exists and in viewport)"""
result = wait_for(self.browser, self.selector, timeout=timeout)
if not result.found:
raise AssertionError(f"Element not found: {self.selector} (timeout: {timeout}s)")
element = result.element
if not element.in_viewport:
raise AssertionError(f"Element found but not visible in viewport: {self.selector}")
return element
def to_exist(self, timeout: float = 10.0) -> Element:
"""Assert element exists"""
result = wait_for(self.browser, self.selector, timeout=timeout)
if not result.found:
raise AssertionError(f"Element does not exist: {self.selector} (timeout: {timeout}s)")
return result.element
def to_have_text(self, expected_text: str, timeout: float = 10.0) -> Element:
"""Assert element has specific text"""
result = wait_for(self.browser, self.selector, timeout=timeout)
if not result.found:
raise AssertionError(f"Element not found: {self.selector} (timeout: {timeout}s)")
element = result.element
if not element.text or expected_text not in element.text:
raise AssertionError(
f"Element text mismatch. Expected '{expected_text}', got '{element.text}'"
)
return element
def to_have_count(self, expected_count: int, timeout: float = 10.0) -> None:
"""Assert selector matches exactly N elements"""
from .snapshot import snapshot
start_time = time.time()
while time.time() - start_time < timeout:
snap = snapshot(self.browser)
matches = query(snap, self.selector)
if len(matches) == expected_count:
return
time.sleep(0.25)
# Final check
snap = snapshot(self.browser)
matches = query(snap, self.selector)
actual_count = len(matches)
raise AssertionError(
f"Element count mismatch. Expected {expected_count}, got {actual_count}"
)
def expect(browser: SentienceBrowser, selector: str | dict) -> Expectation:
"""
Create expectation helper for assertions
Args:
browser: SentienceBrowser instance
selector: String DSL or dict query
Returns:
Expectation helper
"""
return Expectation(browser, selector)
class ExpectationAsync:
"""Assertion helper for element expectations (async)"""
def __init__(self, browser: AsyncSentienceBrowser, selector: str | dict):
self.browser = browser
self.selector = selector
async def to_be_visible(self, timeout: float = 10.0) -> Element:
"""Assert element is visible (exists and in viewport)"""
result = await wait_for_async(self.browser, self.selector, timeout=timeout)
if not result.found:
raise AssertionError(f"Element not found: {self.selector} (timeout: {timeout}s)")
element = result.element
if not element.in_viewport:
raise AssertionError(f"Element found but not visible in viewport: {self.selector}")
return element
async def to_exist(self, timeout: float = 10.0) -> Element:
"""Assert element exists"""
result = await wait_for_async(self.browser, self.selector, timeout=timeout)
if not result.found:
raise AssertionError(f"Element does not exist: {self.selector} (timeout: {timeout}s)")
return result.element
async def to_have_text(self, expected_text: str, timeout: float = 10.0) -> Element:
"""Assert element has specific text"""
result = await wait_for_async(self.browser, self.selector, timeout=timeout)
if not result.found:
raise AssertionError(f"Element not found: {self.selector} (timeout: {timeout}s)")
element = result.element
if not element.text or expected_text not in element.text:
raise AssertionError(
f"Element text mismatch. Expected '{expected_text}', got '{element.text}'"
)
return element
async def to_have_count(self, expected_count: int, timeout: float = 10.0) -> None:
"""Assert selector matches exactly N elements"""
from .snapshot import snapshot_async
start_time = time.time()
while time.time() - start_time < timeout:
snap = await snapshot_async(self.browser)
matches = query(snap, self.selector)
if len(matches) == expected_count:
return
await asyncio.sleep(0.25)
# Final check
snap = await snapshot_async(self.browser)
matches = query(snap, self.selector)
actual_count = len(matches)
raise AssertionError(
f"Element count mismatch. Expected {expected_count}, got {actual_count}"
)
def expect_async(browser: AsyncSentienceBrowser, selector: str | dict) -> ExpectationAsync:
"""
Create expectation helper for assertions (async)
Args:
browser: AsyncSentienceBrowser instance
selector: String DSL or dict query
Returns:
ExpectationAsync helper
Example:
# Assert element is visible
element = await expect_async(browser, "role=button").to_be_visible()
# Assert element has text
element = await expect_async(browser, "h1").to_have_text("Welcome")
# Assert element exists
element = await expect_async(browser, "role=link").to_exist()
# Assert count
await expect_async(browser, "role=button").to_have_count(5)
"""
return ExpectationAsync(browser, selector)