Skip to content

Commit 353418a

Browse files
committed
Update examples
1 parent 9b639f0 commit 353418a

16 files changed

+96
-64
lines changed

examples/basic_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
class MyTestClass(BaseCase):
99

1010
def test_basic(self):
11+
self.open("https://store.xkcd.com/search")
12+
self.type('input[name="q"]', "xkcd book\n")
1113
self.open("https://xkcd.com/353/")
1214
self.click('a[rel="license"]')
1315
self.go_back()
14-
self.click("link=About")
15-
self.open("://store.xkcd.com/collections/everything")
16-
self.update_text("input.search-input", "xkcd book\n")
16+
self.click_link_text("About")

examples/boilerplates/samples/google_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class GoogleTests(BaseCase):
1010

1111
def test_google_dot_com(self):
1212
self.open('https://google.com/ncr')
13-
self.update_text(HomePage.search_box, 'github')
13+
self.type(HomePage.search_box, 'github')
1414
self.assert_element(HomePage.list_box)
1515
self.assert_element(HomePage.search_button)
1616
self.assert_element(HomePage.feeling_lucky_button)

examples/github_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_github(self):
1818
"""AppleWebKit/537.36 (KHTML, like Gecko) """
1919
"""Chrome/75.0.3770.100 Safari/537.36""")
2020
self.open("https://github.com/")
21-
self.update_text("input.header-search-input", "SeleniumBase\n")
21+
self.type("input.header-search-input", "SeleniumBase\n")
2222
self.slow_click('a[href="/seleniumbase/SeleniumBase"]')
2323
self.assert_element("div.repository-content")
2424
self.assert_text("SeleniumBase", "h1")

examples/master_qa/masterqa_test_1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def test_xkcd(self):
1616
self.verify("Can you find the moon?")
1717
self.click('a[rel="next"]')
1818
self.verify("Do the drones look safe?")
19-
self.open("https://store.xkcd.com/collections/everything")
20-
self.update_text("input.search-input", "book\n")
19+
self.open("https://store.xkcd.com/search")
20+
self.type("input.search-input", "book\n")
2121
self.verify("Do you see books in the search results?")
2222
self.open("https://xkcd.com/213/")
2323
for i in range(5):

examples/my_first_test.py

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
class MyTestClass(BaseCase):
55

66
def test_basic(self):
7+
self.open("https://store.xkcd.com/search")
8+
self.type('input[name="q"]', "xkcd book\n")
9+
self.assert_text("xkcd: volume 0", "h3")
710
self.open("https://xkcd.com/353/")
811
self.assert_title("xkcd: Python")
912
self.assert_element('img[alt="Python"]')
1013
self.click('a[rel="license"]')
1114
self.assert_text("free to copy and reuse")
1215
self.go_back()
13-
self.click("link=About")
14-
self.assert_text("xkcd.com", "h2")
15-
self.open("://store.xkcd.com/collections/everything")
16-
self.update_text("input.search-input", "xkcd book\n")
17-
self.assert_exact_text("xkcd: volume 0", "h3")
16+
self.click_link_text("About")
17+
self.assert_exact_text("xkcd.com", "h2")
1818

1919
####
2020

