Skip to content

Commit b00f75b

Browse files
committed
os.path.join() should always be used when joining paths
1 parent d4a7d8f commit b00f75b

File tree

12 files changed

+74
-71
lines changed

12 files changed

+74
-71
lines changed

seleniumbase/console_scripts/sb_install.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ def main(override=None):
555555
zip_ref.extractall(downloads_folder)
556556
zip_ref.close()
557557
os.remove(zip_file_path)
558-
shutil.copyfile(driver_path, "%s/%s" % (downloads_folder, filename))
558+
shutil.copyfile(driver_path, os.path.join(downloads_folder, filename))
559559
print("Unzip Complete!\n")
560560
to_remove = [
561561
"%s/%s/ruby_example/Gemfile" % (downloads_folder, h_ie_fn),
@@ -571,10 +571,10 @@ def main(override=None):
571571
if os.path.exists("%s/%s/ruby_example/" % (downloads_folder, h_ie_fn)):
572572
# Only works if the directory is empty
573573
os.rmdir("%s/%s/ruby_example/" % (downloads_folder, h_ie_fn))
574-
if os.path.exists("%s/%s/" % (downloads_folder, h_ie_fn)):
574+
if os.path.exists(os.path.join(downloads_folder, h_ie_fn)):
575575
# Only works if the directory is empty
576-
os.rmdir("%s/%s/" % (downloads_folder, h_ie_fn))
577-
driver_path = "%s/%s" % (downloads_folder, filename)
576+
os.rmdir(os.path.join(downloads_folder, h_ie_fn))
577+
driver_path = os.path.join(downloads_folder, filename)
578578
print(
579579
"The file [%s] was saved to:\n%s%s%s\n"
580580
% (filename, c3, driver_path, cr)

seleniumbase/core/browser_launcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
# Put the SeleniumBase DRIVER_DIR at the beginning of the System PATH
3636
os.environ["PATH"] = DRIVER_DIR + os.pathsep + os.environ["PATH"]
3737
EXTENSIONS_DIR = os.path.dirname(os.path.realpath(extensions.__file__))
38-
DISABLE_CSP_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "disable_csp.zip")
39-
AD_BLOCK_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "ad_block.zip")
40-
RECORDER_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "recorder.zip")
38+
DISABLE_CSP_ZIP_PATH = os.path.join(EXTENSIONS_DIR, "disable_csp.zip")
39+
AD_BLOCK_ZIP_PATH = os.path.join(EXTENSIONS_DIR, "ad_block.zip")
40+
RECORDER_ZIP_PATH = os.path.join(EXTENSIONS_DIR, "recorder.zip")
4141
PROXY_ZIP_PATH = proxy_helper.PROXY_ZIP_PATH
4242
PROXY_ZIP_LOCK = proxy_helper.PROXY_ZIP_LOCK
4343
PLATFORM = sys.platform

seleniumbase/core/log_helper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
def log_screenshot(test_logpath, driver, screenshot=None, get=False):
1313
screenshot_name = settings.SCREENSHOT_NAME
14-
screenshot_path = "%s/%s" % (test_logpath, screenshot_name)
14+
screenshot_path = os.path.join(test_logpath, screenshot_name)
1515
screenshot_warning = constants.Warnings.SCREENSHOT_UNDEFINED
1616
try:
1717
if not screenshot:
@@ -126,7 +126,7 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
126126
if not driver_displayed:
127127
driver_displayed = "(Unknown Driver)"
128128
basic_info_name = settings.BASIC_INFO_NAME
129-
basic_file_path = "%s/%s" % (test_logpath, basic_info_name)
129+
basic_file_path = os.path.join(test_logpath, basic_info_name)
130130
if url:
131131
last_page = url
132132
else:
@@ -286,7 +286,7 @@ def log_page_source(test_logpath, driver, source=None):
286286
"unresponsive, or closed prematurely!</h4>"
287287
)
288288
)
289-
html_file_path = "%s/%s" % (test_logpath, html_file_name)
289+
html_file_path = os.path.join(test_logpath, html_file_name)
290290
html_file = codecs.open(html_file_path, "w+", "utf-8")
291291
html_file.write(page_source)
292292
html_file.close()

seleniumbase/core/proxy_helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from seleniumbase.fixtures import constants
44

