Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions py/selenium/webdriver/common/bidi/emulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,41 @@ def set_locale_override(
params["userContexts"] = user_contexts

self.conn.execute(command_builder("emulation.setLocaleOverride", params))

def set_scripting_enabled(
self,
enabled: Union[bool, None] = False,
contexts: Optional[list[str]] = None,
user_contexts: Optional[list[str]] = None,
) -> None:
"""Set scripting enabled override for the given contexts or user contexts.

Parameters:
-----------
enabled: False to disable scripting, None to clear the override.
Note: Only emulation of disabled JavaScript is supported.
contexts: List of browsing context IDs to apply the override to.
user_contexts: List of user context IDs to apply the override to.

Raises:
------
ValueError: If both contexts and user_contexts are provided, or if neither
contexts nor user_contexts are provided, or if enabled is True.
"""
if enabled:
raise ValueError("Only emulation of disabled JavaScript is supported (enabled must be False or None)")

if contexts is not None and user_contexts is not None:
raise ValueError("Cannot specify both contexts and userContexts")

if contexts is None and user_contexts is None:
raise ValueError("Must specify either contexts or userContexts")

params: dict[str, Any] = {"enabled": enabled}

if contexts is not None:
params["contexts"] = contexts
elif user_contexts is not None:
params["userContexts"] = user_contexts

self.conn.execute(command_builder("emulation.setScriptingEnabled", params))
71 changes: 71 additions & 0 deletions py/test/selenium/webdriver/common/bidi_emulation_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,74 @@ def test_set_locale_override_with_user_contexts(driver, pages, value):
driver.browsing_context.close(context_id)
finally:
driver.browser.remove_user_context(user_context)


@pytest.mark.xfail_firefox(reason="Not yet supported")
def test_set_scripting_enabled_with_contexts(driver, pages):
"""Test disabling scripting with browsing contexts."""
context_id = driver.current_window_handle

# disable scripting
driver.emulation.set_scripting_enabled(enabled=False, contexts=[context_id])

driver.browsing_context.navigate(
context=context_id,
url="data:text/html,<script>window.foo=123;</script>",
wait="complete",
)
result = driver.script._evaluate("'foo' in window", {"context": context_id}, await_promise=False)
assert result.result["value"] is False, "Page script should not have executed when scripting is disabled"

# clear override via None to restore JS
driver.emulation.set_scripting_enabled(enabled=None, contexts=[context_id])
driver.browsing_context.navigate(
context=context_id,
url="data:text/html,<script>window.foo=123;</script>",
wait="complete",
)
result = driver.script._evaluate("'foo' in window", {"context": context_id}, await_promise=False)
assert result.result["value"] is True, "Page script should execute after clearing the override"


@pytest.mark.xfail_firefox(reason="Not yet supported")
def test_set_scripting_enabled_with_user_contexts(driver, pages):
"""Test disabling scripting with user contexts."""
user_context = driver.browser.create_user_context()
try:
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
try:
driver.switch_to.window(context_id)

driver.emulation.set_scripting_enabled(enabled=False, user_contexts=[user_context])

url = pages.url("javascriptPage.html")
driver.browsing_context.navigate(context_id, url, wait="complete")

# Check that inline event handlers don't work; this page has an onclick handler
click_field = driver.find_element("id", "clickField")
initial_value = click_field.get_attribute("value") # initial value is 'Hello'
click_field.click()

# Get the value after click, it should remain unchanged if scripting is disabled
result_value = driver.script._evaluate(
"document.getElementById('clickField').value", {"context": context_id}, await_promise=False
)
assert result_value.result["value"] == initial_value, (
"Inline onclick handler should not execute, i.e, value should not change to 'clicked'"
)

# Clear the scripting override
driver.emulation.set_scripting_enabled(enabled=None, user_contexts=[user_context])

driver.browsing_context.navigate(context_id, url, wait="complete")

# Click the element again, it should change to 'Clicked' now
driver.find_element("id", "clickField").click()
result_value = driver.script._evaluate(
"document.getElementById('clickField').value", {"context": context_id}, await_promise=False
)
assert result_value.result["value"] == "Clicked"
finally:
driver.browsing_context.close(context_id)
finally:
driver.browser.remove_user_context(user_context)
Loading