@@ -23,14 +23,17 @@ def test_basic(self):
2323
# **** NOTES / USEFUL INFO ****
2424
#
2525
# 1. By default, CSS Selectors are used to identify elements.
26-
# Other options include: "LINK_TEXT", "PARTIAL_LINK_TEXT", "NAME",
26+
# CSS Guide: "https://www.w3schools.com/cssref/css_selectors.asp".
27+
# Other selectors include: "LINK_TEXT", "PARTIAL_LINK_TEXT", "NAME",
2728
# "CLASS_NAME", and "ID", but most of those can be expressed as CSS.
29+
#
2830
# Here's an example of changing the "by":
2931
# [
3032
# from selenium.webdriver.common.by import By
3133
# ...
3234
# self.click('Next', by=By.PARTIAL_LINK_TEXT)
3335
# ]
36+
#
3437
# XPath is used by default if the arg starts with "/", "./", or "(":
3538
# [
3639
# self.click('/html/body/div[3]/div[4]/p[2]/a')
@@ -39,27 +42,46 @@ def test_basic(self):
3942
# If you're completely new to CSS selectors, right-click on a
4043
# web page and select "Inspect" to see the CSS in the html.
4144
#
42-
# 2. Most methods have the optional `timeout` argument. Ex:
45+
# 2. Most methods have the optional "timeout" argument.
46+
# Here's an example of changing the "timeout":
4347
# [
4448
# self.assert_element('img[alt="Python"]', timeout=15)
4549
# ]
46-
# The `timeout` argument tells the method how many seconds to wait
47-
# for an element to appear before raising an exception. This is
50+
# The "timeout" argument tells the method how many seconds to wait
51+
# for an element to appear before failing the test. This is
4852
# useful if a web page needs additional time to load an element.
49-
# If you don't specify a `timeout`, a default timeout is used.
53+
# If you don't specify a "timeout", a default timeout is used.
5054
# Default timeouts are configured in seleniumbase/config/settings.py
5155
#
52-
# 3. SeleniumBase methods are very versatile. For example,
53-
# self.update_text(SELECTOR, TEXT) does the following:
54-
# * Waits for the element to be visible
55-
# * Waits for the element to be interactive
56-
# * Clears the text field
57-
# * Types in the new text
58-
# * Hits Enter/Submit (if the text ends in "\n")
56+
# 3. SeleniumBase methods often perform multiple actions. For example,
57+
# self.type(SELECTOR, TEXT) will do the following:
58+
# * Wait for the element to be visible
59+
# * Wait for the element to be interactive
60+
# * Clear the text field
61+
# * Type in the new text
62+
# * Press Enter/Submit if the text ends in "\n"
5963
#
60-
# self.update_text(S, T) can also be written as self.type(S, T)
64+
# 4. Duplicate method names may exist for the same method:
65+
# (This makes it easier to switch over from other test frameworks.)
66+
# Example:
67+
# self.open() = self.visit() = self.open_url() = self.goto()
68+
# self.type() = self.update_text() = self.input()
69+
# self.send_keys() = self.add_text()
70+
# self.get_element() = self.wait_for_element_present()
71+
# self.find_element() = self.wait_for_element_visible()
72+
# = self.wait_for_element()
73+
# self.assert_element() = self.assert_element_visible()
74+
# self.assert_text() = self.assert_text_visible()
75+
# self.find_text() = self.wait_for_text_visible()
76+
# = self.wait_for_text()
77+
# self.click_link_text(text) = self.click(link=text)
78+
# = self.click_link(text)
79+
# * self.get(url) is SPECIAL: *
80+
# If {url} is a valid URL, self.get() works just like self.open()
81+
# Otherwise {url} becomes a selector for calling self.get_element()
6182
#
62-
# 4. There's usually more than one way to do the same thing. Ex:
83+
# 5. There's usually more than one way to do the same thing.
84+
# Example 1:
6385
# [
6486
# self.assert_text("xkcd: volume 0", "h3")
6587
# ]
@@ -68,33 +90,37 @@ def test_basic(self):
6890
# text = self.get_text("h3")
6991
# self.assert_true("xkcd: volume 0" in text)
7092
# ]
71-
# Or:
93+
# Is also the same as:
7294
# [
73-
# text = self.find_element("h3").text
95+
# element = self.find_element("h3")
96+
# text = element.text
7497
# self.assert_true("xkcd: volume 0" in text)
7598
# ]
7699
#
77-
# And the following line:
100+
# Example 2:
101+
# [
102+
# self.assert_exact_text("xkcd.com", "h2")
103+
# ]
104+
# Is the same as:
105+
# [
106+
# text = self.get_text("h2").strip()
107+
# self.assert_true("xkcd.com".strip() == text)
108+
# ]
109+
#
110+
# Example 3:
78111
# [
79112
# title = self.get_attribute("#comic img", "title")
80113
# ]
81-
# Can also be written as:
114+
# Is the same as:
82115
# [
83116
# element = self.find_element("#comic img")
84117
# title = element.get_attribute("title")
85118
# ]
86119
#
87-
# 5. self.assert_exact_text(TEXT) ignores leading and trailing
120+
# 6. self.assert_exact_text(TEXT) ignores leading and trailing
88121
# whitespace in the TEXT assertion.
89122
# So, self.assert_exact_text("Some Text") will find [" Some Text "].
90123
#
91-
# 6. For backwards-compatibilty, some SeleniumBase methods that do the
92-
# same thing have multiple names, kept on from previous versions.
93-
# Ex: self.wait_for_element() is the same as self.find_element().
94-
# Both search for and return the element, and raise an exception if
95-
# the element does not appear on the page within the timeout limit.
96-
# And self.assert_element() does this too (without returning it).
97-
#
98124
# 7. If a URL starts with "://", then "https://" is automatically used.
99125
# Example: [self.open("://URL")] becomes [self.open("https://URL")]
100126
# This helps by reducing the line length by 5 characters.

