Skip to content

Commit 802fe0d

Browse files
committed
Update example tests
1 parent a401b37 commit 802fe0d

12 files changed

+88
-82
lines changed

examples/boilerplates/samples/sb_swag_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class LoginPage():
55

66
def login_to_swag_labs(self, sb, username):
7-
sb.open("https://www.saucedemo.com/v1")
7+
sb.open("https://www.saucedemo.com")
88
sb.type("#user-name", username)
99
sb.type("#password", "secret_sauce")
1010
sb.click('input[type="submit"]')
@@ -15,4 +15,4 @@ class MyTests():
1515
def test_swag_labs_login(self, sb):
1616
LoginPage().login_to_swag_labs(sb, "standard_user")
1717
sb.assert_element("#inventory_container")
18-
sb.assert_text("Products", "div.product_label")
18+
sb.assert_element('div:contains("Sauce Labs Backpack")')

examples/boilerplates/samples/swag_labs_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class LoginPage():
77

88
def login_to_swag_labs(self, sb, username):
9-
sb.open("https://www.saucedemo.com/v1")
9+
sb.open("https://www.saucedemo.com")
1010
sb.type("#user-name", username)
1111
sb.type("#password", "secret_sauce")
1212
sb.click('input[type="submit"]')
@@ -17,4 +17,4 @@ class MyTests(BaseCase):
1717
def test_swag_labs_login(self):
1818
LoginPage().login_to_swag_labs(self, "standard_user")
1919
self.assert_element("#inventory_container")
20-
self.assert_text("Products", "div.product_label")
20+
self.assert_element('div:contains("Sauce Labs Backpack")')

examples/boilerplates/samples/test_page_objects.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ def click_seleniumbase_io_link(self, sb):
2121
link = '#readme article a[href*="seleniumbase.io"]'
2222
sb.wait_for_element_visible(link)
2323
sb.js_click(link)
24-
sb.wait_for_ready_state_complete()
25-
current_url = sb.get_current_url()
26-
if "seleniumbase.io" not in current_url:
27-
sb.switch_to_window(1) # GitHub probably opened a new window
24+
sb.switch_to_newest_window()
2825

2926

3027
class SeleniumBaseIOPage():

