Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix `evaluate` to return the correct value instead of None for falsy values @thromer

### Added

### Changed
Expand Down
11 changes: 11 additions & 0 deletions tests/core/test_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,17 @@ async def test_evaluate_return_by_value_simple_json(browser: zd.Browser) -> None
]


async def test_evaluate_return_by_value_falsy(browser: zd.Browser) -> None:
tab = await browser.get(sample_file("simple_json.html"))
await tab.wait_for_ready_state("complete")

expr_template = "JSON.parse(document.querySelector('%s').textContent)"

assert await tab.evaluate(expr_template % "#zero") == 0
assert await tab.evaluate(expr_template % "#empty_array") == []
assert await tab.evaluate(expr_template % "#null") is None


async def test_evaluate_stress_test_complex_objects(browser: zd.Browser) -> None:
tab = await browser.get(sample_file("complex_object.html"))
await tab.wait_for_ready_state("complete")
Expand Down
5 changes: 4 additions & 1 deletion tests/sample_data/simple_json.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple JSON object</title>
<title>Simple JSON objects</title>
</head>
<body>
<pre id="obj">{"a": "x", "b": 3.14159}</pre>
<pre id="zero">0</pre>
<pre id="empty_array">[]</pre>
<pre id="null">null</pre>
</body>
</html>
20 changes: 9 additions & 11 deletions zendriver/core/tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import urllib.parse
import warnings
import webbrowser
from typing import TYPE_CHECKING, Any, List, Literal, Optional, Tuple, Union
from typing import TYPE_CHECKING, Any, List, Literal, Optional, Tuple, Union, cast

from .intercept import BaseFetchInterception
from .. import cdp
Expand All @@ -21,6 +21,7 @@
from .expect import DownloadExpectation, RequestExpectation, ResponseExpectation
from ..cdp.fetch import RequestStage
from ..cdp.network import ResourceType
from ..cdp.runtime import DeepSerializedValue


if TYPE_CHECKING:
Expand Down Expand Up @@ -739,15 +740,11 @@ async def evaluate(
if errors:
raise ProtocolException(errors)

if remote_object:
if return_by_value:
if remote_object.value:
return remote_object.value
else:
if remote_object.deep_serialized_value:
return remote_object.deep_serialized_value.value

return remote_object, errors
if return_by_value:
return remote_object.value
# deep_serialized_value is guaranteed to be present when
# serialization_options.serialization="deep"
return cast(DeepSerializedValue, remote_object.deep_serialized_value).value

async def js_dumps(
self, obj_name: str, return_by_value: Optional[bool] = True
Expand Down Expand Up @@ -920,9 +917,10 @@ async def js_dumps(

if exception_details:
raise ProtocolException(exception_details)
if return_by_value and remote_object.value:
if return_by_value:
return remote_object.value
else:
# TODO Why not remote_object.deep_serialized_value.value?
return remote_object, exception_details

async def close(self) -> None:
Expand Down
Loading