examples/parameterized_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class GoogleTestClass(BaseCase):
1111
])
1212
def test_parameterized_google_search(self, search_term, expected_text):
1313
self.open('https://google.com/ncr')
14-
self.update_text('input[title="Search"]', search_term + '\n')
14+
self.type('input[title="Search"]', search_term + '\n')
1515
self.assert_element('#result-stats')
1616
self.assert_text(expected_text, '#search')

examples/swag_labs_suite.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def login(self, username="standard_user"):
1010
self.open("https://www.saucedemo.com/")
1111
if username not in self.get_text("#login_credentials"):
1212
self.fail("Invalid user for login: %s" % username)
13-
self.update_text("#user-name", username)
14-
self.update_text("#password", "secret_sauce")
13+
self.type("#user-name", username)
14+
self.type("#password", "secret_sauce")
1515
self.click('input[type="submit"]')
1616
self.assert_element("#inventory_container")
1717
self.assert_text("Products", "div.product_label")
@@ -58,9 +58,9 @@ def test_swag_labs_basic_functional_flow(self, username):
5858
self.click("link=CHECKOUT")
5959
self.assert_exact_text("Checkout: Your Information", "div.subheader")
6060
self.assert_element("a.cart_cancel_link")
61-
self.update_text("#first-name", "SeleniumBase")
62-
self.update_text("#last-name", "Rocks")
63-
self.update_text("#postal-code", "01720")
61+
self.type("#first-name", "SeleniumBase")
62+
self.type("#last-name", "Rocks")
63+
self.type("#postal-code", "01720")
6464

6565
# Checkout - Overview
6666
self.click("input.btn_primary")

examples/test_apple_site.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_apple_developer_site_webdriver_instructions(self):
1010
self.message_duration = 2.0
1111
self.open("https://developer.apple.com/search/")
1212
title = "Testing with WebDriver in Safari"
13-
self.update_text('[placeholder*="developer.apple.com"]', title + "\n")
13+
self.type('[placeholder*="developer.apple.com"]', title + "\n")
1414
self.click("link=%s" % title)
1515
self.assert_element('[href="/documentation"]')
1616
self.assert_text(title, "h1")

examples/test_demo_site.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,9 @@ def test_demo_site(self):
9393

9494
# Assert exact text
9595
self.assert_exact_text("Demo Page", "h1")
96+
97+
# Assert no broken links
98+
self.assert_no_404_errors()
99+
100+
# Assert no JavaScript errors
101+
self.assert_no_js_errors()

examples/test_event_firing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ def test_event_firing_webdriver(self):
3333
print("\n* EventFiringWebDriver example *")
3434
self.open("https://xkcd.com/1862/")
3535
self.click("link=About")
36-
self.open("https://store.xkcd.com/collections/everything")
37-
self.update_text("input.search-input", "xkcd book\n")
36+
self.open("https://store.xkcd.com/search")
37+
self.type('input[name="q"]', "xkcd book\n")
3838
self.open("https://xkcd.com/1822/")

examples/test_hack_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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.update_text('input[title="Search"]', "SeleniumBase GitHub")
16+
self.type('input[title="Search"]', "SeleniumBase GitHub")
1717
self.click('[value="Bing Search"]')
1818
self.assert_element("h1.b_logo")
1919
self.click('[href*="github.com/seleniumbase/SeleniumBase"]')

examples/test_pytest_parametrize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
@pytest.mark.parametrize('value', ["pytest", "selenium"])
55
def test_sb_fixture_with_no_class(sb, value):
66
sb.open("https://google.com/ncr")
7-
sb.update_text('input[title="Search"]', value + '\n')
7+
sb.type('input[title="Search"]', value + '\n')
88
sb.assert_text(value, "div#center_col")
99

