-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpect.py
More file actions
92 lines (66 loc) · 2.78 KB
/
expect.py
File metadata and controls
92 lines (66 loc) · 2.78 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
"""
Expect/Assert functionality
"""
import time
from .browser import SentienceBrowser
from .models import Element
from .query import query
from .wait import wait_for
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)