SeleniumBase/examples/my_first_test.py

101 lines
4.2 KiB
Python
Raw Normal View History

2015-12-05 05:11:53 +08:00
from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_basic(self):
2019-07-17 13:36:03 +08:00
self.open("https://xkcd.com/353/")
2019-08-18 04:54:16 +08:00
self.assert_title("xkcd: Python")
2019-09-05 16:41:36 +08:00
self.assert_element('img[alt="Python"]')
2019-07-17 13:36:03 +08:00
self.click('a[rel="license"]')
self.assert_text("free to copy and reuse")
2019-08-13 14:27:03 +08:00
self.go_back()
self.click("link=About")
self.assert_text("xkcd.com", "h2")
2020-04-21 14:39:00 +08:00
self.open("://store.xkcd.com/collections/everything")
2019-08-06 13:39:32 +08:00
self.update_text("input.search-input", "xkcd book\n")
2019-08-13 14:27:03 +08:00
self.assert_exact_text("xkcd: volume 0", "h3")
####
#######################################################################
#
# **** NOTES / USEFUL INFO ****
#
# 1. By default, CSS Selectors are used to identify elements.
2018-03-26 09:15:33 +08:00
# Other options 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)
# ]
2017-04-15 03:57:00 +08:00
# 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
2018-03-26 09:15:33 +08:00
# web page and select "Inspect" to see the CSS in the html.
#
# 2. Most methods have the optional `timeout` argument. Ex:
# [
2019-08-13 14:27:03 +08:00
# 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
# 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
#
2019-10-22 14:14:16 +08:00
# 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")
#
2019-10-29 12:36:07 +08:00
# self.update_text(S, T) can also be written as self.type(S, T)
#
2019-10-22 14:14:16 +08:00
# 4. There's usually more than one way to do the same thing. Ex:
# [
2019-08-06 14:00:05 +08:00
# self.assert_text("xkcd: volume 0", "h3")
# ]
2018-03-26 09:15:33 +08:00
# Is the same as:
2017-04-15 03:57:00 +08:00
# [
2019-08-06 14:00:05 +08:00
# text = self.get_text("h3")
# self.assert_true("xkcd: volume 0" in text)
2017-04-15 03:57:00 +08:00
# ]
2018-03-26 09:15:33 +08:00
# Or:
2017-04-15 03:57:00 +08:00
# [
2019-08-06 14:00:05 +08:00
# text = self.find_element("h3").text
2019-08-13 15:30:11 +08:00
# self.assert_true("xkcd: volume 0" in text)
2017-04-15 03:57:00 +08:00
# ]
#
# And the following line:
# [
2018-03-26 09:15:33 +08:00
# title = self.get_attribute("#comic img", "title")
# ]
# Can also be written as:
# [
2018-03-26 09:15:33 +08:00
# element = self.find_element("#comic img")
# title = element.get_attribute("title")
# ]
#
2019-10-22 14:14:16 +08:00
# 5. self.assert_exact_text(TEXT) ignores leading and trailing
2019-08-13 14:27:03 +08:00
# whitespace in the TEXT assertion.
# So, self.assert_exact_text("Some Text") will find [" Some Text "].
#
2019-10-22 14:14:16 +08:00
# 6. For backwards-compatibilty, some SeleniumBase methods that do the
2018-06-12 13:00:22 +08:00
# same thing have multiple names, kept on from previous versions.
2020-05-25 16:25:47 +08:00
# 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.
2020-05-25 16:25:47 +08:00
# And self.assert_element() does this too (without returning it).
2018-06-12 13:00:22 +08:00
#
2019-10-22 14:14:16 +08:00
# 7. For the full method list, see one of the following:
2019-08-13 15:30:11 +08:00
# * SeleniumBase/seleniumbase/fixtures/base_case.py
# * SeleniumBase/help_docs/method_summary.md