2323from 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+
2640def 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
317333def 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
333353def test_evaluate_with_user_activation (driver , pages ):
@@ -426,9 +446,9 @@ def test_call_function_with_user_activation(driver, pages):
426446
427447def 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
443468def test_call_function_with_exception (driver , pages ):
@@ -469,23 +494,30 @@ def test_call_function_with_await_promise(driver, pages):
469494def 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
491523def test_get_realms (driver , pages ):
0 commit comments