Add "--overwrite" option when calling "sbase mkrec FILE"

This commit is contained in:
Michael Mintz 2022-03-15 21:22:53 -04:00
parent a32e2213bf
commit 781d2f6e39
3 changed files with 23 additions and 10 deletions

View File

@ -240,6 +240,7 @@ is included.
``--url=URL`` (Sets the initial start page URL.)
``--edge`` (Use Edge browser instead of Chrome.)
``--gui`` / ``--headed`` (Use headed mode on Linux.)
``--overwrite`` (Overwrite file when it exists.)
* Output:
Creates a new SeleniumBase test using the Recorder.

View File

@ -217,6 +217,7 @@ def show_mkrec_usage():
print(" --url=URL (Sets the initial start page URL.)")
print(" --edge (Use Edge browser instead of Chrome.)")
print(" --gui / --headed (Use headed mode on Linux.)")
print(" --overwrite (Overwrite file when it exists.)")
print(" Output:")
print(" Creates a new SeleniumBase test using the Recorder.")
print(" If the filename already exists, an error is raised.")
@ -240,6 +241,7 @@ def show_codegen_usage():
print(" --url=URL (Sets the initial start page URL.)")
print(" --edge (Use Edge browser instead of Chrome.)")
print(" --gui / --headed (Use headed mode on Linux.)")
print(" --overwrite (Overwrite file when it exists.)")
print(" Output:")
print(" Creates a new SeleniumBase test using the Recorder.")
print(" If the filename already exists, an error is raised.")

View File

@ -22,6 +22,7 @@ Options:
--url=URL (Sets the initial start page URL.)
--edge (Use Edge browser instead of Chrome.)
--gui / --headed (Use headed mode on Linux.)
--overwrite (Overwrite file when it exists.)
Output:
Creates a new SeleniumBase test using the Recorder.
@ -36,19 +37,18 @@ import sys
def invalid_run_command(msg=None):
exp = " ** mkrec / codegen **\n\n"
exp = " ** mkrec / record / codegen **\n\n"
exp += " Usage:\n"
exp += " seleniumbase mkrec [FILE.py]\n"
exp += " sbase mkrec [FILE.py]\n"
exp += " seleniumbase codegen [FILE.py]\n"
exp += " sbase codegen [FILE.py]\n"
exp += " OR: sbase mkrec [FILE.py]\n"
exp += " Examples:\n"
exp += " sbase mkrec new_test.py\n"
exp += " sbase codegen new_test.py\n"
exp += " sbase mkrec new_test.py --url=wikipedia.org\n"
exp += " Options:\n"
exp += " --url=URL (Sets the initial start page URL.)\n"
exp += " --edge (Use Edge browser instead of Chrome.)\n"
exp += " --gui / --headed (Use headed mode on Linux.)\n"
exp += " --overwrite (Overwrite file when it exists.)\n"
exp += " Output:\n"
exp += " Creates a new SeleniumBase test using the Recorder.\n"
exp += " If the filename already exists, an error is raised.\n"
@ -108,12 +108,23 @@ def main():
error_msg = "File must be created in the current directory!"
elif file_name == "abc.py":
error_msg = '"abc.py" is a reserved Python module! Use another name!'
elif os.path.exists(os.getcwd() + "/" + file_name):
error_msg = 'File "%s" already exists in this directory!' % file_name
if error_msg:
error_msg = c5 + "ERROR: " + error_msg + cr
invalid_run_command(error_msg)
dir_name = os.getcwd()
file_path = os.path.join(dir_name, file_name)
if (
"--overwrite" in ' '.join(command_args).lower()
and os.path.exists(file_path)
):
os.remove(file_path)
if os.path.exists(file_path):
error_msg = 'File "%s" already exists in this directory!' % file_name
error_msg = c5 + "ERROR: " + error_msg + cr
invalid_run_command(error_msg)
if len(command_args) >= 2:
options = command_args[1:]
for option in options:
@ -133,6 +144,8 @@ def main():
elif next_is_url:
start_page = option
next_is_url = False
elif option.lower() == "--overwrite":
pass # Already handled if file existed
else:
invalid_cmd = "\n===> INVALID OPTION: >> %s <<\n" % option
invalid_cmd = invalid_cmd.replace(">> ", ">>" + c5 + " ")
@ -144,9 +157,6 @@ def main():
if help_me:
invalid_run_command(invalid_cmd)
dir_name = os.getcwd()
file_path = "%s/%s" % (dir_name, file_name)
data = []
data.append("from seleniumbase import BaseCase")
data.append("")