diff --git a/pylenium/performance.py b/pylenium/performance.py index 95fda65..502ade3 100644 --- a/pylenium/performance.py +++ b/pylenium/performance.py @@ -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. diff --git a/tests/test_flows.py b/tests/test_flows.py index a7a3462..3d407f6 100644 --- a/tests/test_flows.py +++ b/tests/test_flows.py @@ -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"