- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathrefined_raw.py
127 lines (111 loc) · 4.64 KB
/
refined_raw.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
"""Refined Raw Selenium Example - (ONLY Selenium / NO SeleniumBase)"""
importsys
fromseleniumimportwebdriver
fromselenium.webdriver.chrome.serviceimportService
fromselenium.webdriver.supportimportexpected_conditionsasEC
fromselenium.webdriver.support.uiimportWebDriverWait
fromunittestimportTestCase
classRefinedRawSelenium(TestCase):
defsetUp(self):
self.driver=None
options=webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
if"linux"insys.platform:
options.add_argument("--headless=new")
options.add_experimental_option(
"excludeSwitches", ["enable-automation", "enable-logging"],
)
prefs= {
"credentials_enable_service": False,
"profile.password_manager_enabled": False,
}
options.add_experimental_option("prefs", prefs)
service=Service(service_args=["--disable-build-check"])
self.driver=webdriver.Chrome(options=options, service=service)
deftearDown(self):
ifself.driver:
try:
ifself.driver.service.process:
self.driver.quit()
exceptException:
pass
defwait_for_element_visible(
self, selector, by="css selector", timeout=10
):
try:
returnWebDriverWait(self.driver, timeout).until(
EC.visibility_of_element_located((by, selector))
)
exceptException:
raiseException(
"Element {%s} was not visible after %s seconds!"
% (selector, timeout)
)
defwait_for_element_clickable(
self, selector, by="css selector", timeout=10
):
try:
returnWebDriverWait(self.driver, timeout).until(
EC.element_to_be_clickable((by, selector))
)
exceptException:
raiseException(
"Element {%s} was not visible/clickable after %s seconds!"
% (selector, timeout)
)
defwait_for_element_not_visible(
self, selector, by="css selector", timeout=10
):
try:
returnWebDriverWait(self.driver, timeout).until(
EC.invisibility_of_element((by, selector))
)
exceptException:
raiseException(
"Element {%s} was still visible after %s seconds!"
% (selector, timeout)
)
defopen(self, url):
self.driver.get(url)
defclick(self, selector, by="css selector", timeout=7):
el=self.wait_for_element_clickable(selector, by=by, timeout=timeout)
el.click()
deftype(self, selector, text, by="css selector", timeout=10):
el=self.wait_for_element_clickable(selector, by=by, timeout=timeout)
el.clear()
ifnottext.endswith("\n"):
el.send_keys(text)
else:
el.send_keys(text[:-1])
el.submit()
defassert_element(self, selector, by="css selector", timeout=7):
self.wait_for_element_visible(selector, by=by, timeout=timeout)
defassert_text(self, text, selector="html", by="css selector", timeout=7):
el=self.wait_for_element_visible(selector, by=by, timeout=timeout)
self.assertIn(text, el.text)
defassert_exact_text(self, text, selector, by="css selector", timeout=7):
el=self.wait_for_element_visible(selector, by=by, timeout=timeout)
self.assertEqual(text, el.text)
defassert_element_not_visible(
self, selector, by="css selector", timeout=7
):
self.wait_for_element_not_visible(selector, by=by, timeout=timeout)
deftest_add_item_to_cart(self):
self.open("https://www.saucedemo.com")
self.type("#user-name", "standard_user")
self.type("#password", "secret_sauce\n")
self.assert_element("div.inventory_list")
self.assert_text("Products", "span.title")
self.click('button[name*="backpack"]')
self.click("#shopping_cart_container a")
self.assert_exact_text("Your Cart", "span.title")
self.assert_text("Backpack", "div.cart_item")
self.click("#remove-sauce-labs-backpack")
self.assert_element_not_visible("div.cart_item")
self.click("#react-burger-menu-btn")
self.click("a#logout_sidebar_link")
self.assert_element("input#login-button")
# When run with "python" instead of "pytest" or "python -m unittest"
if__name__=="__main__":
fromunittestimportmain
main()