-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSeleniumBase_snippets.py
More file actions
148 lines (109 loc) · 4.84 KB
/
Copy pathSeleniumBase_snippets.py
File metadata and controls
148 lines (109 loc) · 4.84 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# SeleniumBase is a Python framework for web automation
# Docs: https://github.com/seleniumbase/SeleniumBase
# PyPi: pip install seleniumbase
# common SeleniumBase methods
"""
self.open(URL) # Navigate to the web page
self.click(SELECTOR) # Click a page element
self.type(SELECTOR, TEXT) # Type text (Add "\n" to text for pressing enter/return.)
self.assert_element(SELECTOR) # Assert element is visible
self.assert_text(TEXT) # Assert text is visible (has optional SELECTOR arg)
self.assert_title(PAGE_TITLE) # Assert page title
self.assert_no_404_errors() # Assert no 404 errors from files on the page
self.assert_no_js_errors() # Assert no JavaScript errors on the page (Chrome-ONLY)
self.execute_script(JAVASCRIPT) # Execute JavaScript code
self.go_back() # Navigate to the previous URL
self.get_text(SELECTOR) # Get text from a selector
self.get_attribute(SELECTOR, ATTRIBUTE) # Get a specific attribute from a selector
self.is_element_visible(SELECTOR) # Determine if an element is visible on the page
self.is_text_visible(TEXT) # Determine if text is visible on the page (optional SELECTOR)
self.hover_and_click(HOVER_SELECTOR, CLICK_SELECTOR) # Mouseover element & click another
self.select_option_by_text(DROPDOWN_SELECTOR, OPTION_TEXT) # Select a dropdown option
self.switch_to_frame(FRAME_NAME) # Switch webdriver control to an iframe on the page
self.switch_to_default_content() # Switch webdriver control out of the current iframe
self.switch_to_window(WINDOW_NUMBER) # Switch to a different window/tab
self.save_screenshot(FILE_NAME) # Save a screenshot of the current page
"""
# for reports use:
# pytest test_suite.py --html=report.html
# or
# pytest test_suite.py --dashboard --html=report.html
# find all links on the page
links = driver.find_elements_by_xpath("//a[@href]")
for link in links:
print(elem.get_attribute("href"))
# click on the alert box
WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.switch_to.alert.accept()
# Cookies
# read cookies
import pickle
import selenium.webdriver
driver = selenium.webdriver.Firefox()
driver.get("http://www...")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
# or
driver = selenium.webdriver.Firefox()
driver.get("http://www...")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
cookie.pop('same_site')
driver.add_cookie(cook)
driver.refresh()
# save cookies
import pickle
import selenium.webdriver
driver = selenium.webdriver.Firefox()
driver.get("http://www...")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
# scrape multiple pages
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
result_list = []
for page in range(1, 5, 1):
page_url = "https://www.../?page=" + str(page)
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(page_url)
title = driver.find_elements_by_class_name("title")
price = driver.find_elements_by_class_name("price")
description = driver.find_elements_by_class_name("description")
for i in range(len(title)):
result_list.append([title[i].text, price[i].text, description[i].text)
driver.close()
# example , test login
from seleniumbase import BaseCase
class SwagLabsLoginTests(BaseCase):
def login_to_swag_labs(self):
""" Login to Swag Labs and verify that login was successful. """
self.open("https://www.saucedemo.com/")
self.type("#user-name", "standard_user")
self.type("#password", "secret_sauce")
self.click('input[type="submit"]')
def test_swag_labs_login(self):
""" This test checks standard login for the Swag Labs store. """
self.login_to_swag_labs()
self.assert_element("div.header_label div.app_logo")
self.assert_text("Products", "div.product_label")
# here's an example where clicking a button makes a hidden element visible !
from seleniumbase import BaseCase
class VisualLayoutTest(BaseCase):
def test_applitools_layout_change_failure(self):
self.open('https://www...')
self.check_window(name="helloworld", baseline=True)
# Click a button that changes the text of an element
self.click('...')
# Click a button that makes a hidden element visible
self.click("button")
self.check_window(name="helloworld", level=3)
# here's an example where a button is removed from a web page
from seleniumbase import BaseCase
class VisualLayoutTest(BaseCase):
def test_python_home_layout_change_failure(self):
self.open('https://www...')
self.check_window(name="python_home", baseline=True)
self.remove_element('a.button')
self.check_window(name="python_home", level=3)