55
DOWNLOADS_DIR = constants.Files.DOWNLOADS_FOLDER
6-
PROXY_ZIP_PATH = "%s/%s" % (DOWNLOADS_DIR, "proxy.zip")
7-
PROXY_ZIP_LOCK = "%s/%s" % (DOWNLOADS_DIR, "proxy.lock")
6+
PROXY_ZIP_PATH = os.path.join(DOWNLOADS_DIR, "proxy.zip")
7+
PROXY_ZIP_LOCK = os.path.join(DOWNLOADS_DIR, "proxy.lock")
88

99

1010
def create_proxy_zip(proxy_string, proxy_user, proxy_pass):

seleniumbase/core/report_helper.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def process_successes(test, test_count, duration):
3434
def process_failures(test, test_count, browser_type, duration):
3535
bad_page_image = "failure_%s.png" % test_count
3636
bad_page_data = "failure_%s.txt" % test_count
37-
screenshot_path = "%s/%s" % (LATEST_REPORT_DIR, bad_page_image)
37+
screenshot_path = os.path.join(LATEST_REPORT_DIR, bad_page_image)
3838
if hasattr(test, "_last_page_screenshot"):
3939
with open(screenshot_path, "wb") as file:
4040
file.write(test._last_page_screenshot)
@@ -74,7 +74,7 @@ def process_failures(test, test_count, browser_type, duration):
7474

7575
def clear_out_old_report_logs(archive_past_runs=True, get_log_folder=False):
7676
abs_path = os.path.abspath(".")
77-
file_path = abs_path + "/%s" % LATEST_REPORT_DIR
77+
file_path = os.path.join(abs_path, LATEST_REPORT_DIR)
7878
if not os.path.exists(file_path):
7979
try:
8080
os.makedirs(file_path)
@@ -83,12 +83,11 @@ def clear_out_old_report_logs(archive_past_runs=True, get_log_folder=False):
8383

