Update examples

This commit is contained in:
Michael Mintz 2020-06-14 01:17:02 -04:00
parent 9b639f0f37
commit 353418aaff
16 changed files with 96 additions and 64 deletions

View File

@ -8,9 +8,9 @@ from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_basic(self):
self.open("https://store.xkcd.com/search")
self.type('input[name="q"]', "xkcd book\n")
self.open("https://xkcd.com/353/")
self.click('a[rel="license"]')
self.go_back()
self.click("link=About")
self.open("://store.xkcd.com/collections/everything")
self.update_text("input.search-input", "xkcd book\n")
self.click_link_text("About")

View File

@ -10,7 +10,7 @@ class GoogleTests(BaseCase):
def test_google_dot_com(self):
self.open('https://google.com/ncr')
self.update_text(HomePage.search_box, 'github')
self.type(HomePage.search_box, 'github')
self.assert_element(HomePage.list_box)
self.assert_element(HomePage.search_button)
self.assert_element(HomePage.feeling_lucky_button)

View File

@ -18,7 +18,7 @@ class GitHubTests(BaseCase):
"""AppleWebKit/537.36 (KHTML, like Gecko) """
"""Chrome/75.0.3770.100 Safari/537.36""")
self.open("https://github.com/")
self.update_text("input.header-search-input", "SeleniumBase\n")
self.type("input.header-search-input", "SeleniumBase\n")
self.slow_click('a[href="/seleniumbase/SeleniumBase"]')
self.assert_element("div.repository-content")
self.assert_text("SeleniumBase", "h1")

View File

@ -16,8 +16,8 @@ class MasterQATests(MasterQA):
self.verify("Can you find the moon?")
self.click('a[rel="next"]')
self.verify("Do the drones look safe?")
self.open("https://store.xkcd.com/collections/everything")
self.update_text("input.search-input", "book\n")
self.open("https://store.xkcd.com/search")
self.type("input.search-input", "book\n")
self.verify("Do you see books in the search results?")
self.open("https://xkcd.com/213/")
for i in range(5):

View File

