Skip to content

Commit df26042

Browse files
committed
modify tests for result_ownership and serialization_options
1 parent 831cd0c commit df26042

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

py/selenium/webdriver/common/bidi/script.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def from_json(cls, json: dict) -> "Source":
104104
class EvaluateResult:
105105
"""Represents the result of script evaluation."""
106106

107+
type: str
107108
realm: str
108109
result: Optional[dict] = None
109110
exception_details: Optional[dict] = None
@@ -121,6 +122,7 @@ def from_json(cls, json: dict) -> "EvaluateResult":
121122
EvaluateResult: A new instance of EvaluateResult.
122123
"""
123124
return cls(
125+
type=json.get("type"),
124126
realm=json.get("realm"),
125127
result=json.get("result"),
126128
exception_details=json.get("exceptionDetails"),

py/test/selenium/webdriver/common/bidi_script_tests.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323
from selenium.webdriver.support.ui import WebDriverWait
2424

2525

26+
def has_shadow_root(node):
27+
if isinstance(node, dict):
28+
shadow_root = node.get("shadowRoot")
29+
if shadow_root and isinstance(shadow_root, dict):
30+
return True
31+
32+
children = node.get("children", [])
33+
for child in children:
34+
if "value" in child and has_shadow_root(child["value"]):
35+
return True
36+
37+
return False
38+
39+
2640
def test_logs_console_messages(driver, pages):
2741
pages.load("bidi/logEntryAdded.html")
2842

@@ -301,7 +315,8 @@ def test_evaluate_with_result_ownership(driver, pages):
301315
result_ownership=ResultOwnership.ROOT,
302316
)
303317

304-
assert result.result is not None
318+
# ROOT result ownership should return a handle
319+
assert "handle" in result.result
305320

306321
# Test with NONE ownership
307322
result = driver.script.evaluate(
@@ -311,23 +326,28 @@ def test_evaluate_with_result_ownership(driver, pages):
311326
result_ownership=ResultOwnership.NONE,
312327
)
313328

329+
assert "handle" not in result.result
314330
assert result.result is not None
315331

316332

317333
def test_evaluate_with_serialization_options(driver, pages):
318334
"""Test evaluating with serialization options."""
319-
pages.load("blank.html")
335+
pages.load("shadowRootPage.html")
320336

321-
serialization_options = {"maxDomDepth": 1, "maxObjectDepth": 1}
337+
serialization_options = {"maxDomDepth": 2, "maxObjectDepth": 2, "includeShadowTree": 'all'}
322338

323339
result = driver.script.evaluate(
324340
"document.body",
325341
{"context": driver.current_window_handle},
326342
await_promise=False,
327343
serialization_options=serialization_options,
328344
)
345+
root_node = result.result["value"]
329346

330-
assert result.result is not None
347+
# maxDomDepth will contain a children property
348+
assert 'children' in result.result['value']
349+
# the page will have atleast one shadow root
350+
assert has_shadow_root(root_node)
331351

332352

333353
def test_evaluate_with_user_activation(driver, pages):
@@ -426,9 +446,9 @@ def test_call_function_with_user_activation(driver, pages):
426446

427447
def test_call_function_with_serialization_options(driver, pages):
428448
"""Test calling a function with serialization options."""
429-
pages.load("blank.html")
449+
pages.load("shadowRootPage.html")
430450

431-
serialization_options = {"maxDomDepth": 1, "maxObjectDepth": 1}
451+
serialization_options = {"maxDomDepth": 2, "maxObjectDepth": 2, "includeShadowTree": 'all'}
432452

433453
result = driver.script.call_function(
434454
"() => document.body",
@@ -437,7 +457,12 @@ def test_call_function_with_serialization_options(driver, pages):
437457
serialization_options=serialization_options,
438458
)
439459

440-
assert result.result is not None
460+
root_node = result.result["value"]
461+
462+
# maxDomDepth will contain a children property
463+
assert 'children' in result.result['value']
464+
# the page will have atleast one shadow root
465+
assert has_shadow_root(root_node)
441466

442467

443468
def test_call_function_with_exception(driver, pages):
@@ -469,23 +494,30 @@ def test_call_function_with_await_promise(driver, pages):
469494
def test_call_function_with_result_ownership(driver, pages):
470495
"""Test calling a function with different result ownership settings."""
471496
pages.load("blank.html")
472-
# Test with ROOT ownership
497+
498+
# Call a function that returns an object with ownership "root"
473499
result = driver.script.call_function(
474-
"() => ({ test: 'value' })",
475-
await_promise=False,
500+
"function() { return { greet: 'Hi', number: 42 }; }",
476501
target={"context": driver.current_window_handle},
477-
result_ownership=ResultOwnership.ROOT,
478-
)
479-
assert result.result is not None
480-
# Test with NONE ownership
481-
result = driver.script.call_function(
482-
"() => ({ test: 'value' })",
483502
await_promise=False,
503+
result_ownership="root"
504+
)
505+
506+
# Verify that a handle is returned
507+
assert result.result["type"] == "object"
508+
assert "handle" in result.result
509+
handle = result.result["handle"]
510+
511+
# Use the handle in another function call
512+
result2 = driver.script.call_function(
513+
"function() { return this.number + 1; }",
484514
target={"context": driver.current_window_handle},
485-
result_ownership=ResultOwnership.NONE,
515+
await_promise=False,
516+
this={"handle": handle}
486517
)
487-
assert result.result is not None
488-
# have a better way to test the ownership behavior
518+
519+
assert result2.result["type"] == "number"
520+
assert result2.result["value"] == 43
489521

490522

491523
def test_get_realms(driver, pages):

0 commit comments

Comments
 (0)