File: test_dom.py

package info (click to toggle)
python-pywebview 5.0.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,680 kB
  • sloc: python: 9,592; javascript: 917; java: 158; cs: 130; sh: 16; makefile: 3
file content (268 lines) | stat: -rwxr-xr-x 7,858 bytes parent folder | download
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import pytest

import webview
from time import sleep
from .util import run_test


@pytest.fixture
def window():
    return webview.create_window('DOM test', html='''
        <html>
            <body>
                <div id="container">
                    <div id="child1" class="test" data-id="blz" tabindex="3">DUDE</div>
                    <div id="child2" class="test"></div>
                    <div id="child3" class="test" style="display: none; background-color: rgb(255, 0, 0)" tabindex="2"></div>
                </div>
                <div id="container2">
                    <input type="text" value="pizdec" id="input"/>
                    <button id="button">Click me</button>
                </div>
            </body>
        </html>
    ''')


def test_element(window):
    run_test(webview, window, element_test)


def test_classes(window):
    run_test(webview, window, classes_test)


def test_attributes(window):
    run_test(webview, window, attributes_test)


def test_style(window):
    run_test(webview, window, style_test)


def test_traversal(window):
    run_test(webview, window, traversal_test)


def test_visibility(window):
    run_test(webview, window, visibility_test)


def test_focus(window):
    run_test(webview, window, focus_test)


def test_manipulation(window):
    run_test(webview, window, manipulation_test)

def test_manipulation_modes(window):
    run_test(webview, window, manipulation_mode_test)

def test_events(window):
    run_test(webview, window, events_test)


def element_test(window):
    child1 = window.dom.get_element('#child1')

    assert child1.id == 'child1'
    assert child1.tag == 'div'
    assert child1.tabindex == 3
    assert child1.text == 'DUDE'

    child1.text = 'WOW'
    assert child1.text == 'WOW'

    child1.tabindex = 10
    assert child1.tabindex == 10

    input = window.dom.get_element('#input')
    assert input.value == 'pizdec'
    input.value = 'tisok'
    assert input.value == 'tisok'


def classes_test(window):
    child1 = window.dom.get_element('#child1')
    assert list(child1.classes) == ['test']

    child1.classes.append('test2')
    assert list(child1.classes) == ['test', 'test2']

    child1.classes.toggle('test')
    assert list(child1.classes) == ['test2']

    child1.classes.toggle('test')
    assert list(child1.classes) == ['test2', 'test']

    child1.classes = ['woah']
    assert list(child1.classes) == ['woah']

    child1.classes.clear()
    assert len(child1.classes) == 0


def attributes_test(window):
    child1 = window.dom.get_element('#child1')

    assert dict(child1.attributes) == {'class': 'test', 'id': 'child1', 'data-id': 'blz', 'tabindex': '3'}

    assert child1.attributes['class'] == 'test'
    assert set(child1.attributes.keys()) == {'class', 'id', 'data-id', 'tabindex'}
    assert set(child1.attributes.values()) == {'test', 'child1', 'blz', '3'}
    assert set(child1.attributes.items()) == {('class', 'test'), ('id', 'child1'), ('data-id', 'blz'), ('tabindex', '3')}
    assert child1.attributes.get('class') == 'test'
    assert child1.attributes.get('class2') == None
    assert child1.attributes['class2'] == None

    del child1.attributes['class']
    assert dict(child1.attributes) == {'id': 'child1', 'data-id': 'blz', 'tabindex': '3'}

    child1.attributes['data-test'] = 'test2'
    assert dict(child1.attributes) == {'id': 'child1', 'data-id': 'blz', 'tabindex': '3', 'data-test': 'test2'}

    child1.attributes = {'data-test': 'test3'}
    assert child1.attributes['data-test'] == 'test3'

    child1.attributes.clear()
    assert dict(child1.attributes) == {}