@ -4,17 +4,17 @@ from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_basic(self):
self.open("https://store.xkcd.com/search")
self.type('input[name="q"]', "xkcd book\n")
self.assert_text("xkcd: volume 0", "h3")
self.open("https://xkcd.com/353/")
self.assert_title("xkcd: Python")
self.assert_element('img[alt="Python"]')
self.click('a[rel="license"]')
self.assert_text("free to copy and reuse")
self.go_back()
self.click("link=About")
self.assert_text("xkcd.com", "h2")
self.open("://store.xkcd.com/collections/everything")
self.update_text("input.search-input", "xkcd book\n")
self.assert_exact_text("xkcd: volume 0", "h3")
self.click_link_text("About")
self.assert_exact_text("xkcd.com", "h2")
####
@ -23,14 +23,17 @@ class MyTestClass(BaseCase):
# **** NOTES / USEFUL INFO ****
#
# 1. By default, CSS Selectors are used to identify elements.
# Other options include: "LINK_TEXT", "PARTIAL_LINK_TEXT", "NAME",
# CSS Guide: "https://www.w3schools.com/cssref/css_selectors.asp".
# Other selectors include: "LINK_TEXT", "PARTIAL_LINK_TEXT", "NAME",
# "CLASS_NAME", and "ID", but most of those can be expressed as CSS.
#
# Here's an example of changing the "by":
# [
# from selenium.webdriver.common.by import By
# ...
# self.click('Next', by=By.PARTIAL_LINK_TEXT)
# ]
#
# XPath is used by default if the arg starts with "/", "./", or "(":
# [
# self.click('/html/body/div[3]/div[4]/p[2]/a')
@ -39,27 +42,46 @@ class MyTestClass(BaseCase):
# If you're completely new to CSS selectors, right-click on a
# web page and select "Inspect" to see the CSS in the html.
#
# 2. Most methods have the optional `timeout` argument. Ex:
# 2. Most methods have the optional "timeout" argument.
# Here's an example of changing the "timeout":
# [
# self.assert_element('img[alt="Python"]', timeout=15)
# ]
# The `timeout` argument tells the method how many seconds to wait
# for an element to appear before raising an exception. This is
# The "timeout" argument tells the method how many seconds to wait
# for an element to appear before failing the test. This is
# useful if a web page needs additional time to load an element.
# If you don't specify a `timeout`, a default timeout is used.
# If you don't specify a "timeout", a default timeout is used.
# Default timeouts are configured in seleniumbase/config/settings.py
#
# 3. SeleniumBase methods are very versatile. For example,
# self.update_text(SELECTOR, TEXT) does the following:
# * Waits for the element to be visible
# * Waits for the element to be interactive
# * Clears the text field
# * Types in the new text
# * Hits Enter/Submit (if the text ends in "\n")
# 3. SeleniumBase methods often perform multiple actions. For example,
# self.type(SELECTOR, TEXT) will do the following:
# * Wait for the element to be visible
# * Wait for the element to be interactive
# * Clear the text field
# * Type in the new text
# * Press Enter/Submit if the text ends in "\n"
#
# self.update_text(S, T) can also be written as self.type(S, T)
# 4. Duplicate method names may exist for the same method:
# (This makes it easier to switch over from other test frameworks.)
# Example:
# self.open() = self.visit() = self.open_url() = self.goto()
# self.type() = self.update_text() = self.input()
# self.send_keys() = self.add_text()
# self.get_element() = self.wait_for_element_present()
# self.find_element() = self.wait_for_element_visible()
# = self.wait_for_element()
# self.assert_element() = self.assert_element_visible()
# self.assert_text() = self.assert_text_visible()
# self.find_text() = self.wait_for_text_visible()
# = self.wait_for_text()
# self.click_link_text(text) = self.click(link=text)
# = self.click_link(text)
# * self.get(url) is SPECIAL: *
# If {url} is a valid URL, self.get() works just like self.open()
# Otherwise {url} becomes a selector for calling self.get_element()
#
# 4. There's usually more than one way to do the same thing. Ex:
# 5. There's usually more than one way to do the same thing.
# Example 1:
# [
# self.assert_text("xkcd: volume 0", "h3")
# ]
@ -68,33 +90,37 @@ class MyTestClass(BaseCase):
# text = self.get_text("h3")
# self.assert_true("xkcd: volume 0" in text)
# ]
# Or:
# Is also the same as:
# [
# text = self.find_element("h3").text
# element = self.find_element("h3")
# text = element.text
# self.assert_true("xkcd: volume 0" in text)
# ]
#
# And the following line:
# Example 2:
# [
# self.assert_exact_text("xkcd.com", "h2")
# ]
# Is the same as:
# [
# text = self.get_text("h2").strip()
# self.assert_true("xkcd.com".strip() == text)
# ]
#
# Example 3:
# [
# title = self.get_attribute("#comic img", "title")
# ]
# Can also be written as:
# Is the same as:
# [
# element = self.find_element("#comic img")
# title = element.get_attribute("title")
# ]
#
# 5. self.assert_exact_text(TEXT) ignores leading and trailing
# 6. self.assert_exact_text(TEXT) ignores leading and trailing
# whitespace in the TEXT assertion.
# So, self.assert_exact_text("Some Text") will find [" Some Text "].
#
# 6. For backwards-compatibilty, some SeleniumBase methods that do the
# same thing have multiple names, kept on from previous versions.
# Ex: self.wait_for_element() is the same as self.find_element().
# Both search for and return the element, and raise an exception if
# the element does not appear on the page within the timeout limit.
# And self.assert_element() does this too (without returning it).
#
# 7. If a URL starts with "://", then "https://" is automatically used.
# Example: [self.open("://URL")] becomes [self.open("https://URL")]
# This helps by reducing the line length by 5 characters.

View File

@ -11,6 +11,6 @@ class GoogleTestClass(BaseCase):
])
def test_parameterized_google_search(self, search_term, expected_text):
self.open('https://google.com/ncr')
self.update_text('input[title="Search"]', search_term + '\n')
self.type('input[title="Search"]', search_term + '\n')
self.assert_element('#result-stats')
self.assert_text(expected_text, '#search')

View File

@ -10,8 +10,8 @@ class SwagLabsTests(BaseCase):
self.open("https://www.saucedemo.com/")
if username not in self.get_text("#login_credentials"):
self.fail("Invalid user for login: %s" % username)
self.update_text("#user-name", username)
self.update_text("#password", "secret_sauce")
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")
@ -58,9 +58,9 @@ class SwagLabsTests(BaseCase):
self.click("link=CHECKOUT")
self.assert_exact_text("Checkout: Your Information", "div.subheader")
self.assert_element("a.cart_cancel_link")
self.update_text("#first-name", "SeleniumBase")
self.update_text("#last-name", "Rocks")
self.update_text("#postal-code", "01720")
self.type("#first-name", "SeleniumBase")
self.type("#last-name", "Rocks")
self.type("#postal-code", "01720")
# Checkout - Overview
self.click("input.btn_primary")

View File

@ -10,7 +10,7 @@ class AppleTestClass(BaseCase):
self.message_duration = 2.0
self.open("https://developer.apple.com/search/")
title = "Testing with WebDriver in Safari"
self.update_text('[placeholder*="developer.apple.com"]', title + "\n")
self.type('[placeholder*="developer.apple.com"]', title + "\n")
self.click("link=%s" % title)
self.assert_element('[href="/documentation"]')
self.assert_text(title, "h1")

View File

@ -93,3 +93,9 @@ class MyTestClass(BaseCase):
# Assert exact text
self.assert_exact_text("Demo Page", "h1")
# Assert no broken links
self.assert_no_404_errors()
# Assert no JavaScript errors
self.assert_no_js_errors()

