@@ -362,3 +362,74 @@ def test_set_locale_override_with_user_contexts(driver, pages, value):
362362 driver .browsing_context .close (context_id )
363363 finally :
364364 driver .browser .remove_user_context (user_context )
365+
366+
367+ @pytest .mark .xfail_firefox (reason = "Not yet supported" )
368+ def test_set_scripting_enabled_with_contexts (driver , pages ):
369+ """Test disabling scripting with browsing contexts."""
370+ context_id = driver .current_window_handle
371+
372+ # disable scripting
373+ driver .emulation .set_scripting_enabled (enabled = False , contexts = [context_id ])
374+
375+ driver .browsing_context .navigate (
376+ context = context_id ,
377+ url = "data:text/html,<script>window.foo=123;</script>" ,
378+ wait = "complete" ,
379+ )
380+ result = driver .script ._evaluate ("'foo' in window" , {"context" : context_id }, await_promise = False )
381+ assert result .result ["value" ] is False , "Page script should not have executed when scripting is disabled"
382+
383+ # clear override via None to restore JS
384+ driver .emulation .set_scripting_enabled (enabled = None , contexts = [context_id ])
385+ driver .browsing_context .navigate (
386+ context = context_id ,
387+ url = "data:text/html,<script>window.foo=123;</script>" ,
388+ wait = "complete" ,
389+ )
390+ result = driver .script ._evaluate ("'foo' in window" , {"context" : context_id }, await_promise = False )
391+ assert result .result ["value" ] is True , "Page script should execute after clearing the override"
392+
393+
394+ @pytest .mark .xfail_firefox (reason = "Not yet supported" )
395+ def test_set_scripting_enabled_with_user_contexts (driver , pages ):
396+ """Test disabling scripting with user contexts."""
397+ user_context = driver .browser .create_user_context ()
398+ try :
399+ context_id = driver .browsing_context .create (type = WindowTypes .TAB , user_context = user_context )
400+ try :
401+ driver .switch_to .window (context_id )
402+
403+ driver .emulation .set_scripting_enabled (enabled = False , user_contexts = [user_context ])
404+
405+ url = pages .url ("javascriptPage.html" )
406+ driver .browsing_context .navigate (context_id , url , wait = "complete" )
407+
408+ # Check that inline event handlers don't work; this page has an onclick handler
409+ click_field = driver .find_element ("id" , "clickField" )
410+ initial_value = click_field .get_attribute ("value" ) # initial value is 'Hello'
411+ click_field .click ()
412+
413+ # Get the value after click, it should remain unchanged if scripting is disabled
414+ result_value = driver .script ._evaluate (
415+ "document.getElementById('clickField').value" , {"context" : context_id }, await_promise = False
416+ )
417+ assert result_value .result ["value" ] == initial_value , (
418+ "Inline onclick handler should not execute, i.e, value should not change to 'clicked'"
419+ )
420+
421+ # Clear the scripting override
422+ driver .emulation .set_scripting_enabled (enabled = None , user_contexts = [user_context ])
423+
424+ driver .browsing_context .navigate (context_id , url , wait = "complete" )
425+
426+ # Click the element again, it should change to 'Clicked' now
427+ driver .find_element ("id" , "clickField" ).click ()
428+ result_value = driver .script ._evaluate (
429+ "document.getElementById('clickField').value" , {"context" : context_id }, await_promise = False
430+ )
431+ assert result_value .result ["value" ] == "Clicked"
432+ finally :
433+ driver .browsing_context .close (context_id )
434+ finally :
435+ driver .browser .remove_user_context (user_context )
0 commit comments