/
my_first_test.py
executable file
路133 lines (128 loc) 路 5.54 KB
/
my_first_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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")
self.click('input[value="Search"]')
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_text("About")
self.assert_exact_text("xkcd.com", "h2")
self.click_link_text("geohashing")
self.assert_element("#comic img")
####
#######################################################################
#
# **** NOTES / USEFUL INFO ****
#
# 1. By default, CSS Selectors are used to identify elements.
# 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')
# ]
#
# 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.
# 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 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.
# Default timeouts are configured in seleniumbase/config/settings.py
#
# 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"
#
# 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()
#
# 5. There's usually more than one way to do the same thing.
# Example 1:
# [
# self.assert_text("xkcd: volume 0", "h3")
# ]
# Is the same as:
# [
# text = self.get_text("h3")
# self.assert_true("xkcd: volume 0" in text)
# ]
# Is also the same as:
# [
# element = self.find_element("h3")
# text = element.text
# self.assert_true("xkcd: volume 0" in text)
# ]
#
# 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")
# ]
# Is the same as:
# [
# element = self.find_element("#comic img")
# title = element.get_attribute("title")
# ]
#
# 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 "].
#
# 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.
#
# 8. For the full method list, see one of the following:
# * SeleniumBase/seleniumbase/fixtures/base_case.py
# * SeleniumBase/help_docs/method_summary.md