Skip to content

Commit 9cdc135

Browse files
committed
Optimize the proxy_helper for proxy with auth
1 parent c1b42f1 commit 9cdc135

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
AD_BLOCK_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "ad_block.zip")
3131
RECORDER_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "recorder.zip")
3232
PROXY_ZIP_PATH = proxy_helper.PROXY_ZIP_PATH
33-
PROXY_ZIP_PATH_2 = proxy_helper.PROXY_ZIP_PATH_2
3433
PROXY_ZIP_LOCK = proxy_helper.PROXY_ZIP_LOCK
3534
PLATFORM = sys.platform
3635
IS_WINDOWS = False
@@ -173,6 +172,8 @@ def _add_chrome_proxy_extension(
173172
if not ("-n" in sys.argv or " -n=" in arg_join or arg_join == "-c"):
174173
# Single-threaded
175174
proxy_helper.create_proxy_zip(proxy_string, proxy_user, proxy_pass)
175+
proxy_zip = PROXY_ZIP_PATH
176+
chrome_options.add_extension(proxy_zip)
176177
else:
177178
# Pytest multi-threaded test
178179
import fasteners
@@ -183,11 +184,8 @@ def _add_chrome_proxy_extension(
183184
proxy_helper.create_proxy_zip(
184185
proxy_string, proxy_user, proxy_pass
185186
)
186-
proxy_zip = PROXY_ZIP_PATH
187-
if not os.path.exists(PROXY_ZIP_PATH):
188-
# Handle "Permission denied" on the default proxy.zip path
189-
proxy_zip = PROXY_ZIP_PATH_2
190-
chrome_options.add_extension(proxy_zip)
187+
proxy_zip = PROXY_ZIP_PATH
188+
chrome_options.add_extension(proxy_zip)
191189
return chrome_options
192190

193191

seleniumbase/core/proxy_helper.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
import threading
33
import zipfile
44
from seleniumbase.fixtures import constants
5-
from seleniumbase import drivers
65

7-
DRIVER_DIR = os.path.dirname(os.path.realpath(drivers.__file__))
8-
PROXY_ZIP_PATH = "%s/%s" % (DRIVER_DIR, "proxy.zip")
96
DOWNLOADS_DIR = constants.Files.DOWNLOADS_FOLDER
10-
PROXY_ZIP_PATH_2 = "%s/%s" % (DOWNLOADS_DIR, "proxy.zip")
7+
PROXY_ZIP_PATH = "%s/%s" % (DOWNLOADS_DIR, "proxy.zip")
118
PROXY_ZIP_LOCK = "%s/%s" % (DOWNLOADS_DIR, "proxy.lock")
129

1310

@@ -70,15 +67,11 @@ def create_proxy_zip(proxy_string, proxy_user, proxy_pass):
7067
)
7168
lock = threading.RLock() # Support multi-threaded test runs with Pytest
7269
with lock:
73-
try:
74-
zf = zipfile.ZipFile(PROXY_ZIP_PATH, mode="w")
75-
except IOError:
76-
# Handle "Permission denied" on the default proxy.zip path
77-
abs_path = os.path.abspath(".")
78-
downloads_path = os.path.join(abs_path, DOWNLOADS_DIR)
79-
if not os.path.exists(downloads_path):
80-
os.mkdir(downloads_path)
81-
zf = zipfile.ZipFile(PROXY_ZIP_PATH_2, mode="w")
70+
abs_path = os.path.abspath(".")
71+
downloads_path = os.path.join(abs_path, DOWNLOADS_DIR)
72+
if not os.path.exists(downloads_path):
73+
os.mkdir(downloads_path)
74+
zf = zipfile.ZipFile(PROXY_ZIP_PATH, mode="w")
8275
zf.writestr("background.js", background_js)
8376
zf.writestr("manifest.json", manifest_json)
8477
zf.close()
@@ -92,8 +85,6 @@ def remove_proxy_zip_if_present():
9285
try:
9386
if os.path.exists(PROXY_ZIP_PATH):
9487
os.remove(PROXY_ZIP_PATH)
95-
elif os.path.exists(PROXY_ZIP_PATH_2):
96-
os.remove(PROXY_ZIP_PATH_2)
9788
if os.path.exists(PROXY_ZIP_LOCK):
9889
os.remove(PROXY_ZIP_LOCK)
9990
except Exception:

0 commit comments

Comments
 (0)