|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Creates a new SeleniumBase test file using the Recorder. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + seleniumbase mkrec [FILE.py] |
| 7 | + or sbase mkrec [FILE.py] |
| 8 | +
|
| 9 | +Example: |
| 10 | + sbase mkrec new_test.py |
| 11 | +
|
| 12 | +Output: |
| 13 | + Creates a new SeleniumBase test using the Recorder. |
| 14 | + If the filename already exists, an error is raised. |
| 15 | +""" |
| 16 | + |
| 17 | +import codecs |
| 18 | +import colorama |
| 19 | +import shutil |
| 20 | +import os |
| 21 | +import sys |
| 22 | + |
| 23 | + |
| 24 | +def invalid_run_command(msg=None): |
| 25 | + exp = " ** mkrec **\n\n" |
| 26 | + exp += " Usage:\n" |
| 27 | + exp += " seleniumbase mkrec [FILE.py]\n" |
| 28 | + exp += " OR sbase mkrec [FILE.py]\n" |
| 29 | + exp += " Example:\n" |
| 30 | + exp += " sbase mkrec new_test.py\n" |
| 31 | + exp += " Output:\n" |
| 32 | + exp += " Creates a new SeleniumBase test using the Recorder.\n" |
| 33 | + exp += " If the filename already exists, an error is raised.\n" |
| 34 | + if not msg: |
| 35 | + raise Exception("INVALID RUN COMMAND!\n\n%s" % exp) |
| 36 | + elif msg == "help": |
| 37 | + print("\n%s" % exp) |
| 38 | + sys.exit() |
| 39 | + else: |
| 40 | + raise Exception("INVALID RUN COMMAND!\n\n%s\n%s\n" % (exp, msg)) |
| 41 | + |
| 42 | + |
| 43 | +def main(): |
| 44 | + c0 = "" |
| 45 | + c1 = "" |
| 46 | + c2 = "" |
| 47 | + c5 = "" |
| 48 | + c7 = "" |
| 49 | + cr = "" |
| 50 | + if "linux" not in sys.platform: |
| 51 | + colorama.init(autoreset=True) |
| 52 | + c0 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX |
| 53 | + c1 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX |
| 54 | + c2 = colorama.Fore.LIGHTRED_EX + colorama.Back.LIGHTYELLOW_EX |
| 55 | + c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX |
| 56 | + c7 = colorama.Fore.BLACK + colorama.Back.MAGENTA |
| 57 | + cr = colorama.Style.RESET_ALL |
| 58 | + |
| 59 | + help_me = False |
| 60 | + error_msg = None |
| 61 | + invalid_cmd = None |
| 62 | + |
| 63 | + command_args = sys.argv[2:] |
| 64 | + file_name = command_args[0] |
| 65 | + if file_name == "-h" or file_name == "--help": |
| 66 | + invalid_run_command("help") |
| 67 | + elif not file_name.endswith(".py"): |
| 68 | + error_msg = 'File name must end with ".py"!' |
| 69 | + elif "*" in file_name or len(str(file_name)) < 4: |
| 70 | + error_msg = "Invalid file name!" |
| 71 | + elif file_name.startswith("-"): |
| 72 | + error_msg = 'File name cannot start with "-"!' |
| 73 | + elif "/" in str(file_name) or "\\" in str(file_name): |
| 74 | + error_msg = "File must be created in the current directory!" |
| 75 | + elif os.path.exists(os.getcwd() + "/" + file_name): |
| 76 | + error_msg = 'File "%s" already exists in this directory!' % file_name |
| 77 | + if error_msg: |
| 78 | + error_msg = c5 + "ERROR: " + error_msg + cr |
| 79 | + invalid_run_command(error_msg) |
| 80 | + |
| 81 | + if len(command_args) >= 2: |
| 82 | + options = command_args[1:] |
| 83 | + for option in options: |
| 84 | + option = option.lower() |
| 85 | + if option == "-h" or option == "--help": |
| 86 | + help_me = True |
| 87 | + else: |
| 88 | + invalid_cmd = "\n===> INVALID OPTION: >> %s <<\n" % option |
| 89 | + invalid_cmd = invalid_cmd.replace(">> ", ">>" + c5 + " ") |
| 90 | + invalid_cmd = invalid_cmd.replace(" <<", " " + cr + "<<") |
| 91 | + invalid_cmd = invalid_cmd.replace(">>", c7 + ">>" + cr) |
| 92 | + invalid_cmd = invalid_cmd.replace("<<", c7 + "<<" + cr) |
| 93 | + help_me = True |
| 94 | + break |
| 95 | + if help_me: |
| 96 | + invalid_run_command(invalid_cmd) |
| 97 | + |
| 98 | + dir_name = os.getcwd() |
| 99 | + file_path = "%s/%s" % (dir_name, file_name) |
| 100 | + |
| 101 | + data = [] |
| 102 | + data.append("from seleniumbase import BaseCase") |
| 103 | + data.append("") |
| 104 | + data.append("") |
| 105 | + data.append("class RecorderTests(BaseCase):") |
| 106 | + data.append(" def test_recording(self):") |
| 107 | + data.append(' if self.recorder_ext and not self.xvfb:') |
| 108 | + data.append(' import ipdb; ipdb.set_trace()') |
| 109 | + data.append("") |
| 110 | + file = codecs.open(file_path, "w+", "utf-8") |
| 111 | + file.writelines("\r\n".join(data)) |
| 112 | + file.close() |
| 113 | + success = ( |
| 114 | + "\n" + c0 + '* RECORDING initialized:' + cr + " " |
| 115 | + "" + c1 + file_name + "" + cr + "\n" |
| 116 | + ) |
| 117 | + print(success) |
| 118 | + print("pytest %s --rec -q -s" % file_name) |
| 119 | + os.system("pytest %s --rec -q -s" % file_name) |
| 120 | + if os.path.exists(file_path): |
| 121 | + os.remove(file_path) |
| 122 | + recorded_filename = file_name[:-3] + "_rec.py" |
| 123 | + recordings_dir = os.path.join(dir_name, "recordings") |
| 124 | + recorded_file = os.path.join(recordings_dir, recorded_filename) |
| 125 | + os.system("sbase print %s -n" % recorded_file) |
| 126 | + shutil.copy(recorded_file, file_path) |
| 127 | + success = ( |
| 128 | + "\n" + c2 + "***" + cr + " RECORDING COPIED to: " |
| 129 | + "" + c1 + file_name + cr + "\n" |
| 130 | + ) |
| 131 | + print(success) |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + invalid_run_command() |
0 commit comments