From 802fe0d563dd815a7217fba1e5a804adfe06c36a Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Sat, 10 Apr 2021 13:25:56 -0400 Subject: [PATCH] Update example tests --- examples/boilerplates/samples/sb_swag_test.py | 4 +- .../boilerplates/samples/swag_labs_test.py | 4 +- .../boilerplates/samples/test_page_objects.py | 5 +- examples/decryption_test.py | 6 +- examples/image_test.py | 5 ++ examples/list_assert_test.py | 15 +++++ examples/parameterized_test.py | 18 ++++-- examples/swag_labs_suite.py | 57 ++++++------------- examples/test_detect_404s.py | 9 +++ examples/test_hack_search.py | 5 +- examples/test_login.py | 6 +- examples/test_swag_labs.py | 36 ++++++------ 12 files changed, 88 insertions(+), 82 deletions(-) create mode 100644 examples/list_assert_test.py create mode 100644 examples/test_detect_404s.py diff --git a/examples/boilerplates/samples/sb_swag_test.py b/examples/boilerplates/samples/sb_swag_test.py index d13c88b4..e790924d 100644 --- a/examples/boilerplates/samples/sb_swag_test.py +++ b/examples/boilerplates/samples/sb_swag_test.py @@ -4,7 +4,7 @@ class LoginPage(): def login_to_swag_labs(self, sb, username): - sb.open("https://www.saucedemo.com/v1") + sb.open("https://www.saucedemo.com") sb.type("#user-name", username) sb.type("#password", "secret_sauce") sb.click('input[type="submit"]') @@ -15,4 +15,4 @@ class MyTests(): def test_swag_labs_login(self, sb): LoginPage().login_to_swag_labs(sb, "standard_user") sb.assert_element("#inventory_container") - sb.assert_text("Products", "div.product_label") + sb.assert_element('div:contains("Sauce Labs Backpack")') diff --git a/examples/boilerplates/samples/swag_labs_test.py b/examples/boilerplates/samples/swag_labs_test.py index 61a2075b..a1a626ed 100644 --- a/examples/boilerplates/samples/swag_labs_test.py +++ b/examples/boilerplates/samples/swag_labs_test.py @@ -6,7 +6,7 @@ from seleniumbase import BaseCase class LoginPage(): def login_to_swag_labs(self, sb, username): - sb.open("https://www.saucedemo.com/v1") + sb.open("https://www.saucedemo.com") sb.type("#user-name", username) sb.type("#password", "secret_sauce") sb.click('input[type="submit"]') @@ -17,4 +17,4 @@ class MyTests(BaseCase): def test_swag_labs_login(self): LoginPage().login_to_swag_labs(self, "standard_user") self.assert_element("#inventory_container") - self.assert_text("Products", "div.product_label") + self.assert_element('div:contains("Sauce Labs Backpack")') diff --git a/examples/boilerplates/samples/test_page_objects.py b/examples/boilerplates/samples/test_page_objects.py index 763372d3..1c828af7 100644 --- a/examples/boilerplates/samples/test_page_objects.py +++ b/examples/boilerplates/samples/test_page_objects.py @@ -21,10 +21,7 @@ class SeleniumBaseGitHubPage(): link = '#readme article a[href*="seleniumbase.io"]' sb.wait_for_element_visible(link) sb.js_click(link) - sb.wait_for_ready_state_complete() - current_url = sb.get_current_url() - if "seleniumbase.io" not in current_url: - sb.switch_to_window(1) # GitHub probably opened a new window + sb.switch_to_newest_window() class SeleniumBaseIOPage(): diff --git a/examples/decryption_test.py b/examples/decryption_test.py index 6f59f2ea..40cf4663 100755 --- a/examples/decryption_test.py +++ b/examples/decryption_test.py @@ -1,6 +1,6 @@ """ This test demonstrates the use of encryption/decryption. -(Technically, obfuscation/unobfuscation.) +(Technically, obfuscation/unobfuscation of passwords.) """ from seleniumbase import BaseCase @@ -10,7 +10,7 @@ from seleniumbase import encryption class DecryptionTests(BaseCase): def test_decrypt_password(self): - self.open("https://www.saucedemo.com/v1") + self.open("https://www.saucedemo.com") self.type("#user-name", "standard_user") encrypted_password = "$^*ENCRYPT=S3BDTAdCWzMmKEY8Gjg=?&#$" @@ -20,5 +20,5 @@ class DecryptionTests(BaseCase): self.type("#password", password) self.click('input[type="submit"]') - self.assert_text("Products", "div.product_label") self.assert_element("#inventory_container") + self.assert_element('div:contains("Sauce Labs Backpack")') diff --git a/examples/image_test.py b/examples/image_test.py index c83d31d0..36408880 100755 --- a/examples/image_test.py +++ b/examples/image_test.py @@ -1,8 +1,10 @@ +import pytest from seleniumbase import BaseCase class ImageTests(BaseCase): + @pytest.mark.run(order=1) def test_pull_image_from_website(self): """ Pull an image from a website and save it as a PNG file. """ self.open("https://xkcd.com/1117/") @@ -12,6 +14,7 @@ class ImageTests(BaseCase): self.save_element_as_image_file(selector, file_name, folder) print('"%s/%s" has been saved!' % (folder, file_name)) + @pytest.mark.run(order=2) def test_add_text_overlay_to_image(self): """ Add a text overlay to an image. """ self.open("https://xkcd.com/1117/") @@ -23,6 +26,7 @@ class ImageTests(BaseCase): selector, file_name, folder, overlay_text) print('"%s/%s" has been saved!' % (folder, file_name)) + @pytest.mark.run(order=3) def test_add_text_overlay_to_page_section(self): """ Add a text overlay to a section of a page. """ self.open("https://xkcd.com/2200/") @@ -38,6 +42,7 @@ class ImageTests(BaseCase): selector, file_name, folder, overlay_text) print('"%s/%s" has been saved!' % (folder, file_name)) + @pytest.mark.run(order=4) def test_add_text_overlay_to_full_page(self): """ Add a text overlay to a full page. """ self.open("https://xkcd.com/1922/") diff --git a/examples/list_assert_test.py b/examples/list_assert_test.py new file mode 100644 index 00000000..5b2733b1 --- /dev/null +++ b/examples/list_assert_test.py @@ -0,0 +1,15 @@ +""" * Asserting that multiple elements are present or visible: +HTML Presence: assert_elements_present() +HTML Visibility: assert_elements() <> assert_elements_visible() +""" +from seleniumbase import BaseCase + + +class MyTestClass(BaseCase): + + def test_assert_list_of_elements(self): + self.open("https://store.xkcd.com/collections/posters") + self.assert_elements_present("head", "style", "script") + self.assert_elements("h1", "h2", "h3") + my_list = ["#top-menu", "#col-main", "#col-widgets"] + self.assert_elements(my_list) diff --git a/examples/parameterized_test.py b/examples/parameterized_test.py index 396c0ddb..cc142ae7 100644 --- a/examples/parameterized_test.py +++ b/examples/parameterized_test.py @@ -5,12 +5,20 @@ from parameterized import parameterized class GoogleTests(BaseCase): @parameterized.expand([ - ["pypi", "pypi.org"], - ["wikipedia", "wikipedia.org"], - ["seleniumbase", "seleniumbase/SeleniumBase"], + ["PyPI", "pypi.org", 'img[alt="PyPI"]'], + ["Wikipedia", "wikipedia.org", "div#p-logo"], + ["SeleniumBase", "seleniumbase/SeleniumBase", 'img[title*="Selenium"]'] ]) - def test_parameterized_google_search(self, search_term, expected_text): + def test_parameterized_google_search(self, search_key, expected_text, img): self.open('https://google.com/ncr') - self.type('input[title="Search"]', search_term + '\n') + self.type('input[title="Search"]', search_key + '\n') self.assert_element('#result-stats') self.assert_text(expected_text, '#search') + self.click('a:contains("%s")' % expected_text) + self.assert_element(img) + self.click(img) + if "Selenium" in img: + self.click('img[alt="SeleniumBase.io Docs"]') + self.assert_element('[title="SeleniumBase Docs"]') + self.click('a:contains("Features List")') + self.assert_text("Features List", "h1") diff --git a/examples/swag_labs_suite.py b/examples/swag_labs_suite.py index c0716b4f..32c5eb5b 100755 --- a/examples/swag_labs_suite.py +++ b/examples/swag_labs_suite.py @@ -1,15 +1,12 @@ -import pytest from parameterized import parameterized from seleniumbase import BaseCase class SwagLabsTests(BaseCase): - def login_to_swag_labs(self, username="standard_user", v1=False): + def login_to_swag_labs(self, username="standard_user"): """ Login to Swag Labs and verify success. """ url = "https://www.saucedemo.com" - if v1: - url += "/v1" self.open(url) if username not in self.get_text("#login_credentials"): self.fail("Invalid user for login: %s" % username) @@ -17,18 +14,16 @@ class SwagLabsTests(BaseCase): self.type("#password", "secret_sauce") self.click('input[type="submit"]') self.assert_element("#inventory_container") - if v1: - self.assert_text("Products", "div.product_label") + self.assert_element('div:contains("Sauce Labs Backpack")') @parameterized.expand([ ["standard_user"], ["problem_user"], ]) - @pytest.mark.run(order=1) def test_swag_labs_basic_flow(self, username): """ This test checks functional flow of the Swag Labs store. This test is parameterized on the login user. """ - self.login_to_swag_labs(username=username, v1=True) + self.login_to_swag_labs(username=username) if username == "problem_user": print("\n(This test should fail)") @@ -49,60 +44,42 @@ class SwagLabsTests(BaseCase): self.assert_exact_text("1", "span.shopping_cart_badge") # Verify your cart - self.click("#shopping_cart_container path") - self.assert_exact_text("Your Cart", "div.subheader") + self.click("#shopping_cart_container") + self.assert_element('span:contains("Your Cart")') self.assert_text(item_name, "div.inventory_item_name") self.assert_exact_text("1", "div.cart_quantity") self.assert_exact_text("REMOVE", "button.cart_button") - continue_shopping_button = "link=CONTINUE SHOPPING" - if self.browser == "safari": - # Safari sees this element differently - continue_shopping_button = "link=Continue Shopping" - self.assert_element(continue_shopping_button) + self.assert_element("button#continue-shopping") # Checkout - Add info - self.click("link=CHECKOUT") - self.assert_text("Checkout: Your Information", "div.subheader") - self.assert_element("a.cart_cancel_link") + self.click("button#checkout") + self.assert_element('span:contains("Checkout: Your Information")') + self.assert_element("button#cancel") self.type("#first-name", "SeleniumBase") self.type("#last-name", "Rocks") self.type("#postal-code", "01720") # Checkout - Overview - self.click("input.btn_primary") - self.assert_text("Checkout: Overview", "div.subheader") - self.assert_element("link=CANCEL") + self.click("input#continue") + self.assert_element('span:contains("Checkout: Overview")') + self.assert_element("button#cancel") self.assert_text(item_name, "div.inventory_item_name") self.assert_text(item_price, "div.inventory_item_price") - self.assert_exact_text("1", "div.summary_quantity") + self.assert_exact_text("1", "div.cart_quantity") - # Finish Checkout and verify the item was removed from the cart - self.click("link=FINISH") + # Finish Checkout and verify that the cart is now empty + self.click("button#finish") self.assert_exact_text("THANK YOU FOR YOUR ORDER", "h2") self.assert_element("img.pony_express") - self.click("#shopping_cart_container path") + self.click("#shopping_cart_container") self.assert_element_absent("div.inventory_item_name") - self.click(continue_shopping_button) + self.click("button#continue-shopping") self.assert_element_absent("span.shopping_cart_badge") @parameterized.expand([ ["standard_user"], ["problem_user"], ]) - @pytest.mark.run(order=2) - def test_swag_labs_products_page_links(self, username): - """ This test checks for 404s on the Swag Labs products page. - This test is parameterized on the login user. """ - self.login_to_swag_labs(username=username, v1=True) - if username == "problem_user": - print("\n(This test should fail)") - self.assert_no_404_errors() - - @parameterized.expand([ - ["standard_user"], - ["problem_user"], - ]) - @pytest.mark.run(order=3) def test_swag_labs_visual_regressions(self, username): """ This test checks for visual regressions on the Swag Labs page. This test is parameterized on the login user. """ diff --git a/examples/test_detect_404s.py b/examples/test_detect_404s.py new file mode 100644 index 00000000..62c22161 --- /dev/null +++ b/examples/test_detect_404s.py @@ -0,0 +1,9 @@ +from seleniumbase import BaseCase + + +class BrokenLinkTests(BaseCase): + + def test_link_checking(self): + self.open("https://seleniumbase.io/other/broken_page.html") + print("\n(This test should fail)") + self.assert_no_404_errors() diff --git a/examples/test_hack_search.py b/examples/test_hack_search.py index e5470ac8..0a06f352 100755 --- a/examples/test_hack_search.py +++ b/examples/test_hack_search.py @@ -13,13 +13,12 @@ class HackingTests(BaseCase): self.assert_element('input[title="Search"]') self.set_attribute('[action="/search"]', "action", "//bing.com/search") self.set_attributes('[value="Google Search"]', "value", "Bing Search") - self.type('input[title="Search"]', "SeleniumBase GitHub.com") + self.type('input[title="Search"]', "SeleniumBase GitHub") self.highlight('[value="Bing Search"]') self.click('[value="Bing Search"]') self.highlight("h1.b_logo") self.highlight_click('a[href*="github.com/seleniumbase/SeleniumBase"]') - if self.browser == "firefox": - self.switch_to_window(1) # Firefox opens a new window + self.switch_to_newest_window() self.assert_element('[href="/seleniumbase/SeleniumBase"]') self.assert_true("seleniumbase/SeleniumBase" in self.get_current_url()) self.click('a[title="examples"]') diff --git a/examples/test_login.py b/examples/test_login.py index 1e844cc8..70b3a233 100755 --- a/examples/test_login.py +++ b/examples/test_login.py @@ -5,7 +5,7 @@ class SwagLabsLoginTests(BaseCase): def login_to_swag_labs(self): """ Login to Swag Labs and verify that login was successful. """ - self.open("https://www.saucedemo.com/v1") + self.open("https://www.saucedemo.com") self.type("#user-name", "standard_user") self.type("#password", "secret_sauce") self.click('input[type="submit"]') @@ -13,5 +13,5 @@ class SwagLabsLoginTests(BaseCase): def test_swag_labs_login(self): """ This test checks standard login for the Swag Labs store. """ self.login_to_swag_labs() - self.assert_element("div.header_label div.app_logo") - self.assert_text("Products", "div.product_label") + self.assert_element("#inventory_container") + self.assert_element('div:contains("Sauce Labs Backpack")') diff --git a/examples/test_swag_labs.py b/examples/test_swag_labs.py index 6547bf3c..65bc9a64 100755 --- a/examples/test_swag_labs.py +++ b/examples/test_swag_labs.py @@ -5,14 +5,14 @@ class SwagLabsTests(BaseCase): def login_to_swag_labs(self, username="standard_user"): """ Login to Swag Labs and verify success. """ - self.open("https://www.saucedemo.com/v1") + self.open("https://www.saucedemo.com") if username not in self.get_text("#login_credentials"): self.fail("Invalid user for login: %s" % username) self.type("#user-name", username) self.type("#password", "secret_sauce") self.click('input[type="submit"]') self.assert_element("#inventory_container") - self.assert_text("Products", "div.product_label") + self.assert_element('div:contains("Sauce Labs Backpack")') def test_swag_labs_basic_flow(self): """ This test checks functional flow of the Swag Labs store. """ @@ -35,38 +35,34 @@ class SwagLabsTests(BaseCase): self.assert_exact_text("1", "span.shopping_cart_badge") # Verify your cart - self.click("#shopping_cart_container path") - self.assert_exact_text("Your Cart", "div.subheader") + self.click("#shopping_cart_container") + self.assert_element('span:contains("Your Cart")') self.assert_text(item_name, "div.inventory_item_name") self.assert_exact_text("1", "div.cart_quantity") self.assert_exact_text("REMOVE", "button.cart_button") - continue_shopping_button = "link=CONTINUE SHOPPING" - if self.browser == "safari": - # Safari sees this element differently - continue_shopping_button = "link=Continue Shopping" - self.assert_element(continue_shopping_button) + self.assert_element("button#continue-shopping") # Checkout - Add info - self.click("link=CHECKOUT") - self.assert_text("Checkout: Your Information", "div.subheader") - self.assert_element("a.cart_cancel_link") + self.click("button#checkout") + self.assert_element('span:contains("Checkout: Your Information")') + self.assert_element("button#cancel") self.type("#first-name", "SeleniumBase") self.type("#last-name", "Rocks") self.type("#postal-code", "01720") # Checkout - Overview - self.click("input.btn_primary") - self.assert_text("Checkout: Overview", "div.subheader") - self.assert_element("link=CANCEL") + self.click("input#continue") + self.assert_element('span:contains("Checkout: Overview")') + self.assert_element("button#cancel") self.assert_text(item_name, "div.inventory_item_name") self.assert_text(item_price, "div.inventory_item_price") - self.assert_exact_text("1", "div.summary_quantity") + self.assert_exact_text("1", "div.cart_quantity") - # Finish Checkout and verify item is no longer in cart - self.click("link=FINISH") + # Finish Checkout and verify that the cart is now empty + self.click("button#finish") self.assert_exact_text("THANK YOU FOR YOUR ORDER", "h2") self.assert_element("img.pony_express") - self.click("#shopping_cart_container path") + self.click("#shopping_cart_container") self.assert_element_absent("div.inventory_item_name") - self.click(continue_shopping_button) + self.click("button#continue-shopping") self.assert_element_absent("span.shopping_cart_badge")