/
utils.py
129 lines (108 loc) · 5.37 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
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)