examples/decryption_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
This test demonstrates the use of encryption/decryption.
3-
(Technically, obfuscation/unobfuscation.)
3+
(Technically, obfuscation/unobfuscation of passwords.)
44
"""
55

66
from seleniumbase import BaseCase
@@ -10,7 +10,7 @@
1010
class DecryptionTests(BaseCase):
1111

1212
def test_decrypt_password(self):
13-
self.open("https://www.saucedemo.com/v1")
13+
self.open("https://www.saucedemo.com")
1414
self.type("#user-name", "standard_user")
1515

1616
encrypted_password = "$^*ENCRYPT=S3BDTAdCWzMmKEY8Gjg=?&#$"
@@ -20,5 +20,5 @@ def test_decrypt_password(self):
2020
self.type("#password", password)
2121

2222
self.click('input[type="submit"]')
23-
self.assert_text("Products", "div.product_label")
2423
self.assert_element("#inventory_container")
24+
self.assert_element('div:contains("Sauce Labs Backpack")')

examples/image_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import pytest
12
from seleniumbase import BaseCase
23

34

45
class ImageTests(BaseCase):
56

7+
@pytest.mark.run(order=1)
68
def test_pull_image_from_website(self):
79
""" Pull an image from a website and save it as a PNG file. """
810
self.open("https://xkcd.com/1117/")
@@ -12,6 +14,7 @@ def test_pull_image_from_website(self):
1214
self.save_element_as_image_file(selector, file_name, folder)
1315
print('"%s/%s" has been saved!' % (folder, file_name))
1416

17+
@pytest.mark.run(order=2)
1518
def test_add_text_overlay_to_image(self):
1619
""" Add a text overlay to an image. """
1720
self.open("https://xkcd.com/1117/")
@@ -23,6 +26,7 @@ def test_add_text_overlay_to_image(self):
2326
selector, file_name, folder, overlay_text)
2427
print('"%s/%s" has been saved!' % (folder, file_name))
2528

29+
@pytest.mark.run(order=3)
2630
def test_add_text_overlay_to_page_section(self):
2731
""" Add a text overlay to a section of a page. """
2832
self.open("https://xkcd.com/2200/")
@@ -38,6 +42,7 @@ def test_add_text_overlay_to_page_section(self):
3842
selector, file_name, folder, overlay_text)
3943
print('"%s/%s" has been saved!' % (folder, file_name))
4044

45+
@pytest.mark.run(order=4)
4146
def test_add_text_overlay_to_full_page(self):
4247
""" Add a text overlay to a full page. """
4348
self.open("https://xkcd.com/1922/")

examples/list_assert_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
""" * Asserting that multiple elements are present or visible:
2+
HTML Presence: assert_elements_present()
3+
HTML Visibility: assert_elements() <> assert_elements_visible()
4+
"""
5+
from seleniumbase import BaseCase
6+
7+
8+
class MyTestClass(BaseCase):
9+
10+
def test_assert_list_of_elements(self):
11+
self.open("https://store.xkcd.com/collections/posters")
12+
self.assert_elements_present("head", "style", "script")
13+
self.assert_elements("h1", "h2", "h3")
14+
my_list = ["#top-menu", "#col-main", "#col-widgets"]
15+
self.assert_elements(my_list)

examples/parameterized_test.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
class GoogleTests(BaseCase):
66

77
@parameterized.expand([
8-
["pypi", "pypi.org"],
9-
["wikipedia", "wikipedia.org"],
10-
["seleniumbase", "seleniumbase/SeleniumBase"],
8+
["PyPI", "pypi.org", 'img[alt="PyPI"]'],
9+
["Wikipedia", "wikipedia.org", "div#p-logo"],
10+
["SeleniumBase", "seleniumbase/SeleniumBase", 'img[title*="Selenium"]']
1111
])
12-
def test_parameterized_google_search(self, search_term, expected_text):
12+
def test_parameterized_google_search(self, search_key, expected_text, img):
1313
self.open('https://google.com/ncr')
14-
self.type('input[title="Search"]', search_term + '\n')
14+
self.type('input[title="Search"]', search_key + '\n')
1515
self.assert_element('#result-stats')
1616
self.assert_text(expected_text, '#search')
17+
self.click('a:contains("%s")' % expected_text)
18+
self.assert_element(img)
19+
self.click(img)
20+
if "Selenium" in img:
21+
self.click('img[alt="SeleniumBase.io Docs"]')
22+
self.assert_element('[title="SeleniumBase Docs"]')
23+
self.click('a:contains("Features List")')
24+
self.assert_text("Features List", "h1")

examples/swag_labs_suite.py

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
1-
import pytest
21
from parameterized import parameterized
32
from seleniumbase import BaseCase
43

54

65
class SwagLabsTests(BaseCase):
76

8-
def login_to_swag_labs(self, username="standard_user", v1=False):
7+
def login_to_swag_labs(self, username="standard_user"):
98
""" Login to Swag Labs and verify success. """
109
url = "https://www.saucedemo.com"
11-
if v1:
12-
url += "/v1"
1310
self.open(url)
1411
if username not in self.get_text("#login_credentials"):
1512
self.fail("Invalid user for login: %s" % username)
1613
self.type("#user-name", username)
1714
self.type("#password", "secret_sauce")
1815
self.click('input[type="submit"]')
1916
self.assert_element("#inventory_container")
20-
if v1:
21-
self.assert_text("Products", "div.product_label")
17+
self.assert_element('div:contains("Sauce Labs Backpack")')
2218

2319
@parameterized.expand([
2420
["standard_user"],
2521
["problem_user"],
2622
])
27-
@pytest.mark.run(order=1)
2823
def test_swag_labs_basic_flow(self, username):
2924
""" This test checks functional flow of the Swag Labs store.
3025
This test is parameterized on the login user. """
31-
self.login_to_swag_labs(username=username, v1=True)
26+
self.login_to_swag_labs(username=username)
3227
if username == "problem_user":
3328
print("\n(This test should fail)")
3429

@@ -49,60 +44,42 @@ def test_swag_labs_basic_flow(self, username):
4944
self.assert_exact_text("1", "span.shopping_cart_badge")
5045

5146
# Verify your cart
52-
self.click("#shopping_cart_container path")
53-
self.assert_exact_text("Your Cart", "div.subheader")
47+
self.click("#shopping_cart_container")
48+
self.assert_element('span:contains("Your Cart")')
5449
self.assert_text(item_name, "div.inventory_item_name")
5550
self.assert_exact_text("1", "div.cart_quantity")
5651
self.assert_exact_text("REMOVE", "button.cart_button")
57-
continue_shopping_button = "link=CONTINUE SHOPPING"
58-
if self.browser == "safari":
59-
# Safari sees this element differently
60-
continue_shopping_button = "link=Continue Shopping"
61-
self.assert_element(continue_shopping_button)
52+
self.assert_element("button#continue-shopping")
6253

6354
# Checkout - Add info
64-
self.click("link=CHECKOUT")
65-
self.assert_text("Checkout: Your Information", "div.subheader")
66-
self.assert_element("a.cart_cancel_link")
55+
self.click("button#checkout")
56+
self.assert_element('span:contains("Checkout: Your Information")')
57+
self.assert_element("button#cancel")
6758
self.type("#first-name", "SeleniumBase")
6859
self.type("#last-name", "Rocks")
6960
self.type("#postal-code", "01720")
7061

7162
# Checkout - Overview
72-
self.click("input.btn_primary")
73-
self.assert_text("Checkout: Overview", "div.subheader")
74-
self.assert_element("link=CANCEL")
63+
self.click("input#continue")
64+
self.assert_element('span:contains("Checkout: Overview")')
65+
self.assert_element("button#cancel")
7566
self.assert_text(item_name, "div.inventory_item_name")
7667
self.assert_text(item_price, "div.inventory_item_price")
77-
self.assert_exact_text("1", "div.summary_quantity")
68+
self.assert_exact_text("1", "div.cart_quantity")
7869

79-
# Finish Checkout and verify the item was removed from the cart
80-
self.click("link=FINISH")
70+
# Finish Checkout and verify that the cart is now empty
71+
self.click("button#finish")
8172
self.assert_exact_text("THANK YOU FOR YOUR ORDER", "h2")
8273
self.assert_element("img.pony_express")
83-
self.click("#shopping_cart_container path")
74+
self.click("#shopping_cart_container")
8475
self.assert_element_absent("div.inventory_item_name")
85-
self.click(continue_shopping_button)
76+
self.click("button#continue-shopping")
8677
self.assert_element_absent("span.shopping_cart_badge")
8778

8879
@parameterized.expand([
8980
["standard_user"],
9081
["problem_user"],
9182
])
92-
@pytest.mark.run(order=2)
93-
def test_swag_labs_products_page_links(self, username):
94-
""" This test checks for 404s on the Swag Labs products page.
95-
This test is parameterized on the login user. """
96-
self.login_to_swag_labs(username=username, v1=True)
97-
if username == "problem_user":
98-
print("\n(This test should fail)")
99-
self.assert_no_404_errors()
100-
101-
@parameterized.expand([
102-
["standard_user"],
103-
["problem_user"],
104-
])
105-
@pytest.mark.run(order=3)
10683
def test_swag_labs_visual_regressions(self, username):
10784
""" This test checks for visual regressions on the Swag Labs page.
10885
This test is parameterized on the login user. """

examples/test_detect_404s.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from seleniumbase import BaseCase
2+
3+
4+
class BrokenLinkTests(BaseCase):
5+
6+
def test_link_checking(self):
7+
self.open("https://seleniumbase.io/other/broken_page.html")
8+
print("\n(This test should fail)")
9+
self.assert_no_404_errors()

examples/test_hack_search.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ def test_hack_search(self):
1313
self.assert_element('input[title="Search"]')
1414
self.set_attribute('[action="/search"]', "action", "//bing.com/search")
1515
self.set_attributes('[value="Google Search"]', "value", "Bing Search")
16-
self.type('input[title="Search"]', "SeleniumBase GitHub.com")
16+
self.type('input[title="Search"]', "SeleniumBase GitHub")
1717
self.highlight('[value="Bing Search"]')
1818
self.click('[value="Bing Search"]')
1919
self.highlight("h1.b_logo")
2020
self.highlight_click('a[href*="github.com/seleniumbase/SeleniumBase"]')
21-
if self.browser == "firefox":
22-
self.switch_to_window(1) # Firefox opens a new window
21+
self.switch_to_newest_window()
2322
self.assert_element('[href="/seleniumbase/SeleniumBase"]')
2423
self.assert_true("seleniumbase/SeleniumBase" in self.get_current_url())
2524
self.click('a[title="examples"]')

0 commit comments

Comments
 (0)