-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.py
205 lines (170 loc) · 7.9 KB
/
utils.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import datetime
import os
from selenium import webdriver
import settings
def launch_driver(driver_name=settings.DRIVER, desired_capabilities=None):
"""Create and configure a WebDriver.
Args:
driver_name : Name of WebDriver to use
desired_capabilities : Desired browser specs
"""
try:
driver_cls = getattr(webdriver, driver_name)
except AttributeError:
driver_cls = getattr(webdriver, settings.DRIVER)
if driver_name == 'Remote':
if desired_capabilities is None:
desired_capabilities = settings.DESIRED_CAP
command_executor = 'http://{}:{}@hub.browserstack.com:80/wd/hub'.format(
settings.BSTACK_USER, settings.BSTACK_KEY
)
# NOTE: BrowserStack does support the use of Chrome Options, but we are not
# currently using any of them. Below are several steps to setup preferences
# that are specific to Firefox. Currently when running Chrome or Edge in
# BrowserStack we are running with the default base install options.
from selenium.webdriver.firefox.options import Options
ffo = Options()
# Set the default download location [0=Desktop, 1=Downloads, 2=Specified location]
ffo.set_preference('browser.download.folderList', 1)
# Disable the OS-level pop-up modal
ffo.set_preference('browser.download.manager.showWhenStarting', False)
ffo.set_preference('browser.helperApps.alwaysAsk.force', False)
ffo.set_preference('browser.download.manager.alertOnEXEOpen', False)
ffo.set_preference('browser.download.manager.closeWhenDone', True)
ffo.set_preference('browser.download.manager.showAlertOnComplete', False)
ffo.set_preference('browser.download.manager.useWindow', False)
# Specify the file types supported by the download
ffo.set_preference(
'browser.helperApps.neverAsk.saveToDisk',
'text/plain, application/octet-stream, application/binary, text/csv, application/csv, '
'application/excel, text/comma-separated-values, text/xml, application/xml, binary/octet-stream',
)
# Block Third Party Tracking Cookies (Default in Firefox is now 5 which blocks
# all Cross-site cookies)
ffo.set_preference('network.cookie.cookieBehavior', 4)
driver = driver_cls(
command_executor=command_executor,
desired_capabilities=desired_capabilities,
options=ffo,
)
elif driver_name == 'Chrome' and settings.HEADLESS:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('window-size=1200x600')
driver = driver_cls(options=chrome_options)
elif driver_name == 'Chrome' and not settings.HEADLESS:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
# disable w3c for local testing
chrome_options.add_experimental_option('w3c', False)
preferences = {'download.default_directory': ''}
chrome_options.add_experimental_option('prefs', preferences)
driver = driver_cls(options=chrome_options)
elif driver_name == 'Firefox' and not settings.HEADLESS:
from selenium.webdriver.firefox.options import Options
ffo = Options()
# Set the default download location [0=Desktop, 1=Downloads, 2=Specified location]
ffo.set_preference('browser.download.folderList', 1)
ffo.set_preference('browser.download.manager.showWhenStarting', False)
ffo.set_preference('browser.helperApps.alwaysAsk.force', False)
ffo.set_preference(
'browser.helperApps.neverAsk.saveToDisk',
'text/plain, application/octet-stream, application/binary, text/csv, application/csv, '
'application/excel, text/comma-separated-values, text/xml, application/xml, binary/octet-stream',
)
# Block Third Party Tracking Cookies (Default in Firefox is now 5 which blocks
# all Cross-site cookies)
ffo.set_preference('network.cookie.cookieBehavior', 4)
# Force Firefox to open links in new tab instead of new browser window.
ffo.set_preference('browser.link.open_newwindow', 3)
driver = driver_cls(options=ffo)
elif driver_name == 'Edge' and not settings.HEADLESS:
driver = webdriver.Edge()
else:
driver = driver_cls()
driver.maximize_window()
return driver
def find_current_browser(driver):
current_browser = driver.desired_capabilities.get('browserName')
return current_browser
def switch_to_new_tab(driver):
# Took this snippet from browserstack support docs
# https://www.browserstack.com/guide/how-to-switch-tabs-in-selenium-python
# get current window handle
main_window = driver.current_window_handle
# get first child window
all_windows = driver.window_handles
for new_tab in all_windows:
# switch focus to child window
if new_tab != main_window:
driver.switch_to.window(new_tab)
# We need to return the main_window so we can keep track
# of it and when we close the newly opened tab
return main_window
def close_current_tab(driver, main_window):
# close browser tab window
driver.close()
# switch to parent window
driver.switch_to.window(main_window)
def find_row_by_name(files_page, file_name):
all_files = files_page.file_rows
for file_row in all_files:
if file_name in file_row.text:
return file_row
return
def verify_file_download(driver, file_name):
"""Helper function to verify the file download functionality on the Project Files
page.
"""
current_date = datetime.datetime.now()
if settings.DRIVER == 'Remote':
# First verify the downloaded file exists on the virtual remote machine
assert driver.execute_script(
'browserstack_executor: {"action": "fileExists", "arguments": {"fileName": "%s"}}'
% (file_name)
)
# Next get the file properties and then verify that the file creation date is today
file_props = driver.execute_script(
'browserstack_executor: {"action": "getFileProperties", "arguments": {"fileName": "%s"}}'
% (file_name)
)
file_create_date = datetime.datetime.fromtimestamp(file_props['created_time'])
assert file_create_date.date() == current_date.date()
else:
# First verify the downloaded file exists
file_path = os.path.expanduser('~/Downloads/' + file_name)
assert os.path.exists(file_path)
# Next verify the file was downloaded today
file_mtime = os.path.getmtime(file_path)
file_mod_date = datetime.datetime.fromtimestamp(file_mtime)
assert file_mod_date.date() == current_date.date()
def latest_download_file():
path = os.path.expanduser('~/Downloads/')
os.chdir(path)
files = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
newest = files[-1]
return newest
def get_guid_from_url(url, itemno):
url_string_list = url.split('/')
guid = url_string_list[itemno]
return guid
def read_data_from_table(driver, table_path, check_match, item_match=None):
datalist = []
table_data = driver.find_element_by_xpath(table_path)
path1 = table_path + '/tbody/tr'
rows = table_data.find_elements_by_xpath(path1)
rlen = len(rows)
for i in range(1, rlen + 1):
path2 = path1 + '[' + str(i) + ']/td'
items = driver.find_elements_by_xpath(path2)
clen = len(items)
for j in range(1, clen + 1):
path3 = path2 + '[' + str(j) + ']'
cell_data = driver.find_element_by_xpath(path3).text
datalist.append(cell_data)
if check_match:
if item_match in cell_data:
return i, datalist
return rlen, datalist