Skip to content

Commit

Permalink
[wptrunner] Introduce ChromeDriverDevToolsProtocolPart
Browse files Browse the repository at this point in the history
... to encapsulate direct interactions with CDP. The `goog` or `ms`
vendor prefix has been lifted into a per-`Protocol` setting as well to
reduce boilerplate.

This should be a no-op refactor.
  • Loading branch information
jonathan-j-lee committed May 6, 2024
1 parent 6c11254 commit 05203cd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 55 deletions.
67 changes: 34 additions & 33 deletions tools/wptrunner/wptrunner/executors/executorchrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
WebDriverTestharnessExecutor,
WebDriverTestharnessProtocolPart,
)
from .protocol import PrintProtocolPart
from .protocol import PrintProtocolPart, ProtocolPart

here = os.path.dirname(__file__)

Expand Down Expand Up @@ -80,8 +80,6 @@ def setup(self):
# exposed to the base WebDriver testharness executor.
self.test_window = None
self.reuse_window = self.parent.reuse_window
# Company prefix to apply to vendor-specific WebDriver extension commands.
self.cdp_company_prefix = "goog"

def close_test_window(self):
if self.test_window:
Expand All @@ -106,13 +104,7 @@ def open_test_window(self, window_id):
self.webdriver.window_handle = self.test_window
# Reset navigation history with Chrome DevTools Protocol:
# https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-resetNavigationHistory
body = {
"cmd": "Page.resetNavigationHistory",
"params": {},
}
self.webdriver.send_session_command("POST",
self.cdp_company_prefix + "/cdp/execute",
body=body)
self.parent.cdp.execute_cdp_command("Page.resetNavigationHistory")
self.webdriver.url = "about:blank"
return
except error.NoSuchWindowException:
Expand All @@ -139,8 +131,6 @@ class ChromeDriverPrintProtocolPart(PrintProtocolPart):
def setup(self):
self.webdriver = self.parent.webdriver
self.runner_handle = None
# Company prefix to apply to vendor-specific WebDriver extension commands.
self.cdp_company_prefix = "goog"

def load_runner(self):
url = urljoin(self.parent.executor.server_url("http"), "/print_pdf_runner.html")
Expand All @@ -158,23 +148,18 @@ def load_runner(self):

def render_as_pdf(self, width, height):
margin = 0.5
body = {
"cmd": "Page.printToPDF",
"params": {
# Chrome accepts dimensions in inches; we are using cm
"paperWidth": width / 2.54,
"paperHeight": height / 2.54,
"marginLeft": margin,
"marginRight": margin,
"marginTop": margin,
"marginBottom": margin,
"shrinkToFit": False,
"printBackground": True,
}
params = {
# Chrome accepts dimensions in inches; we are using cm
"paperWidth": width / 2.54,
"paperHeight": height / 2.54,
"marginLeft": margin,
"marginRight": margin,
"marginTop": margin,
"marginBottom": margin,
"shrinkToFit": False,
"printBackground": True,
}
return self.webdriver.send_session_command("POST",
self.cdp_company_prefix + "/cdp/execute",
body=body)["data"]
return self.parent.cdp.execute_cdp_command("Page.printToPDF", params)["data"]

def pdf_to_png(self, pdf_base64, ranges):
handle = self.webdriver.window_handle
Expand All @@ -191,19 +176,33 @@ def pdf_to_png(self, pdf_base64, ranges):


class ChromeDriverFedCMProtocolPart(WebDriverFedCMProtocolPart):
def confirm_idp_login(self):
return self.webdriver.send_session_command("POST",
f"{self.parent.vendor_prefix}/fedcm/confirmidplogin")


class ChromeDriverDevToolsProtocolPart(ProtocolPart):
"""A low-level API for sending Chrome DevTools Protocol [0] commands directly to the browser.
Prefer using standard APIs where possible.
[0]: https://chromedevtools.github.io/devtools-protocol/
"""
name = "cdp"

def setup(self):
self.webdriver = self.parent.webdriver
# Company prefix to apply to vendor-specific WebDriver extension commands.
self.fedcm_company_prefix = "goog"


def confirm_idp_login(self):
def execute_cdp_command(self, command, params=None):
body = {"cmd": command, "params": params or {}}
return self.webdriver.send_session_command("POST",
self.fedcm_company_prefix + "/fedcm/confirmidplogin")
f"{self.parent.vendor_prefix}/cdp/execute",
body=body)


class ChromeDriverProtocol(WebDriverProtocol):
implements = [
ChromeDriverDevToolsProtocolPart,
ChromeDriverFedCMProtocolPart,
ChromeDriverPrintProtocolPart,
ChromeDriverTestharnessProtocolPart,
Expand All @@ -212,6 +211,8 @@ class ChromeDriverProtocol(WebDriverProtocol):
part.name != ChromeDriverFedCMProtocolPart.name)
]
reuse_window = False
# Prefix to apply to vendor-specific WebDriver extension commands.
vendor_prefix = "goog"


class ChromeDriverRefTestExecutor(WebDriverRefTestExecutor, _SanitizerMixin): # type: ignore
Expand Down
25 changes: 3 additions & 22 deletions tools/wptrunner/wptrunner/executors/executoredge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,23 @@

from .executorwebdriver import (
WebDriverCrashtestExecutor,
WebDriverProtocol,
WebDriverRefTestExecutor,
WebDriverRun,
WebDriverTestharnessExecutor,
)

from .executorchrome import (
ChromeDriverPrintProtocolPart,
ChromeDriverTestharnessProtocolPart,
ChromeDriverProtocol,
make_sanitizer_mixin,
)

here = os.path.dirname(__file__)

_SanitizerMixin = make_sanitizer_mixin(WebDriverCrashtestExecutor)

class EdgeDriverTestharnessProtocolPart(ChromeDriverTestharnessProtocolPart):
def setup(self):
super().setup()
self.cdp_company_prefix = "ms"


class EdgeDriverPrintProtocolPart(ChromeDriverPrintProtocolPart):
def setup(self):
super().setup()
self.cdp_company_prefix = "ms"


class EdgeDriverProtocol(WebDriverProtocol):
implements = [
EdgeDriverPrintProtocolPart,
EdgeDriverTestharnessProtocolPart,
*(part for part in WebDriverProtocol.implements
if part.name != EdgeDriverTestharnessProtocolPart.name)
]
reuse_window = False
class EdgeDriverProtocol(ChromeDriverProtocol):
vendor_prefix = "ms"


class EdgeDriverRefTestExecutor(WebDriverRefTestExecutor, _SanitizerMixin): # type: ignore
Expand Down

0 comments on commit 05203cd

Please sign in to comment.