1010

1111
class Test_SB_Fixture():
1212
@pytest.mark.parametrize('value', ["pytest", "selenium"])
1313
def test_sb_fixture_inside_class(self, sb, value):
1414
sb.open("https://google.com/ncr")
15-
sb.update_text('input[title="Search"]', value + '\n')
15+
sb.type('input[title="Search"]', value + '\n')
1616
sb.assert_text(value, "div#center_col")

examples/test_sb_fixture.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# "sb" pytest fixture test in a method with no class
44
def test_sb_fixture_with_no_class(sb):
55
sb.open("https://google.com/ncr")
6-
sb.update_text('input[title="Search"]', 'SeleniumBase\n')
6+
sb.type('input[title="Search"]', 'SeleniumBase\n')
77
sb.click('a[href*="github.com/seleniumbase/SeleniumBase"]')
88
sb.click('a[title="seleniumbase"]')
99

@@ -12,6 +12,6 @@ def test_sb_fixture_with_no_class(sb):
1212
class Test_SB_Fixture():
1313
def test_sb_fixture_inside_class(self, sb):
1414
sb.open("https://google.com/ncr")
15-
sb.update_text('input[title="Search"]', 'SeleniumBase\n')
15+
sb.type('input[title="Search"]', 'SeleniumBase\n')
1616
sb.click('a[href*="github.com/seleniumbase/SeleniumBase"]')
1717
sb.click('a[title="examples"]')

examples/test_swag_labs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def login(self, username="standard_user"):
88
self.open("https://www.saucedemo.com/")
99
if username not in self.get_text("#login_credentials"):
1010
self.fail("Invalid user for login: %s" % username)
11-
self.update_text("#user-name", username)
12-
self.update_text("#password", "secret_sauce")
11+
self.type("#user-name", username)
12+
self.type("#password", "secret_sauce")
1313
self.click('input[type="submit"]')
1414
self.assert_element("#inventory_container")
1515
self.assert_text("Products", "div.product_label")
@@ -50,9 +50,9 @@ def test_swag_labs_basic_flow(self):
5050
self.click("link=CHECKOUT")
5151
self.assert_exact_text("Checkout: Your Information", "div.subheader")
5252
self.assert_element("a.cart_cancel_link")
53-
self.update_text("#first-name", "SeleniumBase")
54-
self.update_text("#last-name", "Rocks")
55-
self.update_text("#postal-code", "01720")
53+
self.type("#first-name", "SeleniumBase")
54+
self.type("#last-name", "Rocks")
55+
self.type("#postal-code", "01720")
5656

5757
# Checkout - Overview
5858
self.click("input.btn_primary")

examples/translations/english_test_1.py

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

66
def test_example_1(self):
7-
url = "https://store.xkcd.com/collections/everything"
7+
url = "https://store.xkcd.com/collections/posters"
88
self.open(url)
99
self.type("input.search-input", "xkcd book\n")
1010
self.assert_text("xkcd: volume 0", "h3")
@@ -17,5 +17,5 @@ def test_example_1(self):
1717
self.click('a[rel="license"]')
1818
self.assert_text("back to this page")
1919
self.go_back()
20-
self.click("link=About")
20+
self.click_link_text("About")
2121
self.assert_exact_text("xkcd.com", "h2")

integrations/node_js/my_first_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
class MyTestClass(BaseCase):
55

66
def test_basic(self):
7+
self.open("https://store.xkcd.com/search")
8+
self.type('input[name="q"]', "xkcd book\n")
9+
self.assert_text("xkcd: volume 0", "h3")
710
self.open("https://xkcd.com/353/")
811
self.assert_title("xkcd: Python")
912
self.assert_element('img[alt="Python"]')
1013
self.click('a[rel="license"]')
1114
self.assert_text("free to copy and reuse")
1215
self.go_back()
13-
self.click("link=About")
14-
self.assert_text("xkcd.com", "h2")
15-
self.open("://store.xkcd.com/collections/everything")
16-
self.update_text("input.search-input", "xkcd book\n")
17-
self.assert_exact_text("xkcd: volume 0", "h3")
16+
self.click_link_text("About")
17+
self.assert_exact_text("xkcd.com", "h2")

0 commit comments

Comments
 (0)