Skip to content

Commit 781d2f6

Browse files
committed
Add "--overwrite" option when calling "sbase mkrec FILE"
1 parent a32e221 commit 781d2f6

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

seleniumbase/console_scripts/ReadMe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ is included.
240240
``--url=URL`` (Sets the initial start page URL.)
241241
``--edge`` (Use Edge browser instead of Chrome.)
242242
``--gui`` / ``--headed`` (Use headed mode on Linux.)
243+
``--overwrite`` (Overwrite file when it exists.)
243244

244245
* Output:
245246
Creates a new SeleniumBase test using the Recorder.

seleniumbase/console_scripts/run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def show_mkrec_usage():
217217
print(" --url=URL (Sets the initial start page URL.)")
218218
print(" --edge (Use Edge browser instead of Chrome.)")
219219
print(" --gui / --headed (Use headed mode on Linux.)")
220+
print(" --overwrite (Overwrite file when it exists.)")
220221
print(" Output:")
221222
print(" Creates a new SeleniumBase test using the Recorder.")
222223
print(" If the filename already exists, an error is raised.")
@@ -240,6 +241,7 @@ def show_codegen_usage():
240241
print(" --url=URL (Sets the initial start page URL.)")
241242
print(" --edge (Use Edge browser instead of Chrome.)")
242243
print(" --gui / --headed (Use headed mode on Linux.)")
244+
print(" --overwrite (Overwrite file when it exists.)")
243245
print(" Output:")
244246
print(" Creates a new SeleniumBase test using the Recorder.")
245247
print(" If the filename already exists, an error is raised.")

seleniumbase/console_scripts/sb_mkrec.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
--url=URL (Sets the initial start page URL.)
2323
--edge (Use Edge browser instead of Chrome.)
2424
--gui / --headed (Use headed mode on Linux.)
25+
--overwrite (Overwrite file when it exists.)
2526
2627
Output:
2728
Creates a new SeleniumBase test using the Recorder.
@@ -36,19 +37,18 @@
3637

3738

3839
def invalid_run_command(msg=None):
39-
exp = " ** mkrec / codegen **\n\n"
40+
exp = " ** mkrec / record / codegen **\n\n"
4041
exp += " Usage:\n"
4142
exp += " seleniumbase mkrec [FILE.py]\n"
42-
exp += " sbase mkrec [FILE.py]\n"
43-
exp += " seleniumbase codegen [FILE.py]\n"
44-
exp += " sbase codegen [FILE.py]\n"
43+
exp += " OR: sbase mkrec [FILE.py]\n"
4544
exp += " Examples:\n"
4645
exp += " sbase mkrec new_test.py\n"
47-
exp += " sbase codegen new_test.py\n"
46+
exp += " sbase mkrec new_test.py --url=wikipedia.org\n"
4847
exp += " Options:\n"
4948
exp += " --url=URL (Sets the initial start page URL.)\n"
5049
exp += " --edge (Use Edge browser instead of Chrome.)\n"
5150
exp += " --gui / --headed (Use headed mode on Linux.)\n"
51+
exp += " --overwrite (Overwrite file when it exists.)\n"
5252
exp += " Output:\n"
5353
exp += " Creates a new SeleniumBase test using the Recorder.\n"
5454
exp += " If the filename already exists, an error is raised.\n"
@@ -108,12 +108,23 @@ def main():
108108
error_msg = "File must be created in the current directory!"
109109
elif file_name == "abc.py":
110110
error_msg = '"abc.py" is a reserved Python module! Use another name!'
111-
elif os.path.exists(os.getcwd() + "/" + file_name):
112-
error_msg = 'File "%s" already exists in this directory!' % file_name
113111
if error_msg:
114112
error_msg = c5 + "ERROR: " + error_msg + cr
115113
invalid_run_command(error_msg)
116114

115+
dir_name = os.getcwd()
116+
file_path = os.path.join(dir_name, file_name)
117+
118+
if (
119+
"--overwrite" in ' '.join(command_args).lower()
120+
and os.path.exists(file_path)
121+
):
122+
os.remove(file_path)
123+
if os.path.exists(file_path):
124+
error_msg = 'File "%s" already exists in this directory!' % file_name
125+
error_msg = c5 + "ERROR: " + error_msg + cr
126+
invalid_run_command(error_msg)
127+
117128
if len(command_args) >= 2:
118129
options = command_args[1:]
119130
for option in options:
@@ -133,6 +144,8 @@ def main():
133144
elif next_is_url:
134145
start_page = option
135146
next_is_url = False
147+
elif option.lower() == "--overwrite":
148+
pass # Already handled if file existed
136149
else:
137150
invalid_cmd = "\n===> INVALID OPTION: >> %s <<\n" % option
138151
invalid_cmd = invalid_cmd.replace(">> ", ">>" + c5 + " ")
@@ -144,9 +157,6 @@ def main():
144157
if help_me:
145158
invalid_run_command(invalid_cmd)
146159

147-
dir_name = os.getcwd()
148-
file_path = "%s/%s" % (dir_name, file_name)
149-
150160
data = []
151161
data.append("from seleniumbase import BaseCase")
152162
data.append("")

0 commit comments

Comments
 (0)