Skip to content

Commit 759423a

Browse files
committed
Update example tests
1 parent 1961a98 commit 759423a

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

examples/performance_test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Performance test example.
3+
4+
Uses decorators.print_runtime(), which prints the runtime duration
5+
of a method or "with"-block after the method (or block) completes.
6+
Also raises an exception when exceeding the time "limit" if set.
7+
8+
Arguments ->
9+
description # Optional - Shows description in print output.
10+
limit # Optional - Fail if the duration is above the limit.
11+
12+
Method / Function example usage ->
13+
from seleniumbase import decorators
14+
15+
@decorators.print_runtime("My Method")
16+
def my_method():
17+
# code ...
18+
# code ...
19+
20+
"with"-block example usage ->
21+
from seleniumbase import decorators
22+
23+
with decorators.print_runtime("My Code Block"):
24+
# code ...
25+
# code ...
26+
"""
27+
from seleniumbase import BaseCase
28+
from seleniumbase import decorators
29+
30+
31+
class PerformanceClass(BaseCase):
32+
@decorators.print_runtime("Open Swag Labs and Log In")
33+
def login_to_swag_labs(self):
34+
with decorators.print_runtime("Open Swag Labs"):
35+
self.open("https://www.saucedemo.com")
36+
self.type("#user-name", "standard_user")
37+
self.type("#password", "secret_sauce\n")
38+
39+
def test_performance_of_swag_labs(self):
40+
self.login_to_swag_labs()
41+
self.assert_element("div.inventory_list")
42+
self.assert_exact_text("PRODUCTS", "span.title")
43+
with decorators.print_runtime("Add backpack and see cart"):
44+
self.click('button[name*="backpack"]')
45+
self.click("#shopping_cart_container a")
46+
self.assert_text("Backpack", "div.cart_item")
47+
with decorators.print_runtime("Remove backpack from cart"):
48+
self.click('button:contains("Remove")') # HTML innerText
49+
self.assert_text_not_visible("Backpack", "div.cart_item")
50+
with decorators.print_runtime("Log out from Swag Labs", 3):
51+
self.js_click("a#logout_sidebar_link")
52+
self.assert_element("div#login_button_container")

examples/time_limit_test.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
""" This test fails on purpose to demonstrate the time-limit feature
2-
for tests that run longer than the time limit specified (seconds).
3-
The time-limit clock starts after the browser has fully launched,
4-
which is after pytest starts it's own internal clock for tests.
5-
Usage: (inside tests) => self.set_time_limit(SECONDS)
6-
Usage: (command-line) => --time-limit=SECONDS """
7-
81
import pytest
92
from seleniumbase import BaseCase
3+
from seleniumbase import decorators
104

115

126
class TimeLimitTests(BaseCase):
137
@pytest.mark.expected_failure
14-
def test_time_limit_feature(self):
15-
self.set_time_limit(5) # Fail test if time exceeds 5 seconds
16-
self.open("https://xkcd.com/1658/")
8+
def test_runtime_limit_decorator(self):
9+
"""This test fails on purpose to show the runtime_limit() decorator
10+
for code blocks that run longer than the time limit specified."""
11+
print("\n(This test should fail)")
12+
self.open("https://xkcd.com/1190")
13+
with decorators.runtime_limit(0.5):
14+
self.sleep(0.75)
15+
16+
@pytest.mark.expected_failure
17+
def test_set_time_limit_method(self):
18+
"""This test fails on purpose to show the set_time_limit() method
19+
for tests that run longer than the time limit specified (seconds).
20+
The time-limit clock starts after the browser has fully launched,
21+
which is after pytest starts it's own internal clock for tests.
22+
Usage: (inside tests) => self.set_time_limit(SECONDS)
23+
Usage: (command-line) => --time-limit=SECONDS"""
24+
self.set_time_limit(2.5) # Fail test if time exceeds 2.5 seconds
1725
print("\n(This test should fail)")
18-
self.sleep(7)
26+
self.open("https://xkcd.com/1658")
27+
self.sleep(3)

0 commit comments

Comments
 (0)