Add and update example tests

This commit is contained in:
Michael Mintz 2020-08-13 03:41:09 -04:00
parent 5032aaf29b
commit 18773022d5
3 changed files with 39 additions and 5 deletions

View File

@ -1,7 +1,3 @@
"""
Same as my_first_test.py, but without the asserts.
"""
from seleniumbase import BaseCase
@ -10,7 +6,10 @@ 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 book", "div.results")
self.open("https://xkcd.com/353/")
self.click('a[rel="license"]')
self.go_back()
self.click_link_text("About")
self.click_link_text("comic #249")
self.assert_element('img[alt*="Chess"]')

View File

@ -5,7 +5,8 @@ class MyTestClass(BaseCase):
def test_basic(self):
self.open("https://store.xkcd.com/search")
self.type('input[name="q"]', "xkcd book\n")
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")
@ -15,6 +16,8 @@ class MyTestClass(BaseCase):
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")
####

32
examples/nth_child_test.py Executable file
View File

@ -0,0 +1,32 @@
from seleniumbase import BaseCase
class NthChildSelectorTests(BaseCase):
def test_locate_rows_with_colors(self):
self.open("https://xkcd.com/color/rgb/")
tbody = "center > table tbody"
self.demo_mode = True
self.highlight(tbody)
self.post_message("Part 1: Assert text in given row.")
self.assert_text("teal", "tr:nth-child(2)")
self.assert_text("aqua", "tr:nth-child(4)")
self.assert_text("mint", "tr:nth-child(14)")
self.assert_text("jade", "tr:nth-child(36)")
soup = self.get_beautiful_soup(self.get_page_source())
self.post_message("Part 2: Find row with given text.")
self.locate_first_row_with_color("rust", tbody, soup)
self.locate_first_row_with_color("azure", tbody, soup)
self.locate_first_row_with_color("topaz", tbody, soup)
def locate_first_row_with_color(self, color, tbody, soup):
rows = soup.body.table.find_all("tr")
num_rows = len(rows)
for row in range(num_rows):
row_selector = tbody + " tr:nth-child(%s)" % (row + 1)
if color in rows[row].text:
message = '"%s" found on row %s' % (color, row + 1)
self.post_message_and_highlight(message, row_selector)
return # Found row and done
self.post_error_message(
'"%s" could not be found on any row!' % color)