1
+ import re
1
2
import warnings
2
3
from selenium import webdriver
3
4
from selenium .common .exceptions import WebDriverException
4
5
from selenium .webdriver .common .desired_capabilities import DesiredCapabilities
5
6
from seleniumbase .config import settings
7
+ from seleniumbase .config import proxy_list
6
8
from seleniumbase .core import download_helper
7
9
from seleniumbase .fixtures import constants
8
10
9
11
10
- def _create_firefox_profile (downloads_path ):
12
+ def _create_firefox_profile (downloads_path , proxy_string ):
11
13
profile = webdriver .FirefoxProfile ()
12
14
profile .set_preference ("reader.parse-on-load.enabled" , False )
13
15
profile .set_preference ("pdfjs.disabled" , True )
16
+ if proxy_string :
17
+ proxy_server = proxy_string .split (':' )[0 ]
18
+ proxy_port = proxy_string .split (':' )[1 ]
19
+ profile .set_preference ("network.proxy.type" , 1 )
20
+ profile .set_preference ("network.proxy.http" , proxy_server )
21
+ profile .set_preference ("network.proxy.http_port" , int (proxy_port ))
22
+ profile .set_preference ("network.proxy.ssl" , proxy_server )
23
+ profile .set_preference ("network.proxy.ssl_port" , int (proxy_port ))
14
24
profile .set_preference (
15
25
"security.mixed_content.block_active_content" , False )
16
26
profile .set_preference (
@@ -30,15 +40,43 @@ def _create_firefox_profile(downloads_path):
30
40
return profile
31
41
32
42
43
+ def display_proxy_warning (proxy_string ):
44
+ message = ('\n \n WARNING: Proxy String ["%s"] is NOT in the expected '
45
+ '"ip_address:port" format, (OR the key does not exist '
46
+ 'in proxy_list.PROXY_LIST). '
47
+ '*** DEFAULTING to NOT USING a Proxy Server! ***'
48
+ % proxy_string )
49
+ warnings .simplefilter ('always' , Warning ) # See Warnings
50
+ warnings .warn (message , category = Warning , stacklevel = 2 )
51
+ warnings .simplefilter ('default' , Warning ) # Set Default
52
+
53
+
54
+ def validate_proxy_string (proxy_string ):
55
+ if proxy_string in proxy_list .PROXY_LIST .keys ():
56
+ proxy_string = proxy_list .PROXY_LIST [proxy_string ]
57
+ if not proxy_string :
58
+ return None
59
+ valid = re .match ('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$' , proxy_string )
60
+ if valid :
61
+ proxy_string = valid .group ()
62
+ else :
63
+ display_proxy_warning (proxy_string )
64
+ proxy_string = None
65
+ return proxy_string
66
+
67
+
33
68
def get_driver (browser_name , headless = False , use_grid = False ,
34
- servername = 'localhost' , port = 4444 ):
69
+ servername = 'localhost' , port = 4444 , proxy_string = None ):
70
+ if proxy_string :
71
+ proxy_string = validate_proxy_string (proxy_string )
35
72
if use_grid :
36
- return get_remote_driver (browser_name , headless , servername , port )
73
+ return get_remote_driver (
74
+ browser_name , headless , servername , port , proxy_string )
37
75
else :
38
- return get_local_driver (browser_name , headless )
76
+ return get_local_driver (browser_name , headless , proxy_string )
39
77
40
78
41
- def get_remote_driver (browser_name , headless , servername , port ):
79
+ def get_remote_driver (browser_name , headless , servername , port , proxy_string ):
42
80
downloads_path = download_helper .get_downloads_folder ()
43
81
download_helper .reset_downloads_folder ()
44
82
address = "http://%s:%s/wd/hub" % (servername , port )
@@ -58,6 +96,8 @@ def get_remote_driver(browser_name, headless, servername, port):
58
96
chrome_options .add_argument ("--disable-infobars" )
59
97
if headless :
60
98
chrome_options .add_argument ("--headless" )
99
+ if proxy_string :
100
+ chrome_options .add_argument ('--proxy-server=%s' % proxy_string )
61
101
if settings .START_CHROME_IN_FULL_SCREEN_MODE :
62
102
# Run Chrome in full screen mode on WINDOWS
63
103
chrome_options .add_argument ("--start-maximized" )
@@ -71,7 +111,7 @@ def get_remote_driver(browser_name, headless, servername, port):
71
111
if browser_name == constants .Browser .FIREFOX :
72
112
try :
73
113
# Use Geckodriver for Firefox if it's on the PATH
74
- profile = _create_firefox_profile (downloads_path )
114
+ profile = _create_firefox_profile (downloads_path , proxy_string )
75
115
firefox_capabilities = DesiredCapabilities .FIREFOX .copy ()
76
116
firefox_capabilities ['marionette' ] = True
77
117
if headless :
@@ -85,7 +125,7 @@ def get_remote_driver(browser_name, headless, servername, port):
85
125
browser_profile = profile )
86
126
except WebDriverException :
87
127
# Don't use Geckodriver: Only works for old versions of Firefox
88
- profile = _create_firefox_profile (downloads_path )
128
+ profile = _create_firefox_profile (downloads_path , proxy_string )
89
129
firefox_capabilities = DesiredCapabilities .FIREFOX .copy ()
90
130
firefox_capabilities ['marionette' ] = False
91
131
if headless :
@@ -122,7 +162,7 @@ def get_remote_driver(browser_name, headless, servername, port):
122
162
webdriver .DesiredCapabilities .PHANTOMJS ))
123
163
124
164
125
- def get_local_driver (browser_name , headless ):
165
+ def get_local_driver (browser_name , headless , proxy_string ):
126
166
'''
127
167
Spins up a new web browser and returns the driver.
128
168
Can also be used to spin up additional browsers for the same test.
@@ -134,7 +174,7 @@ def get_local_driver(browser_name, headless):
134
174
try :
135
175
try :
136
176
# Use Geckodriver for Firefox if it's on the PATH
137
- profile = _create_firefox_profile (downloads_path )
177
+ profile = _create_firefox_profile (downloads_path , proxy_string )
138
178
firefox_capabilities = DesiredCapabilities .FIREFOX .copy ()
139
179
firefox_capabilities ['marionette' ] = True
140
180
options = webdriver .FirefoxOptions ()
@@ -145,7 +185,7 @@ def get_local_driver(browser_name, headless):
145
185
firefox_options = options )
146
186
except WebDriverException :
147
187
# Don't use Geckodriver: Only works for old versions of Firefox
148
- profile = _create_firefox_profile (downloads_path )
188
+ profile = _create_firefox_profile (downloads_path , proxy_string )
149
189
firefox_capabilities = DesiredCapabilities .FIREFOX .copy ()
150
190
firefox_capabilities ['marionette' ] = False
151
191
firefox_driver = webdriver .Firefox (
@@ -182,12 +222,14 @@ def get_local_driver(browser_name, headless):
182
222
chrome_options .add_argument ("--disable-infobars" )
183
223
if headless :
184
224
chrome_options .add_argument ("--headless" )
225
+ if proxy_string :
226
+ chrome_options .add_argument ('--proxy-server=%s' % proxy_string )
185
227
if settings .START_CHROME_IN_FULL_SCREEN_MODE :
186
228
# Run Chrome in full screen mode on WINDOWS
187
229
chrome_options .add_argument ("--start-maximized" )
188
230
# Run Chrome in full screen mode on MAC/Linux
189
231
chrome_options .add_argument ("--kiosk" )
190
- return webdriver .Chrome (chrome_options = chrome_options )
232
+ return webdriver .Chrome (options = chrome_options )
191
233
except Exception as e :
192
234
if headless :
193
235
raise Exception (e )
0 commit comments