forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_evaluate.py
215 lines (161 loc) · 5.94 KB
/
test_evaluate.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
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
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
from datetime import datetime
from playwright import Error
async def test_evaluate_work(page):
result = await page.evaluate("7 * 3")
assert result == 21
async def test_evaluate_return_none_for_null(page):
result = await page.evaluate("a => a", None)
assert result is None
async def test_evaluate_transfer_nan(page):
result = await page.evaluate("a => a", float("nan"))
assert math.isnan(result)
async def test_evaluate_transfer_neg_zero(page):
result = await page.evaluate("a => a", -0)
assert result == float("-0")
async def test_evaluate_transfer_infinity(page):
result = await page.evaluate("a => a", float("Infinity"))
assert result == float("Infinity")
async def test_evaluate_transfer_neg_infinity(page):
result = await page.evaluate("a => a", float("-Infinity"))
assert result == float("-Infinity")
async def test_evaluate_roundtrip_unserializable_values(page):
value = {
"infinity": float("Infinity"),
"nInfinity": float("-Infinity"),
"nZero": float("-0"),
}
result = await page.evaluate("a => a", value)
assert result == value
async def test_evaluate_transfer_arrays(page):
result = await page.evaluate("a => a", [1, 2, 3])
assert result == [1, 2, 3]
async def test_evaluate_return_undefined_for_objects_with_symbols(page):
assert await page.evaluate('[Symbol("foo4")]') == [None]
assert (
await page.evaluate(
"""() => {
const a = { };
a[Symbol('foo4')] = 42;
return a;
}"""
)
== {}
)
assert (
await page.evaluate(
"""() => {
return { foo: [{ a: Symbol('foo4') }] };
}"""
)
== {"foo": [{"a": None}]}
)
async def test_evaluate_work_with_unicode_chars(page):
result = await page.evaluate('a => a["中文字符"]', {"中文字符": 42})
assert result == 42
async def test_evaluate_throw_when_evaluation_triggers_reload(page):
error = None
try:
await page.evaluate(
"() => { location.reload(); return new Promise(() => {}); }"
)
except Error as e:
error = e
assert "navigation" in error.message
async def test_evaluate_work_with_exposed_function(page):
await page.exposeFunction("callController", lambda a, b: a * b)
result = await page.evaluate("callController(9, 3)")
assert result == 27
async def test_evaluate_reject_promise_with_exception(page):
error = None
try:
await page.evaluate("not_existing_object.property")
except Error as e:
error = e
assert "not_existing_object" in error.message
async def test_evaluate_support_thrown_strings(page):
error = None
try:
await page.evaluate('throw "qwerty"')
except Error as e:
error = e
assert "qwerty" in error.message
async def test_evaluate_support_thrown_numbers(page):
error = None
try:
await page.evaluate("throw 100500")
except Error as e:
error = e
assert "100500" in error.message
async def test_evaluate_return_complex_objects(page):
obj = {"foo": "bar!"}
result = await page.evaluate("a => a", obj)
assert result == obj
async def test_evaluate_accept_none_as_one_of_multiple_parameters(page):
result = await page.evaluate(
'({ a, b }) => Object.is(a, undefined) && Object.is(b, "foo")',
{"a": None, "b": "foo"},
)
assert result
async def test_evaluate_properly_serialize_none_arguments(page):
assert await page.evaluate("x => ({a: x})", None) == {"a": None}
async def test_evaluate_fail_for_circular_object(page):
assert (
await page.evaluate(
"""() => {
const a = {};
const b = {a};
a.b = b;
return a;
}"""
)
is None
)
async def test_evaluate_accept_string(page):
assert await page.evaluate("1 + 2") == 3
async def test_evaluate_accept_element_handle_as_an_argument(page):
await page.setContent("<section>42</section>")
element = await page.querySelector("section")
text = await page.evaluate("e => e.textContent", element)
assert text == "42"
async def test_evaluate_throw_if_underlying_element_was_disposed(page):
await page.setContent("<section>39</section>")
element = await page.querySelector("section")
await element.dispose()
error = None
try:
await page.evaluate("e => e.textContent", element)
except Error as e:
error = e
assert "JSHandle is disposed" in error.message
async def test_evaluate_evaluate_exception(page):
error = await page.evaluate('new Error("error message")')
assert "Error: error message" in error
async def test_evaluate_evaluate_date(page):
result = await page.evaluate(
'() => ({ date: new Date("2020-05-27T01:31:38.506Z") })'
)
assert result == {"date": datetime.fromisoformat("2020-05-27T01:31:38.506")}
async def test_evaluate_roundtrip_date(page):
date = datetime.fromisoformat("2020-05-27T01:31:38.506")
result = await page.evaluate("date => date", date)
assert result == date
async def test_evaluate_jsonvalue_date(page):
date = datetime.fromisoformat("2020-05-27T01:31:38.506")
result = await page.evaluate(
'() => ({ date: new Date("2020-05-27T01:31:38.506Z") })'
)
assert result == {"date": date}