-
Notifications
You must be signed in to change notification settings - Fork 5
/
Session12_Waits.py
174 lines (143 loc) · 6.02 KB
/
Session12_Waits.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
from datetime import datetime
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.maximize_window()
actions = ActionChains(driver)
driver.implicitly_wait(1)
# 1 - Sleep
print(datetime.now())
sleep(3)
print(datetime.now())
#######################################################
# 2 - Implicitly Wait
driver.get("http://play1.automationcamp.ir/index.html")
driver.implicitly_wait(3)
print(datetime.now())
try:
driver.find_element(By.XPATH, "//*[text()='sjguiafhipe']")
except:
print(datetime.now())
#######################################################
# 3 - Wait until element has an attribute
def wait_until_element_has_an_attribute(element_selector, element_locator, attribute, attribute_value, timeout=5,
exact=True):
for i in range(timeout * 5):
try:
element = driver.find_element(element_selector, element_locator)
if exact:
assert element.get_attribute(attribute) == attribute_value
else:
assert attribute_value in element.get_attribute(attribute)
print(" Element attribute met: " + str(attribute_value))
return
except:
sleep(0.2)
raise Exception("Element attribute is not: " + str(attribute_value))
# 4 - Wait until element has not an attribute
def wait_until_element_has_not_an_attribute(element_selector, element_locator, attribute, attribute_value, timeout=5,
exact=True):
for i in range(timeout * 5):
try:
element = driver.find_element(element_selector, element_locator)
if exact:
assert element.get_attribute(attribute) != attribute_value
else:
assert attribute_value not in element.get_attribute(attribute)
print(" Element attribute not in or not equals: " + str(attribute_value))
return
except:
sleep(0.2)
raise Exception("Element attribute in or equals: " + str(attribute_value))
driver.get("http://play1.automationcamp.ir/expected_conditions.html")
trigger = driver.find_element(By.ID, "enabled_trigger")
trigger.location_once_scrolled_into_view
wait_until_element_has_an_attribute(By.ID, "enabled_target", 'class', 'danger', exact=False)
wait_until_element_has_not_an_attribute(By.ID, "enabled_target", 'class', 'success', exact=False)
trigger.click()
wait_until_element_has_an_attribute(By.ID, "enabled_target", 'class', 'success', exact=False)
wait_until_element_has_not_an_attribute(By.ID, "enabled_target", 'class', 'danger', exact=False)
print("Test case PASSED")
#######################################################
# 5 - Wait until element is enabled
def wait_until_element_is_enabled(selector, locator, timeout):
for i in range(timeout * 2):
try:
element = driver.find_element(selector, locator)
assert element.is_enabled()
return
except:
sleep(0.5)
# 6 - Wait until element is not enabled
def wait_until_element_is_not_enabled(selector, locator, timeout):
for i in range(timeout * 2):
try:
element = driver.find_element(selector, locator)
assert not element.is_enabled()
return
except:
sleep(0.5)
driver.get("http://play1.automationcamp.ir/expected_conditions.html")
trigger = driver.find_element(By.ID, "enabled_trigger")
trigger.location_once_scrolled_into_view
trigger.click()
wait_until_element_is_enabled(By.ID, "enabled_target", 5)
print("Element is enabled now!")
#######################################################
# 7 - Wait until element is visible
def wait_until_element_is_visible(selector, locator, timeout=5):
for i in range(timeout * 2):
try:
element = driver.find_element(selector, locator)
assert element.is_displayed()
print("Test is PASSED")
return
except:
sleep(0.5)
# 8 - Wait until element is not invisible
def wait_until_element_is_not_visible(selector, locator, timeout=5):
for i in range(timeout * 2):
try:
element = driver.find_element(selector, locator)
assert not element.is_displayed()
print("Test is PASSED")
return
except:
sleep(0.5)
driver.get("http://play1.automationcamp.ir/expected_conditions.html")
trigger = driver.find_element(By.ID, "visibility_trigger")
trigger.location_once_scrolled_into_view
print(driver.find_element(By.ID, "visibility_target").is_displayed())
trigger.click()
wait_until_element_is_visible(By.ID, "visibility_target")
print(driver.find_element(By.ID, "visibility_target").is_displayed())
#######################################################
# 9 - WebDriverWait until/until not Expected Conditions
driver.get("http://play1.automationcamp.ir/expected_conditions.html")
trigger = driver.find_element(By.ID, "enabled_trigger")
trigger.location_once_scrolled_into_view
trigger.click()
wait = WebDriverWait(driver, 5)
element = wait.until(EC.element_to_be_clickable((By.ID, "enabled_target")))
print(element)
#######################################################
# 10 - Wait until page is loaded
def wait_until_page_is_loaded(timeout=10):
for i in range(timeout * 2):
try:
state = driver.execute_script("return document.readyState")
assert state == 'complete'
print("State is: " + str(state))
return
except:
sleep(0.5)
driver.get("https://archive.org/details/audio_bookspoetry")
wait_until_page_is_loaded()