-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade to Selenium 4 and use CDP for Performance metrics (#233)
Co-authored-by: Carlos Kidman <carlos@qap.dev>
- Loading branch information
Showing
9 changed files
with
115 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" Chrome DevTools Protocol (CDP) introduced in Selenium 4. | ||
Resources: | ||
- https://www.selenium.dev/documentation/webdriver/bidirectional/chrome_devtools/ | ||
- https://chromedevtools.github.io/devtools-protocol/ | ||
* Currently only supports the Chrome Browser, although some chromium browsers may work as well. | ||
""" | ||
|
||
from typing import Dict | ||
|
||
|
||
class CDP: | ||
"""Chrome DevTools Protocol.""" | ||
|
||
def __init__(self, webdriver): | ||
self._webdriver = webdriver | ||
|
||
def execute_command(self, cmd: str, cmd_args: Dict) -> Dict: | ||
"""Execute Chrome Devtools Protocol command and get returned result. | ||
The command and command args should follow chrome devtools protocol domains/commands, refer to link | ||
https://chromedevtools.github.io/devtools-protocol/ | ||
Args: | ||
cmd: The command name | ||
cmd_args: The command args. Pass an empty dict {} if there is no command args | ||
Examples: | ||
py.cdp.execute_command('Network.getResponseBody', {'requestId': requestId}) | ||
Returns: | ||
A dict of results or an empty dict {} if there is no result to return. | ||
For example, to getResponseBody: | ||
{'base64Encoded': False, 'body': 'response body string'} | ||
""" | ||
return self._webdriver.execute_cdp_cmd(cmd, cmd_args) | ||
|
||
def get_performance_metrics(self) -> Dict: | ||
"""Get performance metrics from Chrome DevTools - similar to the Performance tab in Chrome. | ||
Examples: | ||
metrics = py.cdp.get_performance_metrics() | ||
Returns: | ||
A dict of performance metrics including 'ScriptDuration', 'ThreadTime', 'ProcessTime', and 'DomContentLoaded'. | ||
{'metrics': [ | ||
{'name': 'Timestamp', 'value': 425608.80694}, | ||
{'name': 'AudioHandlers', 'value': 0}, | ||
{'name': 'ThreadTime', 'value': 0.002074}, | ||
... | ||
] | ||
} | ||
""" | ||
# The commented out code below should have been executed prior to this function call. | ||
# self._webdriver.execute_cdp_cmd("Performance.enable", {}) | ||
return self._webdriver.execute_cdp_cmd("Performance.getMetrics", {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" Chrome DevTools Protocol - Performance Tab """ | ||
from pylenium.driver import Pylenium | ||
|
||
|
||
def test_capture_performance_metrics(py: Pylenium): | ||
py.visit("https://qap.dev") | ||
metrics = py.cdp.get_performance_metrics() | ||
assert metrics["metrics"] | ||
assert metrics["metrics"][0]["name"] == "Timestamp" | ||
assert metrics["metrics"][0]["value"] > 0 |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
import pytest | ||
from pylenium.driver import Pylenium | ||
|
||
def test_add_to_cart(py): | ||
py.visit('https://jane.com') | ||
py.get('[data-testid="deal-image"]').click() | ||
|
||
for dropdown in py.find('select'): | ||
dropdown.select(1) | ||
@pytest.fixture | ||
def sauce(py: Pylenium) -> Pylenium: | ||
"""Login to saucedemo.com as standard user.""" | ||
py.visit("https://www.saucedemo.com/") | ||
py.get("#user-name").type("standard_user") | ||
py.get("#password").type("secret_sauce") | ||
py.get("#login-button").click() | ||
yield py | ||
py.get("#react-burger-menu-btn").click() | ||
py.get("#logout_sidebar_link").should().be_visible().click() | ||
|
||
py.get('[data-testid="add-to-bag"]', timeout=1).click() | ||
assert py.contains("$00.11") | ||
|
||
def test_add_to_cart_css(sauce: Pylenium): | ||
"""Add an item to the cart. The number badge on the cart icon should increment as expected.""" | ||
sauce.get("[id*='add-to-cart']").click() | ||
assert sauce.get("a.shopping_cart_link").should().have_text("1") | ||
|
||
def test_add_to_cart_xpath(py): | ||
py.visit('https://jane.com') | ||
py.getx('//*[@data-testid="deal-image"]').click() | ||
|
||
for dropdown in py.findx('//select'): | ||
dropdown.select(1) | ||
|
||
py.getx('//*[@data-testid="add-to-bag"]', timeout=1).click() | ||
assert py.contains("$00.11") | ||
def test_add_to_cart_xpath(sauce: Pylenium): | ||
"""Add 6 different items to the cart. There should be 6 items in the cart.""" | ||
for button in sauce.findx("//*[contains(@id, 'add-to-cart')]"): | ||
button.click() | ||
sauce.getx("//a[@class='shopping_cart_link']").click() | ||
assert sauce.findx("//*[@class='cart_item']").should().have_length(6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters