Skip to content

Commit

Permalink
Fixed SeleniumTestCase.set_emulated_media() for Selenium Grid.
Browse files Browse the repository at this point in the history
When using Selenium Grid via the `--selenium-hub` option, we cannot use
the `.execute_cdp_cmd()` method which doesn't exist on the remote
driver.
  • Loading branch information
ngnpope committed May 9, 2024
1 parent b149ca6 commit 6e45237
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions django/test/selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,25 @@ def dark(self):
finally:
self.selenium.execute_script("localStorage.removeItem('theme');")

def set_emulated_media(self, features, media=""):
def set_emulated_media(self, *, media=None, features=None):
if self.browser not in {"chrome", "edge"}:
self.skipTest("Emulated media controls are only supported on Chrome/Edge.")

# Chrome Dev Tools Protocol Emulation.setEmulatedMedia
# https://chromedevtools.github.io/devtools-protocol/1-3/Emulation/#method-setEmulatedMedia
self.selenium.execute_cdp_cmd(
"Emulation.setEmulatedMedia", {"media": media, "features": features}

# Not using .execute_cdp_cmd() as it isn't supported by the remote web driver
# when using Selenium Grid.

params = {}
if media is not None:
params["media"] = media
if features is not None:
params["features"] = features

self.selenium.execute(
driver_command="executeCdpCommand",
params={"cmd": "Emulation.setEmulatedMedia", "params": params},
)

@contextmanager
Expand Down

0 comments on commit 6e45237

Please sign in to comment.