Skip to content

Commit eb4bc25

Browse files
committed
modify tests for disown and user_activation
1 parent df26042 commit eb4bc25

File tree

1 file changed

+33
-50
lines changed

1 file changed

+33
-50
lines changed

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

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def test_evaluate_with_serialization_options(driver, pages):
334334
"""Test evaluating with serialization options."""
335335
pages.load("shadowRootPage.html")
336336

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

339339
result = driver.script.evaluate(
340340
"document.body",
@@ -345,7 +345,7 @@ def test_evaluate_with_serialization_options(driver, pages):
345345
root_node = result.result["value"]
346346

347347
# maxDomDepth will contain a children property
348-
assert 'children' in result.result['value']
348+
assert "children" in result.result["value"]
349349
# the page will have atleast one shadow root
350350
assert has_shadow_root(root_node)
351351

@@ -361,37 +361,8 @@ def test_evaluate_with_user_activation(driver, pages):
361361
user_activation=True,
362362
)
363363

364-
assert result.result is not None
365-
# try to verify user_activation
366-
367-
368-
# def test_evaluate_clipboard_copy_and_read_with_user_activation(driver, pages):
369-
# pages.load("blank.html")
370-
#
371-
# # Step 1: Write some text and copy it
372-
# driver.script.evaluate(
373-
# """
374-
# const el = document.createElement("textarea");
375-
# el.value = "Copied from test!";
376-
# document.body.appendChild(el);
377-
# el.select();
378-
# document.execCommand("copy");
379-
# """,
380-
# {"context": driver.current_window_handle},
381-
# await_promise=True,
382-
# user_activation=True
383-
# )
384-
#
385-
# # Step 2: Read from the clipboard
386-
# result2 = driver.script.evaluate(
387-
# "navigator.clipboard.readText()",
388-
# {"context": driver.current_window_handle},
389-
# await_promise=True,
390-
# user_activation=True
391-
# )
392-
#
393-
# assert result2.result["type"] == "string"
394-
# assert result2.result["value"] == "Copied from test!"
364+
# the value should be True if user activation is active
365+
assert result.result["value"] is True
395366

396367

397368
def test_call_function(driver, pages):
@@ -440,15 +411,15 @@ def test_call_function_with_user_activation(driver, pages):
440411
user_activation=True,
441412
)
442413

443-
# Note: The actual behavior depends on browser implementation
444-
assert result.result is not None
414+
# the value should be True if user activation is active
415+
assert result.result["value"] is True
445416

446417

447418
def test_call_function_with_serialization_options(driver, pages):
448419
"""Test calling a function with serialization options."""
449420
pages.load("shadowRootPage.html")
450421

451-
serialization_options = {"maxDomDepth": 2, "maxObjectDepth": 2, "includeShadowTree": 'all'}
422+
serialization_options = {"maxDomDepth": 2, "maxObjectDepth": 2, "includeShadowTree": "all"}
452423

453424
result = driver.script.call_function(
454425
"() => document.body",
@@ -460,7 +431,7 @@ def test_call_function_with_serialization_options(driver, pages):
460431
root_node = result.result["value"]
461432

462433
# maxDomDepth will contain a children property
463-
assert 'children' in result.result['value']
434+
assert "children" in result.result["value"]
464435
# the page will have atleast one shadow root
465436
assert has_shadow_root(root_node)
466437

@@ -500,7 +471,7 @@ def test_call_function_with_result_ownership(driver, pages):
500471
"function() { return { greet: 'Hi', number: 42 }; }",
501472
target={"context": driver.current_window_handle},
502473
await_promise=False,
503-
result_ownership="root"
474+
result_ownership="root",
504475
)
505476

506477
# Verify that a handle is returned
@@ -513,7 +484,7 @@ def test_call_function_with_result_ownership(driver, pages):
513484
"function() { return this.number + 1; }",
514485
target={"context": driver.current_window_handle},
515486
await_promise=False,
516-
this={"handle": handle}
487+
this={"handle": handle},
517488
)
518489

519490
assert result2.result["type"] == "number"
@@ -561,20 +532,32 @@ def test_disown_handles(driver, pages):
561532
"""Test disowning handles."""
562533
pages.load("blank.html")
563534

564-
# First create some handles by evaluating an object
535+
# Create an object with root ownership (this will return a handle)
565536
result = driver.script.evaluate(
566-
"({ test: 'value' })",
567-
{"context": driver.current_window_handle},
537+
"({foo: 'bar'})", target={"context": driver.current_window_handle}, await_promise=False, result_ownership="root"
538+
)
539+
540+
handle = result.result["handle"]
541+
assert handle is not None
542+
543+
# Use the handle in a function call (this should succeed)
544+
result_before = driver.script.call_function(
545+
"function(obj) { return obj.foo; }",
568546
await_promise=False,
569-
result_ownership=ResultOwnership.ROOT,
547+
target={"context": driver.current_window_handle},
548+
arguments=[{"handle": handle}],
570549
)
571550

572-
# Extract handle from result (this would be implementation specific)
573-
if "handle" in result.result:
574-
handle = result.result["handle"]
551+
assert result_before.result["value"] == "bar"
575552

576-
# Disown the handle
577-
driver.script.disown(handles=[handle], target={"context": driver.current_window_handle})
553+
# Disown the handle
554+
driver.script.disown(handles=[handle], target={"context": driver.current_window_handle})
578555

579-
# The disown operation should complete without error
580-
# Note: We can't easily test the actual garbage collection behavior, try something
556+
# Try using the disowned handle (this should fail)
557+
with pytest.raises(Exception):
558+
driver.script.call_function(
559+
"function(obj) { return obj.foo; }",
560+
await_promise=False,
561+
target={"context": driver.current_window_handle},
562+
arguments=[{"handle": handle}],
563+
)

0 commit comments

Comments
 (0)