def style_test(window):
    child3 = window.dom.get_element('#child3')
    assert child3.style['display'] == 'none'
    assert child3.style['background-color'] == 'rgb(255, 0, 0)'

    child3.style['display'] = 'flex'
    child3.style['background-color'] = 'rgb(0, 0, 255)'
    assert child3.style['display'] == 'flex'
    assert child3.style['background-color'] == 'rgb(0, 0, 255)'

    child3.style.clear()
    assert child3.style['display'] == 'block'
    assert child3.style['background-color'] == 'rgba(0, 0, 0, 0)'


def visibility_test(window):
    child3 = window.dom.get_element('#child3')
    assert child3.visible == False

    child3.show()
    assert child3.visible == True

    child3.hide()
    assert child3.visible == False

    child3.toggle()
    assert child3.visible == True

    child3.toggle()
    assert child3.visible == False


def focus_test(window):
    input = window.dom.get_element('#input')
    assert input.focused == False

    input.focus()
    assert input.focused == True

    input.blur()
    assert input.focused == False


def traversal_test(window):
    child2 = window.dom.get_element('#child2')

    assert child2.parent.id == 'container'
    assert child2.next.id == 'child3'
    assert child2.previous.id == 'child1'

    children = window.dom.get_element('#container').children

    assert len(children) == 3
    assert children[0].id == 'child1'
    assert children[1].id == 'child2'
    assert children[2].id == 'child3'


def manipulation_test(window):
    container = window.dom.get_element('#container')
    container2 = window.dom.get_element('#container2')
    assert len(container.children) == 3

    container.empty()
    assert container.children == []

    container.append('<div id="child1"></div>')
    assert len(container.children) == 1
    assert container.children[0].id == 'child1'

    child2 = window.dom.create_element('<div id="child2" class="child-class">CHILD</div>', container)
    assert len(container.children) == 2
    assert container.children[1].id == 'child2'
    assert child2.parent.id == 'container'
    assert child2.id == 'child2'

    child3 = child2.copy(container2)
    assert len(container2.children) == 3
    assert container2.children[2].text == 'CHILD'
    assert list(child3.classes) == ['child-class']
    assert child3.id == ''
    assert child2._node_id != child3._node_id

    child4 = child2.copy()
    child4.parent.id = 'container'
    assert len(container.children) == 3

    child4.move(container2)
    assert len(container.children) == 2
    assert len(container2.children) == 4

    container.children[0].remove()
    assert len(container.children) == 1
    assert container.children[0].id == 'child2'


def manipulation_mode_test(window):
    child1 = window.dom.get_element('#child1')
    child2 = window.dom.get_element('#child2')
    child3 = window.dom.get_element('#child3')
    container2 = window.dom.get_element('#container2')

    child1.move(container2, mode=webview.dom.ManipulationMode.FirstChild)
    assert container2.children[0].id == 'child1'

    child2.move(container2, mode=webview.dom.ManipulationMode.LastChild)
    assert container2.children[-1].id == 'child2'

    child3.move(child1, mode=webview.dom.ManipulationMode.Before)
    assert container2.children[0].id == 'child3'

    child1.move(child2, mode=webview.dom.ManipulationMode.After)
    assert container2.children[-1].id == 'child1'

    child2.move(container2, mode=webview.dom.ManipulationMode.Replace)
    assert window.dom.get_element('#container2') == None
    assert child2.parent.tag == 'body'


def events_test(window):
    def click_handler(event):
        nonlocal button_value
        assert event['target']['id'] == 'button'
        button_value = True

    button_value = False
    button = window.dom.get_element('#button')
    button.events.click += click_handler

    window.evaluate_js('document.getElementById("button").click()')
    assert button_value == True

    button.events.click -= click_handler
    button_value = False
    window.evaluate_js('document.getElementById("button").click()')
    sleep(0.1)
    assert button_value == False
    assert window.evaluate_js('Object.keys(pywebview._eventHandlers).length === 0') == True