View File

@ -33,6 +33,6 @@ class EventFiringTestClass(BaseCase):
print("\n* EventFiringWebDriver example *")
self.open("https://xkcd.com/1862/")
self.click("link=About")
self.open("https://store.xkcd.com/collections/everything")
self.update_text("input.search-input", "xkcd book\n")
self.open("https://store.xkcd.com/search")
self.type('input[name="q"]', "xkcd book\n")
self.open("https://xkcd.com/1822/")

View File

@ -13,7 +13,7 @@ class HackingTest(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.update_text('input[title="Search"]', "SeleniumBase GitHub")
self.type('input[title="Search"]', "SeleniumBase GitHub")
self.click('[value="Bing Search"]')
self.assert_element("h1.b_logo")
self.click('[href*="github.com/seleniumbase/SeleniumBase"]')

View File

@ -4,7 +4,7 @@ import pytest
@pytest.mark.parametrize('value', ["pytest", "selenium"])
def test_sb_fixture_with_no_class(sb, value):
sb.open("https://google.com/ncr")
sb.update_text('input[title="Search"]', value + '\n')
sb.type('input[title="Search"]', value + '\n')
sb.assert_text(value, "div#center_col")
@ -12,5 +12,5 @@ class Test_SB_Fixture():
@pytest.mark.parametrize('value', ["pytest", "selenium"])
def test_sb_fixture_inside_class(self, sb, value):
sb.open("https://google.com/ncr")
sb.update_text('input[title="Search"]', value + '\n')
sb.type('input[title="Search"]', value + '\n')
sb.assert_text(value, "div#center_col")

View File

@ -3,7 +3,7 @@
# "sb" pytest fixture test in a method with no class
def test_sb_fixture_with_no_class(sb):
sb.open("https://google.com/ncr")
sb.update_text('input[title="Search"]', 'SeleniumBase\n')
sb.type('input[title="Search"]', 'SeleniumBase\n')
sb.click('a[href*="github.com/seleniumbase/SeleniumBase"]')
sb.click('a[title="seleniumbase"]')
@ -12,6 +12,6 @@ def test_sb_fixture_with_no_class(sb):
class Test_SB_Fixture():
def test_sb_fixture_inside_class(self, sb):
sb.open("https://google.com/ncr")
sb.update_text('input[title="Search"]', 'SeleniumBase\n')
sb.type('input[title="Search"]', 'SeleniumBase\n')
sb.click('a[href*="github.com/seleniumbase/SeleniumBase"]')
sb.click('a[title="examples"]')

View File

@ -8,8 +8,8 @@ class SwagLabsTests(BaseCase):
self.open("https://www.saucedemo.com/")
if username not in self.get_text("#login_credentials"):
self.fail("Invalid user for login: %s" % username)
self.update_text("#user-name", username)
self.update_text("#password", "secret_sauce")
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")
@ -50,9 +50,9 @@ class SwagLabsTests(BaseCase):
self.click("link=CHECKOUT")
self.assert_exact_text("Checkout: Your Information", "div.subheader")
self.assert_element("a.cart_cancel_link")
self.update_text("#first-name", "SeleniumBase")
self.update_text("#last-name", "Rocks")
self.update_text("#postal-code", "01720")
self.type("#first-name", "SeleniumBase")
self.type("#last-name", "Rocks")
self.type("#postal-code", "01720")
# Checkout - Overview
self.click("input.btn_primary")

View File

@ -4,7 +4,7 @@ from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_example_1(self):
url = "https://store.xkcd.com/collections/everything"
url = "https://store.xkcd.com/collections/posters"
self.open(url)
self.type("input.search-input", "xkcd book\n")
self.assert_text("xkcd: volume 0", "h3")
@ -17,5 +17,5 @@ class MyTestClass(BaseCase):
self.click('a[rel="license"]')
self.assert_text("back to this page")
self.go_back()
self.click("link=About")
self.click_link_text("About")
self.assert_exact_text("xkcd.com", "h2")

View File

@ -4,14 +4,14 @@ from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_basic(self):
self.open("https://store.xkcd.com/search")
self.type('input[name="q"]', "xkcd book\n")
self.assert_text("xkcd: volume 0", "h3")
self.open("https://xkcd.com/353/")
self.assert_title("xkcd: Python")
self.assert_element('img[alt="Python"]')
self.click('a[rel="license"]')
self.assert_text("free to copy and reuse")
self.go_back()
self.click("link=About")
self.assert_text("xkcd.com", "h2")
self.open("://store.xkcd.com/collections/everything")
self.update_text("input.search-input", "xkcd book\n")
self.assert_exact_text("xkcd: volume 0", "h3")
self.click_link_text("About")
self.assert_exact_text("xkcd.com", "h2")