Skip to content

Commit 3ec44f3

Browse files
committed
New script "sbase mkrec FILE.py" to make a recording
1 parent 386bfea commit 3ec44f3

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

seleniumbase/console_scripts/run.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,24 @@ def show_mkfile_usage():
193193
print("")
194194

195195

196+
def show_mkrec_usage():
197+
c2 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
198+
c3 = colorama.Fore.BLUE + colorama.Back.LIGHTYELLOW_EX
199+
cr = colorama.Style.RESET_ALL
200+
sc = " " + c2 + "** " + c3 + "mkrec" + c2 + " **" + cr
201+
print(sc)
202+
print("")
203+
print(" Usage:")
204+
print(" seleniumbase mkrec [FILE.py]")
205+
print(" OR: sbase mkrec [FILE.py]")
206+
print(" Example:")
207+
print(" sbase mkrec new_test.py")
208+
print(" Output:")
209+
print(" Creates a new SeleniumBase test using the Recorder.")
210+
print(" If the filename already exists, an error is raised.")
211+
print("")
212+
213+
196214
def show_mkpres_usage():
197215
c2 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
198216
c3 = colorama.Fore.BLUE + colorama.Back.LIGHTYELLOW_EX
@@ -680,6 +698,7 @@ def show_detailed_help():
680698
show_install_usage()
681699
show_mkdir_usage()
682700
show_mkfile_usage()
701+
show_mkrec_usage()
683702
show_mkpres_usage()
684703
show_mkchart_usage()
685704
show_convert_usage()
@@ -736,6 +755,14 @@ def main():
736755
else:
737756
show_basic_usage()
738757
show_mkfile_usage()
758+
elif command == "mkrec":
759+
if len(command_args) >= 1:
760+
from seleniumbase.console_scripts import sb_mkrec
761+
762+
sb_mkrec.main()
763+
else:
764+
show_basic_usage()
765+
show_mkrec_usage()
739766
elif command == "mkpres":
740767
if len(command_args) >= 1:
741768
from seleniumbase.console_scripts import sb_mkpres
@@ -887,6 +914,10 @@ def main():
887914
print("")
888915
show_mkfile_usage()
889916
return
917+
elif command_args[0] == "mkrec":
918+
print("")
919+
show_mkrec_usage()
920+
return
890921
elif command_args[0] == "mkpres":
891922
print("")
892923
show_mkpres_usage()
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Comments
 (0)