Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
43 changes: 43 additions & 0 deletions pylenium/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,49 @@ def get_resources(self):
except TimeoutException:
return None # because there were no Resources captured for the current web page

def mark(self, mark):
"""Add a named mark) to the browser's performance timeline."""
js = 'return performance.mark("{}");'.format(mark)
self._wait().until(lambda driver: driver.execute_script(js), "PerformanceMark not generated yet")

def measure(self, mark) -> float:
"""Return duration in miliseconds"""
js = 'return performance.measure("Measure", "{}");'.format(mark)
measured = self._wait().until(lambda driver: driver.execute_script(js), "PerformanceMeasure not generated yet")
return PerformanceMeasure(**measured).duration


class PerformanceMeasure(BaseModel):
"""The PerformanceMeasure Representation.

Metrics regarding the browser's document navigation events

References:
https://developer.mozilla.org/en-US/docs/Web/API/PerformanceMeasure
"""

detail: None = Field(alias="detail")
name: str = Field(alias="name")
entry_type: str = Field(alias="entryType")
start_time: float = Field(alias="startTime")
duration: float = Field(alias="duration")


class PerformanceMark(BaseModel):
"""The PerformanceMark Representation.

Metrics regarding the browser's document navigation events

References:
https://developer.mozilla.org/en-US/docs/Web/API/PerformanceMark
"""

detail: None = Field(alias="detail")
name: str = Field(alias="name")
entry_type: str = Field(alias="entryType")
start_time: float = Field(alias="startTime")
duration: float = Field(alias="duration")


class NavigationTiming(BaseModel):
"""The PerformanceNavigationTiming Representation.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ def test_add_to_cart_xpath(self, sauce: Pylenium):
button.click()
sauce.getx("//a[@class='shopping_cart_link']").click()
assert sauce.findx("//*[@class='cart_item']").should().have_length(6)

def test_add_to_cart_xpath_duration(self, sauce: Pylenium):
"""Add 6 different items to the cart. There should be 6 items in the cart. Duration should be less than 2000ms"""
sauce.performance.mark("Adding item in cart")
for button in sauce.findx("//*[contains(@id, 'add-to-cart')]"):
button.click()
sauce.getx("//a[@class='shopping_cart_link']").click()
time_in_ms = sauce.performance.measure("Adding item in cart")
assert sauce.findx("//*[@class='cart_item']").should().have_length(6)
assert time_in_ms < 2000, "should be less 2000ms"
Loading