forked from zauberzeug/nicegui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_element.py
138 lines (98 loc) · 4.21 KB
/
test_element.py
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
from selenium.webdriver.common.by import By
from nicegui import ui
from nicegui.element import Element
from .screen import Screen
def test_classes(screen: Screen):
label = ui.label('Some label')
def assert_classes(classes: str) -> None:
assert screen.selenium.find_element(By.XPATH,
f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
screen.open('/')
screen.wait(0.5)
assert_classes('')
label.classes('one')
assert_classes('one')
label.classes('one')
assert_classes('one')
label.classes('two three')
assert_classes('one two three')
label.classes(remove='two')
assert_classes('one three')
label.classes(replace='four')
assert_classes('four')
def test_style_parsing():
assert Element._parse_style(None) == {}
assert Element._parse_style('color: red; background-color: green') == {'color': 'red', 'background-color': 'green'}
assert Element._parse_style('width:12em;height:34.5em') == {'width': '12em', 'height': '34.5em'}
assert Element._parse_style('transform: translate(120.0px, 50%)') == {'transform': 'translate(120.0px, 50%)'}
assert Element._parse_style('box-shadow: 0 0 0.5em #1976d2') == {'box-shadow': '0 0 0.5em #1976d2'}
def test_props_parsing():
assert Element._parse_props(None) == {}
assert Element._parse_props('one two=1 three="abc def"') == {'one': True, 'two': '1', 'three': 'abc def'}
assert Element._parse_props('loading percentage=12.5') == {'loading': True, 'percentage': '12.5'}
assert Element._parse_props('size=50%') == {'size': '50%'}
assert Element._parse_props('href=http://192.168.42.100/') == {'href': 'http://192.168.42.100/'}
assert Element._parse_props('hint="Your \\"given\\" name"') == {'hint': 'Your "given" name'}
assert Element._parse_props('input-style="{ color: #ff0000 }"') == {'input-style': '{ color: #ff0000 }'}
def test_style(screen: Screen):
label = ui.label('Some label')
def assert_style(style: str) -> None:
assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
screen.open('/')
screen.wait(0.5)
assert_style('')
label.style('color: red')
assert_style('color: red;')
label.style('color: red')
assert_style('color: red;')
label.style('color: blue')
assert_style('color: blue;')
label.style('font-weight: bold')
assert_style('color: blue; font-weight: bold;')
label.style(remove='color: blue')
assert_style('font-weight: bold;')
label.style(replace='text-decoration: underline')
assert_style('text-decoration: underline;')
label.style('color: blue;')
assert_style('text-decoration: underline; color: blue;')
def test_props(screen: Screen):
input = ui.input()
def assert_props(*props: str) -> None:
class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
screen.open('/')
screen.wait(0.5)
assert_props('standard')
input.props('dark')
assert_props('standard', 'dark')
input.props('dark')
assert_props('standard', 'dark')
input.props(remove='dark')
assert_props('standard')
def test_remove_and_clear(screen: Screen):
with ui.row() as row:
ui.label('Label A')
b = ui.label('Label B')
ui.label('Label C')
ui.button('Remove B', on_click=lambda: row.remove(b))
ui.button('Remove 0', on_click=lambda: row.remove(0))
ui.button('Clear', on_click=lambda: row.clear())
screen.open('/')
screen.should_contain('Label A')
screen.should_contain('Label B')
screen.should_contain('Label C')
screen.click('Remove B')
screen.wait(0.5)
screen.should_contain('Label A')
screen.should_not_contain('Label B')
screen.should_contain('Label C')
screen.click('Remove 0')
screen.wait(0.5)
screen.should_not_contain('Label A')
screen.should_not_contain('Label B')
screen.should_contain('Label C')
screen.click('Clear')
screen.wait(0.5)
screen.should_not_contain('Label A')
screen.should_not_contain('Label B')
screen.should_not_contain('Label C')