How to use test_evaluate_transfer_infinity method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_evaluate.py

Source: test_evaluate.py Github

copy
1# Copyright (c) Microsoft Corporation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import math
16from datetime import datetime
17
18from playwright.async_api import Error
19
20
21async def test_evaluate_work(page):
22    result = await page.evaluate("7 * 3")
23    assert result == 21
24
25
26async def test_evaluate_return_none_for_null(page):
27    result = await page.evaluate("a => a", None)
28    assert result is None
29
30
31async def test_evaluate_transfer_nan(page):
32    result = await page.evaluate("a => a", float("nan"))
33    assert math.isnan(result)
34
35
36async def test_evaluate_transfer_neg_zero(page):
37    result = await page.evaluate("a => a", -0)
38    assert result == float("-0")
39
40
41async def test_evaluate_transfer_infinity(page):
42    result = await page.evaluate("a => a", float("Infinity"))
43    assert result == float("Infinity")
44
45
46async def test_evaluate_transfer_neg_infinity(page):
47    result = await page.evaluate("a => a", float("-Infinity"))
48    assert result == float("-Infinity")
49
50
51async def test_evaluate_roundtrip_unserializable_values(page):
52    value = {
53        "infinity": float("Infinity"),
54        "nInfinity": float("-Infinity"),
55        "nZero": float("-0"),
56    }
57    result = await page.evaluate("a => a", value)
58    assert result == value
59
60
61async def test_evaluate_transfer_arrays(page):
62    result = await page.evaluate("a => a", [1, 2, 3])
63    assert result == [1, 2, 3]
64
65
66async def test_evaluate_return_undefined_for_objects_with_symbols(page):
67    assert await page.evaluate('[Symbol("foo4")]') == [None]
68    assert (
69        await page.evaluate(
70            """() => {
71                const a = { };
72                a[Symbol('foo4')] = 42;
73                return a;
74            }"""
75        )
76        == {}
77    )
78    assert (
79        await page.evaluate(
80            """() => {
81                return { foo: [{ a: Symbol('foo4') }] };
82            }"""
83        )
84        == {"foo": [{"a": None}]}
85    )
86
87
88async def test_evaluate_work_with_unicode_chars(page):
89    result = await page.evaluate('a => a["中文字符"]', {"中文字符": 42})
90    assert result == 42
91
92
93async def test_evaluate_throw_when_evaluation_triggers_reload(page):
94    error = None
95    try:
96        await page.evaluate(
97            "() => { location.reload(); return new Promise(() => {}); }"
98        )
99    except Error as e:
100        error = e
101    assert "navigation" in error.message
102
103
104async def test_evaluate_work_with_exposed_function(page):
105    await page.expose_function("callController", lambda a, b: a * b)
106    result = await page.evaluate("callController(9, 3)")
107    assert result == 27
108
109
110async def test_evaluate_reject_promise_with_exception(page):
111    error = None
112    try:
113        await page.evaluate("not_existing_object.property")
114    except Error as e:
115        error = e
116    assert "not_existing_object" in error.message
117
118
119async def test_evaluate_support_thrown_strings(page):
120    error = None
121    try:
122        await page.evaluate('throw "qwerty"')
123    except Error as e:
124        error = e
125    assert "qwerty" in error.message
126
127
128async def test_evaluate_support_thrown_numbers(page):
129    error = None
130    try:
131        await page.evaluate("throw 100500")
132    except Error as e:
133        error = e
134    assert "100500" in error.message
135
136
137async def test_evaluate_return_complex_objects(page):
138    obj = {"foo": "bar!"}
139    result = await page.evaluate("a => a", obj)
140    assert result == obj
141
142
143async def test_evaluate_accept_none_as_one_of_multiple_parameters(page):
144    result = await page.evaluate(
145        '({ a, b }) => Object.is(a, undefined) && Object.is(b, "foo")',
146        {"a": None, "b": "foo"},
147    )
148    assert result
149
150
151async def test_evaluate_properly_serialize_none_arguments(page):
152    assert await page.evaluate("x => ({a: x})", None) == {"a": None}
153
154
155async def test_evaluate_fail_for_circular_object(page):
156    assert (
157        await page.evaluate(
158            """() => {
159                const a = {};
160                const b = {a};
161                a.b = b;
162                return a;
163            }"""
164        )
165        is None
166    )
167
168
169async def test_evaluate_accept_string(page):
170    assert await page.evaluate("1 + 2") == 3
171
172
173async def test_evaluate_accept_element_handle_as_an_argument(page):
174    await page.set_content("<section>42</section>")
175    element = await page.query_selector("section")
176    text = await page.evaluate("e => e.textContent", element)
177    assert text == "42"
178
179
180async def test_evaluate_throw_if_underlying_element_was_disposed(page):
181    await page.set_content("<section>39</section>")
182    element = await page.query_selector("section")
183    await element.dispose()
184    error = None
185    try:
186        await page.evaluate("e => e.textContent", element)
187    except Error as e:
188        error = e
189    assert "JSHandle is disposed" in error.message
190
191
192async def test_evaluate_evaluate_exception(page):
193    error = await page.evaluate('new Error("error message")')
194    assert "Error: error message" in error
195
196
197async def test_evaluate_evaluate_date(page):
198    result = await page.evaluate(
199        '() => ({ date: new Date("2020-05-27T01:31:38.506Z") })'
200    )
201    assert result == {"date": datetime.fromisoformat("2020-05-27T01:31:38.506")}
202
203
204async def test_evaluate_roundtrip_date(page):
205    date = datetime.fromisoformat("2020-05-27T01:31:38.506")
206    result = await page.evaluate("date => date", date)
207    assert result == date
208
209
210async def test_evaluate_jsonvalue_date(page):
211    date = datetime.fromisoformat("2020-05-27T01:31:38.506")
212    result = await page.evaluate(
213        '() => ({ date: new Date("2020-05-27T01:31:38.506Z") })'
214    )
215    assert result == {"date": date}
216
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)