Skip to content

Commit c6260fc

Browse files
authored
In evaluate, return the correct value when the result is falsy (#203)
* In evaluate, return the correct value when the result is falsy. Before this change evaluate(..., return_by_value=True) would incorrectly return None for any falsy result. Also cleanup the return_by_value=False case, by removing an unnecessary check for remote_object.deep_serialized_value. See the linked issue for the rationale. Fixes #201. * Update change log, fix mypy error * Update return type for tab.evaluate
1 parent 6121b0f commit c6260fc

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
- Fix `evaluate` to return the correct value instead of None for falsy values @thromer
13+
1214
### Added
1315

1416
### Changed

tests/core/test_tab.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,17 @@ async def test_evaluate_return_by_value_simple_json(browser: zd.Browser) -> None
339339
]
340340

341341

342+
async def test_evaluate_return_by_value_falsy(browser: zd.Browser) -> None:
343+
tab = await browser.get(sample_file("simple_json.html"))
344+
await tab.wait_for_ready_state("complete")
345+
346+
expr_template = "JSON.parse(document.querySelector('%s').textContent)"
347+
348+
assert await tab.evaluate(expr_template % "#zero") == 0
349+
assert await tab.evaluate(expr_template % "#empty_array") == []
350+
assert await tab.evaluate(expr_template % "#null") is None
351+
352+
342353
async def test_evaluate_stress_test_complex_objects(browser: zd.Browser) -> None:
343354
tab = await browser.get(sample_file("complex_object.html"))
344355
await tab.wait_for_ready_state("complete")

tests/sample_data/simple_json.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>Simple JSON object</title>
5+
<title>Simple JSON objects</title>
66
</head>
77
<body>
88
<pre id="obj">{"a": "x", "b": 3.14159}</pre>
9+
<pre id="zero">0</pre>
10+
<pre id="empty_array">[]</pre>
11+
<pre id="null">null</pre>
912
</body>
1013
</html>

zendriver/core/tab.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import urllib.parse
1212
import warnings
1313
import webbrowser
14-
from typing import TYPE_CHECKING, Any, List, Literal, Optional, Tuple, Union
14+
from typing import TYPE_CHECKING, Any, List, Literal, Optional, Tuple, Union, cast
1515

1616
from .intercept import BaseFetchInterception
1717
from .. import cdp
@@ -21,6 +21,7 @@
2121
from .expect import DownloadExpectation, RequestExpectation, ResponseExpectation
2222
from ..cdp.fetch import RequestStage
2323
from ..cdp.network import ResourceType
24+
from ..cdp.runtime import DeepSerializedValue
2425

2526

2627
if TYPE_CHECKING:
@@ -739,15 +740,11 @@ async def evaluate(
739740
if errors:
740741
raise ProtocolException(errors)
741742

742-
if remote_object:
743-
if return_by_value:
744-
if remote_object.value:
745-
return remote_object.value
746-
else:
747-
if remote_object.deep_serialized_value:
748-
return remote_object.deep_serialized_value.value
749-
750-
return remote_object, errors
743+
if return_by_value:
744+
return remote_object.value
745+
# deep_serialized_value is guaranteed to be present when
746+
# serialization_options.serialization="deep"
747+
return cast(DeepSerializedValue, remote_object.deep_serialized_value).value
751748

752749
async def js_dumps(
753750
self, obj_name: str, return_by_value: Optional[bool] = True
@@ -920,9 +917,10 @@ async def js_dumps(
920917

921918
if exception_details:
922919
raise ProtocolException(exception_details)
923-
if return_by_value and remote_object.value:
920+
if return_by_value:
924921
return remote_object.value
925922
else:
923+
# TODO Why not remote_object.deep_serialized_value.value?
926924
return remote_object, exception_details
927925

928926
async def close(self) -> None:

0 commit comments

Comments
 (0)