8484
if archive_past_runs:
8585
archive_timestamp = int(time.time())
86-
if not os.path.exists("%s/../%s/" % (file_path, ARCHIVE_DIR)):
87-
os.makedirs("%s/../%s/" % (file_path, ARCHIVE_DIR))
88-
archive_dir = "%s/../%s/report_%s" % (
89-
file_path,
90-
ARCHIVE_DIR,
91-
archive_timestamp,
86+
archive_dir_root = os.path.join(file_path, "..", ARCHIVE_DIR)
87+
if not os.path.exists(archive_dir_root):
88+
os.makedirs(archive_dir_root)
89+
archive_dir = os.path.join(
90+
archive_dir_root, "report_%s" % archive_timestamp
9291
)
9392
shutil.move(file_path, archive_dir)
9493
os.makedirs(file_path)
@@ -98,20 +97,20 @@ def clear_out_old_report_logs(archive_past_runs=True, get_log_folder=False):
9897
# Just delete bad pages to make room for the latest run.
9998
filelist = [
10099
f
101-
for f in os.listdir("./%s" % LATEST_REPORT_DIR)
100+
for f in os.listdir(os.path.join(".", LATEST_REPORT_DIR))
102101
if f.startswith("failure_")
103102
or (f == HTML_REPORT)
104103
or (f.startswith("automation_failure"))
105104
or (f == RESULTS_TABLE)
106105
]
107106
for f in filelist:
108-
os.remove("%s/%s" % (file_path, f))
107+
os.remove(os.path.join(file_path, f))
109108

110109

111110
def add_bad_page_log_file(page_results_list):
112111
abs_path = os.path.abspath(".")
113-
file_path = abs_path + "/%s" % LATEST_REPORT_DIR
114-
log_file = "%s/%s" % (file_path, RESULTS_TABLE)
112+
file_path = os.path.join(abs_path, LATEST_REPORT_DIR)
113+
log_file = os.path.join(file_path, RESULTS_TABLE)
115114
f = open(log_file, "w")
116115
h_p1 = """"Num","Result","Stacktrace","Screenshot","""
117116
h_p2 = """"URL","Browser","Epoch Time","Duration","""
@@ -127,16 +126,16 @@ def archive_new_report_logs():
127126
log_string = clear_out_old_report_logs(get_log_folder=True)
128127
log_folder = log_string.split("/")[-1]
129128
abs_path = os.path.abspath(".")
130-
file_path = abs_path + "/%s" % ARCHIVE_DIR
131-
report_log_path = "%s/%s" % (file_path, log_folder)
129+
file_path = os.path.join(abs_path, ARCHIVE_DIR)
130+
report_log_path = os.path.join(file_path, log_folder)
132131
return report_log_path
133132

134133

135134
def add_results_page(html):
136135
abs_path = os.path.abspath(".")
137-
file_path = abs_path + "/%s" % LATEST_REPORT_DIR
136+
file_path = os.path.join(abs_path, LATEST_REPORT_DIR)
138137
results_file_name = HTML_REPORT
139-
results_file = "%s/%s" % (file_path, results_file_name)
138+
results_file = os.path.join(file_path, results_file_name)
140139
f = open(results_file, "w")
141140
f.write(html)
142141
f.close()
@@ -181,11 +180,10 @@ def build_report(
181180
% summary_table
182181
)
183182

184-
log_link_shown = "../%s%s/" % (
185-
ARCHIVE_DIR,
186-
web_log_path.split(ARCHIVE_DIR)[1],
183+
log_link_shown = os.path.join(
184+
"..", "%s%s" % (ARCHIVE_DIR, web_log_path.split(ARCHIVE_DIR)[1])
187185
)
188-
csv_link = "%s/%s" % (web_log_path, RESULTS_TABLE)
186+
csv_link = os.path.join(web_log_path, RESULTS_TABLE)
189187
csv_link_shown = "%s" % RESULTS_TABLE
190188
log_table = """<p><p><p><p><h2><table><tbody>
191189
<tr><td>LOG FILES LINK:&nbsp;&nbsp;<td><a href="%s">%s</a></tr>

seleniumbase/fixtures/base_case.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,7 +2682,7 @@ def load_html_file(self, html_file, new_page=True):
26822682
if abs_path in html_file:
26832683
file_path = html_file
26842684
else:
2685-
file_path = abs_path + "/%s" % html_file
2685+
file_path = os.path.join(abs_path, html_file)
26862686
html_string = None
26872687
with open(file_path, "r") as f:
26882688
html_string = f.read().strip()
@@ -2703,7 +2703,7 @@ def open_html_file(self, html_file):
27032703
if abs_path in html_file:
27042704
file_path = html_file
27052705
else:
2706-
file_path = abs_path + "/%s" % html_file
2706+
file_path = os.path.join(abs_path, html_file)
27072707
self.open("file://" + file_path)
27082708

27092709
def execute_script(self, script, *args, **kwargs):
@@ -3476,13 +3476,15 @@ def save_cookies(self, name="cookies.txt"):
34763476
raise Exception("Invalid filename for Cookies!")
34773477
if "/" in name:
34783478
name = name.split("/")[-1]
3479+
if "\\" in name:
3480+
name = name.split("\\")[-1]
34793481
if len(name) < 1:
34803482
raise Exception("Filename for Cookies is too short!")
34813483
if not name.endswith(".txt"):
34823484
name = name + ".txt"
34833485
folder = constants.SavedCookies.STORAGE_FOLDER
34843486
abs_path = os.path.abspath(".")
3485-
file_path = abs_path + "/%s" % folder
3487+
file_path = os.path.join(abs_path, folder)
34863488
if not os.path.exists(file_path):
34873489
os.makedirs(file_path)
34883490
cookies_file_path = os.path.join(file_path, name)
@@ -3497,13 +3499,15 @@ def load_cookies(self, name="cookies.txt"):
34973499
raise Exception("Invalid filename for Cookies!")
34983500
if "/" in name:
34993501
name = name.split("/")[-1]
3502+
if "\\" in name:
3503+
name = name.split("\\")[-1]
35003504
if len(name) < 1:
35013505
raise Exception("Filename for Cookies is too short!")
35023506
if not name.endswith(".txt"):
35033507
name = name + ".txt"
35043508
folder = constants.SavedCookies.STORAGE_FOLDER
35053509
abs_path = os.path.abspath(".")
3506-
file_path = abs_path + "/%s" % folder
3510+
file_path = os.path.join(abs_path, folder)
35073511
cookies_file_path = os.path.join(file_path, name)
35083512
json_cookies = None
35093513
with open(cookies_file_path, "r") as f:
@@ -3539,7 +3543,7 @@ def delete_saved_cookies(self, name="cookies.txt"):
35393543
name = name + ".txt"
35403544
folder = constants.SavedCookies.STORAGE_FOLDER
35413545
abs_path = os.path.abspath(".")
3542-
file_path = abs_path + "/%s" % folder
3546+
file_path = os.path.join(abs_path, folder)
35433547
cookies_file_path = os.path.join(file_path, name)
35443548
if os.path.exists(cookies_file_path):
35453549
if cookies_file_path.endswith(".txt"):
@@ -5589,7 +5593,7 @@ def get_pdf_text(
55895593
if self.get_current_url() != pdf:
55905594
self.open(pdf)
55915595
file_name = pdf.split("/")[-1]
5592-
file_path = downloads_folder + "/" + file_name
5596+
file_path = os.path.join(downloads_folder, file_name)
55935597
if not os.path.exists(file_path):
55945598
self.download_file(pdf)
55955599
elif override:
@@ -7964,7 +7968,7 @@ def save_presentation(
79647968
os.makedirs(saved_presentations_folder)
79657969
except Exception:
79667970
pass
7967-
file_path = saved_presentations_folder + "/" + filename
7971+
file_path = os.path.join(saved_presentations_folder, filename)
79687972
out_file = codecs.open(file_path, "w+", encoding="utf-8")
79697973
out_file.writelines(the_html)
79707974
out_file.close()
@@ -8661,7 +8665,7 @@ def save_chart(self, chart_name=None, filename=None, folder=None):
86618665
os.makedirs(saved_charts_folder)
86628666
except Exception:
86638667
pass
8664-
file_path = saved_charts_folder + "/" + filename
8668+
file_path = os.path.join(saved_charts_folder, filename)
86658669
out_file = codecs.open(file_path, "w+", encoding="utf-8")
86668670
out_file.writelines(the_html)
86678671
out_file.close()

seleniumbase/fixtures/page_actions.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,10 +1058,10 @@ def save_screenshot(
10581058
name = name + ".png"
10591059
if folder:
10601060
abs_path = os.path.abspath(".")
1061-
file_path = abs_path + "/%s" % folder
1061+
file_path = os.path.join(abs_path, folder)
10621062
if not os.path.exists(file_path):
10631063
os.makedirs(file_path)
1064-
screenshot_path = "%s/%s" % (file_path, name)
1064+
screenshot_path = os.path.join(file_path, name)
10651065
else:
10661066
screenshot_path = name
10671067
if selector:
@@ -1096,10 +1096,10 @@ def save_page_source(driver, name, folder=None):
10961096
name = name + ".html"
10971097
if folder:
10981098
abs_path = os.path.abspath(".")
1099-
file_path = abs_path + "/%s" % folder
1099+
file_path = os.path.join(abs_path, folder)
11001100
if not os.path.exists(file_path):
11011101
os.makedirs(file_path)
1102-
html_file_path = "%s/%s" % (file_path, name)
1102+
html_file_path = os.path.join(file_path, name)
11031103
else:
11041104
html_file_path = name
11051105
page_source = driver.page_source
@@ -1134,10 +1134,10 @@ def save_test_failure_data(driver, name, browser_type=None, folder=None):
11341134
name = name + ".txt"
11351135
if folder:
11361136
abs_path = os.path.abspath(".")
1137-
file_path = abs_path + "/%s" % folder
1137+
file_path = os.path.join(abs_path, folder)
11381138
if not os.path.exists(file_path):
11391139
os.makedirs(file_path)
1140-
failure_data_file_path = "%s/%s" % (file_path, name)
1140+
failure_data_file_path = os.path.join(file_path, name)
11411141
else:
11421142
failure_data_file_path = name
11431143
failure_data_file = codecs.open(failure_data_file_path, "w+", "utf-8")

seleniumbase/masterqa/master_qa.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,29 @@ def __clear_out_old_logs(
9595

9696
if archive_past_runs:
9797
archive_timestamp = int(time.time())
98-
if not os.path.exists("%s/../%s/" % (file_path, self.ARCHIVE_DIR)):
99-
os.makedirs("%s/../%s/" % (file_path, self.ARCHIVE_DIR))
100-
archive_dir = "%s/../%s/log_%s" % (
101-
file_path,
102-
self.ARCHIVE_DIR,
103-
archive_timestamp,
98+
archive_dir_root = os.path.join(file_path, "..", self.ARCHIVE_DIR)
99+
if not os.path.exists(archive_dir_root):
100+
os.makedirs(archive_dir_root)
101+
archive_dir = os.path.join(
102+
archive_dir_root, "log_%s" % archive_timestamp
104103
)
105104
shutil.move(file_path, archive_dir)
106105
os.makedirs(file_path)
107106
if get_log_folder:
108107
return archive_dir
109108
else:
109+
latest_report_local = os.path.join(".", self.LATEST_REPORT_DIR)
110110
# Just delete bad pages to make room for the latest run.
111111
filelist = [
112112
f
113-
for f in os.listdir("./%s" % self.LATEST_REPORT_DIR)
113+
for f in os.listdir(latest_report_local)
114114
if (f.startswith("failed_"))
115115
or (f == self.RESULTS_PAGE)
116116
or (f.startswith("automation_failure"))
117117
or (f == self.BAD_PAGE_LOG)
118118
]
119119
for f in filelist:
120-
os.remove("%s/%s" % (file_path, f))
120+
os.remove(os.path.join(file_path, f))
121121

122122
def __jq_confirm_dialog(self, question):
123123
count = self.manual_check_count + 1
@@ -364,8 +364,8 @@ def __add_failure(self, exception=None):
364364

365365
def __add_bad_page_log_file(self):
366366
abs_path = os.path.abspath(".")
367-
file_path = abs_path + "/%s" % self.LATEST_REPORT_DIR
368-
log_file = "%s/%s" % (file_path, self.BAD_PAGE_LOG)
367+
file_path = os.path.join(abs_path, self.LATEST_REPORT_DIR)
368+
log_file = os.path.join(file_path, self.BAD_PAGE_LOG)
369369
f = open(log_file, "w")
370370
h_p1 = """"Num","Result","Screenshot","URL","Browser","Epoch Time","""
371371
h_p2 = """"Verification Instructions","Additional Info"\n"""
@@ -377,9 +377,9 @@ def __add_bad_page_log_file(self):
377377

378378
def __add_results_page(self, html):
379379
abs_path = os.path.abspath(".")
380-
file_path = abs_path + "/%s" % self.LATEST_REPORT_DIR
380+
file_path = os.path.join(abs_path, self.LATEST_REPORT_DIR)
381381
results_file_name = self.RESULTS_PAGE
382-
results_file = "%s/%s" % (file_path, results_file_name)
382+
results_file = os.path.join(file_path, results_file_name)
383383
f = open(results_file, "w")
384384
f.write(html)
385385
f.close()
@@ -394,7 +394,7 @@ def __process_manual_check_results(self, auto_close_results_page=False):
394394
if self.manual_check_successes == self.manual_check_count:
395395
pass
396396
else:
397-
print("WARNING: There were page issues detected!")
397+
print("WARNING: Not all tests passed manual inspection!")
398398
perfection = False
399399

400400
if self.incomplete_runs > 0:
@@ -414,7 +414,7 @@ def __process_manual_check_results(self, auto_close_results_page=False):
414414
log_folder = log_string.split("/")[-1]
415415
abs_path = os.path.abspath(".")
416416
file_path = abs_path + "/%s" % self.ARCHIVE_DIR
417-
log_path = "%s/%s" % (file_path, log_folder)
417+
log_path = os.path.join(file_path, log_folder)
418418
web_log_path = "file://%s" % log_path
419419

420420
tf_color = "#11BB11"
@@ -448,11 +448,12 @@ def __process_manual_check_results(self, auto_close_results_page=False):
448448
% summary_table
449449
)
450450

451-
log_link_shown = "../%s%s/" % (
452-
self.ARCHIVE_DIR,
453-
web_log_path.split(self.ARCHIVE_DIR)[1],
451+
log_link_shown = os.path.join(
452+
"..", "%s%s" % (
453+
self.ARCHIVE_DIR, web_log_path.split(self.ARCHIVE_DIR)[1]
454+
)
454455
)
455-
csv_link = "%s/%s" % (web_log_path, self.BAD_PAGE_LOG)
456+
csv_link = os.path.join(web_log_path, self.BAD_PAGE_LOG)
456457
csv_link_shown = "%s" % self.BAD_PAGE_LOG
457458
log_table = """<p><p><p><p><h2><table><tbody>
458459
<tr><td>LOG FILES LINK:&nbsp;&nbsp;<td><a href="%s">%s</a></tr>

0 commit comments

Comments
 (0)