@@ -183,6 +183,51 @@ def test_add_preload_script_with_contexts(driver, pages):
183183 assert result .result ["value" ] is True
184184
185185
186+ def test_add_preload_script_with_user_contexts (driver , pages ):
187+ """Test adding a preload script with user contexts."""
188+ function_declaration = "() => { window.contextSpecific = true; }"
189+ user_context = driver .browser .create_user_context ()
190+
191+ context1 = driver .browsing_context .create (type = "window" , user_context = user_context )
192+ driver .switch_to .window (context1 )
193+
194+ user_contexts = [user_context ]
195+
196+ script_id = driver .script .add_preload_script (function_declaration , user_contexts = user_contexts )
197+ assert script_id is not None
198+
199+ pages .load ("blank.html" )
200+
201+ result = driver .script .evaluate (
202+ "window.contextSpecific" , {"context" : driver .current_window_handle }, await_promise = False
203+ )
204+ assert result .result ["value" ] is True
205+
206+
207+ def test_add_preload_script_with_sandbox (driver , pages ):
208+ """Test adding a preload script with sandbox."""
209+ function_declaration = "() => { window.sandboxScript = true; }"
210+
211+ script_id = driver .script .add_preload_script (function_declaration , sandbox = "test-sandbox" )
212+ assert script_id is not None
213+
214+ pages .load ("blank.html" )
215+
216+ # calling evaluate without sandbox should return undefined
217+ result = driver .script .evaluate (
218+ "window.sandboxScript" , {"context" : driver .current_window_handle }, await_promise = False
219+ )
220+ assert result .result ["type" ] == "undefined"
221+
222+ # calling evaluate within the sandbox should return True
223+ result = driver .script .evaluate (
224+ "window.sandboxScript" ,
225+ {"context" : driver .current_window_handle , "sandbox" : "test-sandbox" },
226+ await_promise = False ,
227+ )
228+ assert result .result ["value" ] is True
229+
230+
186231def test_add_preload_script_invalid_arguments (driver ):
187232 """Test that providing both contexts and user_contexts raises an error."""
188233 function_declaration = "() => {}"
@@ -244,6 +289,91 @@ def test_evaluate_with_exception(driver, pages):
244289 assert "Test error" in str (result .exception_details )
245290
246291
292+ def test_evaluate_with_result_ownership (driver , pages ):
293+ """Test evaluating with different result ownership settings."""
294+ pages .load ("blank.html" )
295+
296+ # Test with ROOT ownership
297+ result = driver .script .evaluate (
298+ "({ test: 'value' })" ,
299+ {"context" : driver .current_window_handle },
300+ await_promise = False ,
301+ result_ownership = ResultOwnership .ROOT ,
302+ )
303+
304+ assert result .result is not None
305+
306+ # Test with NONE ownership
307+ result = driver .script .evaluate (
308+ "({ test: 'value' })" ,
309+ {"context" : driver .current_window_handle },
310+ await_promise = False ,
311+ result_ownership = ResultOwnership .NONE ,
312+ )
313+
314+ assert result .result is not None
315+
316+
317+ def test_evaluate_with_serialization_options (driver , pages ):
318+ """Test evaluating with serialization options."""
319+ pages .load ("blank.html" )
320+
321+ serialization_options = {"maxDomDepth" : 1 , "maxObjectDepth" : 1 }
322+
323+ result = driver .script .evaluate (
324+ "document.body" ,
325+ {"context" : driver .current_window_handle },
326+ await_promise = False ,
327+ serialization_options = serialization_options ,
328+ )
329+
330+ assert result .result is not None
331+
332+
333+ def test_evaluate_with_user_activation (driver , pages ):
334+ """Test evaluating with user activation."""
335+ pages .load ("blank.html" )
336+
337+ result = driver .script .evaluate (
338+ "navigator.userActivation ? navigator.userActivation.isActive : false" ,
339+ {"context" : driver .current_window_handle },
340+ await_promise = False ,
341+ user_activation = True ,
342+ )
343+
344+ assert result .result is not None
345+ # try to verify user_activation
346+
347+
348+ # def test_evaluate_clipboard_copy_and_read_with_user_activation(driver, pages):
349+ # pages.load("blank.html")
350+ #
351+ # # Step 1: Write some text and copy it
352+ # driver.script.evaluate(
353+ # """
354+ # const el = document.createElement("textarea");
355+ # el.value = "Copied from test!";
356+ # document.body.appendChild(el);
357+ # el.select();
358+ # document.execCommand("copy");
359+ # """,
360+ # {"context": driver.current_window_handle},
361+ # await_promise=True,
362+ # user_activation=True
363+ # )
364+ #
365+ # # Step 2: Read from the clipboard
366+ # result2 = driver.script.evaluate(
367+ # "navigator.clipboard.readText()",
368+ # {"context": driver.current_window_handle},
369+ # await_promise=True,
370+ # user_activation=True
371+ # )
372+ #
373+ # assert result2.result["type"] == "string"
374+ # assert result2.result["value"] == "Copied from test!"
375+
376+
247377def test_call_function (driver , pages ):
248378 """Test calling a function."""
249379 pages .load ("blank.html" )
@@ -294,6 +424,70 @@ def test_call_function_with_user_activation(driver, pages):
294424 assert result .result is not None
295425
296426
427+ def test_call_function_with_serialization_options (driver , pages ):
428+ """Test calling a function with serialization options."""
429+ pages .load ("blank.html" )
430+
431+ serialization_options = {"maxDomDepth" : 1 , "maxObjectDepth" : 1 }
432+
433+ result = driver .script .call_function (
434+ "() => document.body" ,
435+ await_promise = False ,
436+ target = {"context" : driver .current_window_handle },
437+ serialization_options = serialization_options ,
438+ )
439+
440+ assert result .result is not None
441+
442+
443+ def test_call_function_with_exception (driver , pages ):
444+ """Test calling a function that throws an exception."""
445+ pages .load ("blank.html" )
446+
447+ result = driver .script .call_function (
448+ "() => { throw new Error('Function error'); }" ,
449+ await_promise = False ,
450+ target = {"context" : driver .current_window_handle },
451+ )
452+
453+ assert result .exception_details is not None
454+ assert "Function error" in str (result .exception_details )
455+
456+
457+ def test_call_function_with_await_promise (driver , pages ):
458+ """Test calling a function that returns a promise."""
459+ pages .load ("blank.html" )
460+
461+ result = driver .script .call_function (
462+ "() => Promise.resolve('async result')" , await_promise = True , target = {"context" : driver .current_window_handle }
463+ )
464+
465+ assert result .result ["type" ] == "string"
466+ assert result .result ["value" ] == "async result"
467+
468+
469+ def test_call_function_with_result_ownership (driver , pages ):
470+ """Test calling a function with different result ownership settings."""
471+ pages .load ("blank.html" )
472+ # Test with ROOT ownership
473+ result = driver .script .call_function (
474+ "() => ({ test: 'value' })" ,
475+ await_promise = False ,
476+ 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' })" ,
483+ await_promise = False ,
484+ target = {"context" : driver .current_window_handle },
485+ result_ownership = ResultOwnership .NONE ,
486+ )
487+ assert result .result is not None
488+ # have a better way to test the ownership behavior
489+
490+
297491def test_get_realms (driver , pages ):
298492 """Test getting all realms."""
299493 pages .load ("blank.html" )
@@ -326,7 +520,7 @@ def test_get_realms_filtered_by_type(driver, pages):
326520 realms = driver .script .get_realms (type = RealmType .WINDOW )
327521
328522 assert len (realms ) > 0
329- # All realms should be of the specified type
523+ # All realms should be of the WINDOW type
330524 for realm in realms :
331525 assert realm .type == RealmType .WINDOW
332526
@@ -352,113 +546,3 @@ def test_disown_handles(driver, pages):
352546
353547 # The disown operation should complete without error
354548 # Note: We can't easily test the actual garbage collection behavior, try something
355-
356-
357- def test_evaluate_with_result_ownership (driver , pages ):
358- """Test evaluating with different result ownership settings."""
359- pages .load ("blank.html" )
360-
361- # Test with ROOT ownership
362- result = driver .script .evaluate (
363- "({ test: 'value' })" ,
364- {"context" : driver .current_window_handle },
365- await_promise = False ,
366- result_ownership = ResultOwnership .ROOT ,
367- )
368-
369- assert result .result is not None
370-
371- # Test with NONE ownership
372- result = driver .script .evaluate (
373- "({ test: 'value' })" ,
374- {"context" : driver .current_window_handle },
375- await_promise = False ,
376- result_ownership = ResultOwnership .NONE ,
377- )
378-
379- assert result .result is not None
380-
381-
382- def test_evaluate_with_serialization_options (driver , pages ):
383- """Test evaluating with serialization options."""
384- pages .load ("blank.html" )
385-
386- serialization_options = {"maxDomDepth" : 1 , "maxObjectDepth" : 1 }
387-
388- result = driver .script .evaluate (
389- "document.body" ,
390- {"context" : driver .current_window_handle },
391- await_promise = False ,
392- serialization_options = serialization_options ,
393- )
394-
395- assert result .result is not None
396-
397-
398- def test_call_function_with_serialization_options (driver , pages ):
399- """Test calling a function with serialization options."""
400- pages .load ("blank.html" )
401-
402- serialization_options = {"maxDomDepth" : 1 , "maxObjectDepth" : 1 }
403-
404- result = driver .script .call_function (
405- "() => document.body" ,
406- await_promise = False ,
407- target = {"context" : driver .current_window_handle },
408- serialization_options = serialization_options ,
409- )
410-
411- assert result .result is not None
412-
413-
414- def test_add_preload_script_with_sandbox (driver , pages ):
415- """Test adding a preload script with sandbox."""
416- function_declaration = "() => { window.sandboxScript = true; }"
417-
418- script_id = driver .script .add_preload_script (function_declaration , sandbox = "test-sandbox" )
419- assert script_id is not None
420-
421- pages .load ("blank.html" )
422-
423- # The script should execute in the sandbox, have a check to verify this
424-
425-
426- def test_evaluate_with_user_activation (driver , pages ):
427- """Test evaluating with user activation."""
428- pages .load ("blank.html" )
429-
430- result = driver .script .evaluate (
431- "navigator.userActivation ? navigator.userActivation.isActive : false" ,
432- {"context" : driver .current_window_handle },
433- await_promise = False ,
434- user_activation = True ,
435- )
436-
437- assert result .result is not None
438- # try to verify user_activation
439-
440-
441- def test_call_function_with_exception (driver , pages ):
442- """Test calling a function that throws an exception."""
443- pages .load ("blank.html" )
444-
445- result = driver .script .call_function (
446- "() => { throw new Error('Function error'); }" ,
447- await_promise = False ,
448- target = {"context" : driver .current_window_handle },
449- )
450-
451- assert result .exception_details is not None
452- assert "Function error" in str (result .exception_details )
453-
454-
455- def test_call_function_with_await_promise (driver , pages ):
456- """Test calling a function that returns a promise."""
457- pages .load ("blank.html" )
458-
459- result = driver .script .call_function (
460- "() => Promise.resolve('async result')" , await_promise = True , target = {"context" : driver .current_window_handle }
461- )
462-
463- assert result .result ["type" ] == "string"
464- assert result .result ["value" ] == "async result"
0 commit comments