-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_smart_selector.py
More file actions
76 lines (59 loc) · 2.64 KB
/
test_smart_selector.py
File metadata and controls
76 lines (59 loc) · 2.64 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
"""
Tests for smart selector inference
"""
import pytest
from sentience import SentienceBrowser, record, snapshot
def test_smart_selector_inference():
"""Test that recorder infers selectors automatically"""
with SentienceBrowser() as browser:
browser.page.goto("https://example.com")
browser.page.wait_for_load_state("networkidle")
# Take snapshot to get element
snap = snapshot(browser)
if len(snap.elements) > 0:
element = snap.elements[0]
with record(browser) as rec:
# Record click without providing selector
rec.record_click(element.id)
# Should have inferred a selector
step = rec.trace.steps[0]
# Selector may or may not be inferred depending on element properties
# But element_id should always be present
assert step.element_id == element.id
def test_smart_selector_with_text():
"""Test selector inference for elements with text"""
with SentienceBrowser() as browser:
browser.page.goto("https://example.com")
browser.page.wait_for_load_state("networkidle")
snap = snapshot(browser)
# Find element with text
element_with_text = None
for el in snap.elements:
if el.text and len(el.text) > 0:
element_with_text = el
break
if element_with_text:
with record(browser) as rec:
rec.record_click(element_with_text.id)
step = rec.trace.steps[0]
# If selector was inferred, it should include text
if step.selector:
assert "text" in step.selector or "role" in step.selector
def test_smart_selector_validation():
"""Test that inferred selectors are validated"""
with SentienceBrowser() as browser:
browser.page.goto("https://example.com")
browser.page.wait_for_load_state("networkidle")
snap = snapshot(browser)
if len(snap.elements) > 0:
element = snap.elements[0]
with record(browser) as rec:
rec.record_click(element.id)
step = rec.trace.steps[0]
# If selector was inferred and validated, it should match the element
if step.selector:
# Verify selector would match the element
from sentience.query import query
matches = query(snap, step.selector)
# Should match at least the original element
assert any(el.id == element.id for el in matches)