File: test_evaluate_js.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 (202 lines) | stat: -rwxr-xr-x 3,522 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
import pytest

import webview

from .util import run_test


@pytest.fixture
def window():
    return webview.create_window('Evaluate JS test', html='<html><body><div id="node">TEST</div></body></html>')


def test_mixed(window):
    run_test(webview, window, mixed_test)


def test_array(window):
    run_test(webview, window, array_test)


def test_object(window):
    run_test(webview, window, object_test)


def test_string(window):
    run_test(webview, window, string_test)


def test_int(window):
    run_test(webview, window, int_test)


def test_float(window):
    run_test(webview, window, float_test)


def test_undefined(window):
    run_test(webview, window, undefined_test)


def test_null(window):
    run_test(webview, window, null_test)


def test_nan(window):
    run_test(webview, window, nan_test)


def test_body(window):
    run_test(webview, window, body_test)


def test_document(window):
    run_test(webview, window, document_test)


def test_node(window):
    run_test(webview, window, node_test)


def test_exception(window):
    run_test(webview, window, exception_test)


def mixed_test(window):
    result = window.evaluate_js(
        """
        document.body.style.backgroundColor = '#212121';
        // comment
        function test() {
            return 2 + 2;
        }
        test();
    """
    )
    assert result == 4


def array_test(window):
    result = window.evaluate_js(
        """
    function getValue() {
        return [undefined, 1, 'two', 3.00001, {four: true}]
    }
    getValue()
    """
    )
    assert result == [None, 1, 'two', 3.00001, {'four': True}]


def object_test(window):
    result = window.evaluate_js(
        """
    function getValue() {
        return {1: 2, 'test': true, obj: {2: false, 3: 3.1}}
    }

    getValue()
    """
    )
    assert result == {'1': 2, 'test': True, 'obj': {'2': False, '3': 3.1}}


def string_test(window):
    result = window.evaluate_js(
        """
    function getValue() {
        return "this is only a test"
    }

    getValue()
    """
    )
    assert result == 'this is only a test'


def int_test(window):
    result = window.evaluate_js(
        """
    function getValue() {
        return 23
    }

    getValue()
    """
    )
    assert result == 23


def float_test(window):
    result = window.evaluate_js(
        """
    function getValue() {
        return 23.23443
    }

    getValue()
    """
    )
    assert result == 23.23443


def undefined_test(window):
    result = window.evaluate_js(
        """
    function getValue() {
        return undefined
    }

    getValue()
    """
    )
    assert result is None


def null_test(window):
    result = window.evaluate_js(
        """
    function getValue() {
        return null
    }

    getValue()
    """
    )
    assert result is None


def nan_test(window):
    result = window.evaluate_js(
        """
    function getValue() {
        return NaN
    }

    getValue()
    """
    )
    assert result is None


def body_test(window):
    result = window.evaluate_js('document.body')

    assert result['nodeName'] == 'BODY'


def document_test(window):
    result = window.evaluate_js('document')

    assert result['nodeName'] == '#document'


def node_test(window):
    node = window.evaluate_js('document.querySelector("#node")')

    assert node['id'] == 'node'
    assert node['innerText'] == 'TEST'

def exception_test(window):
    with pytest.raises(webview.errors.JavascriptException):
        window.evaluate_js('eklmn')