forked from TimurNurlygayanov/ui-tests-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
189 lines (143 loc) · 6.05 KB
/
base.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python3
# -*- encoding=utf8 -*-
import time
from termcolor import colored
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class WebPage(object):
_web_driver = None
def __init__(self, web_driver, url=''):
self._web_driver = web_driver
self.get(url)
def __setattr__(self, name, value):
if not name.startswith('_'):
self.__getattribute__(name)._set_value(self._web_driver, value)
else:
super(WebPage, self).__setattr__(name, value)
def __getattribute__(self, item):
attr = object.__getattribute__(self, item)
if not item.startswith('_') and not callable(attr):
attr._web_driver = self._web_driver
attr._page = self
return attr
def get(self, url):
self._web_driver.get(url)
self.wait_page_loaded()
def go_back(self):
self._web_driver.back()
self.wait_page_loaded()
def refresh(self):
self._web_driver.refresh()
self.wait_page_loaded()
def screenshot(self, file_name='screenshot.png'):
self._web_driver.save_screenshot(file_name)
def scroll_down(self, offset=0):
""" Scroll the page down. """
if offset:
self._web_driver.execute_script('window.scrollTo(0, {0});'.format(offset))
else:
self._web_driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
def scroll_up(self, offset=0):
""" Scroll the page up. """
if offset:
self._web_driver.execute_script('window.scrollTo(0, -{0});'.format(offset))
else:
self._web_driver.execute_script('window.scrollTo(0, -document.body.scrollHeight);')
def switch_to_iframe(self, iframe):
""" Switch to iframe by it's name. """
self._web_driver.switch_to.frame(iframe)
def switch_out_iframe(self):
""" Cancel iframe focus. """
self._web_driver.switch_to.default_content()
def get_current_url(self):
""" Returns current browser URL. """
return self._web_driver.current_url
def get_page_source(self):
""" Returns current page body. """
source = ''
try:
source = self._web_driver.page_source
except:
print(colored('Can not get page source', 'red'))
return source
def check_js_errors(self, ignore_list=None):
""" This function checks JS errors on the page. """
ignore_list = ignore_list or []
logs = self._web_driver.get_log('browser')
for log_message in logs:
if log_message['level'] != 'WARNING':
ignore = False
for issue in ignore_list:
if issue in log_message['message']:
ignore = True
break
assert ignore, 'JS error "{0}" on the page!'.format(log_message)
def wait_page_loaded(self, timeout=60, check_js_complete=True,
check_page_changes=False, check_images=False,
wait_for_element=None,
wait_for_xpath_to_disappear='',
sleep_time=2):
""" This function waits until the page will be completely loaded.
We use many different ways to detect is page loaded or not:
1) Check JS status
2) Check modification in source code of the page
3) Check that all images uploaded completely
(Note: this check is disabled by default)
4) Check that expected elements presented on the page
"""
page_loaded = False
double_check = False
k = 0
if sleep_time:
time.sleep(sleep_time)
# Get source code of the page to track changes in HTML:
source = ''
try:
source = self._web_driver.page_source
except:
pass
# Wait until page loaded (and scroll it, to make sure all objects will be loaded):
while not page_loaded:
time.sleep(0.5)
k += 1
if check_js_complete:
# Scroll down and wait when page will be loaded:
try:
self._web_driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
page_loaded = self._web_driver.execute_script("return document.readyState == 'complete';")
except Exception as e:
pass
if page_loaded and check_page_changes:
# Check if the page source was changed
new_source = ''
try:
new_source = self._web_driver.page_source
except:
pass
page_loaded = new_source == source
source = new_source
# Wait when some element will disappear:
if page_loaded and wait_for_xpath_to_disappear:
bad_element = None
try:
bad_element = WebDriverWait(self._web_driver, 0.1).until(
EC.presence_of_element_located((By.XPATH, wait_for_xpath_to_disappear))
)
except:
pass # Ignore timeout errors
page_loaded = not bad_element
if page_loaded and wait_for_element:
try:
page_loaded = WebDriverWait(self._web_driver, 0.1).until(
EC.element_to_be_clickable(wait_for_element._locator)
)
except:
pass # Ignore timeout errors
assert k < timeout, 'The page loaded more than {0} seconds!'.format(timeout)
# Check two times that page completely loaded:
if page_loaded and not double_check:
page_loaded = False
double_check = True
# Go up:
self._web_driver.execute_script('window.scrollTo(document.body.scrollHeight, 0);')