Skip to content

Commit eaabc9c

Browse files
authored
Merge pull request #1010 from seleniumbase/sbase-mkrec-command
New script "sbase mkrec FILE.py" to make a recording
2 parents 386bfea + 774c753 commit eaabc9c

File tree

11 files changed

+194
-13
lines changed

11 files changed

+194
-13
lines changed
File renamed without changes.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title>SeleniumBase</title>
66
</head>
77
<body>
8-
<p<a href="https://github.com/seleniumbase/SeleniumBase/"><img src="https://seleniumbase.io/img/sb_logo_10.png" alt="SeleniumBase" width="275" /></a></p>
8+
<p><a href="https://github.com/seleniumbase/SeleniumBase/"><img src="https://seleniumbase.io/img/sb_logo_10.png" alt="SeleniumBase" width="275" /></a></p>
99
<h1><img src="https://seleniumbase.io/img/logo6.png" title="SeleniumBase" width="32" /> Docs Home Page:</h1>
1010

1111
<h2><img src="https://seleniumbase.io/img/logo6.png" title="SeleniumBase" width="32" /> <a href="https://seleniumbase.io">https://seleniumbase.io</a></h2>

docs/prepare.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
""" For preparing the mkdocs-generated seleniumbase.io website. """
2+
13
import codecs
24
import os
35
import re

docs/requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ python-dateutil==2.8.2
1010
keyring==23.2.1;python_version>="3.6"
1111
pkginfo==1.7.1;python_version>="3.6"
1212
Jinja2==3.0.2;python_version>="3.6"
13-
click==8.0.2;python_version>="3.6"
13+
click==8.0.3;python_version>="3.6"
1414
zipp==3.6.0;python_version>="3.6"
1515
readme-renderer==30.0
1616
pymdown-extensions==9.0;python_version>="3.6"
1717
importlib-metadata==4.8.1;python_version>="3.6"
1818
bleach==4.1.0
1919
lunr==0.6.0;python_version>="3.6"
20-
nltk==3.6.4;python_version>="3.6"
20+
nltk==3.6.5;python_version>="3.6"
2121
watchdog==2.1.6;python_version>="3.6"
2222
mkdocs==1.2.2;python_version>="3.6"
23-
mkdocs-material==7.3.2;python_version>="3.6"
23+
mkdocs-material==7.3.3;python_version>="3.6"
2424
mkdocs-exclude-search==0.5.2;python_version>="3.6"
2525
mkdocs-simple-hooks==0.1.3
2626
mkdocs-material-extensions==1.0.3;python_version>="3.6"

help_docs/recorder_mode.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class RecorderTest(BaseCase):
8282

8383
<p>🔴 SeleniumBase <code>1.66.10</code> adds better error-handling for the Recorder. It also adds the console script option <code>-r</code> for <code>sbase mkfile</code> to generate a new test file with a breakpoint for Recorder Mode: <code>sbase mkfile NEW_FILE.py -r</code></p>
8484

85+
<p>🔴 SeleniumBase <code>1.66.12</code> adds the ability to instantly create a new test recording by running <code>sbase mkrec FILE.py</code>. Once the browser spins up, you can open a new web page and start performing actions that will get recorded and saved to the file you specified.</p>
86+
8587
--------
8688

8789
<div>To learn more about SeleniumBase, check out the Docs Site:</div>

requirements.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pip>=20.3.4;python_version<"3.6"
2-
pip>=21.2.4;python_version>="3.6"
2+
pip>=21.3;python_version>="3.6"
33
packaging>=20.9;python_version<"3.6"
44
packaging>=21.0;python_version>="3.6"
55
typing-extensions>=3.10.0.2
@@ -115,8 +115,11 @@ coverage==6.0.1;python_version>="3.6"
115115
pytest-cov==2.12.1;python_version<"3.6"
116116
pytest-cov==3.0.0;python_version>="3.6"
117117
flake8==3.7.9;python_version<"3.5"
118-
flake8==3.9.2;python_version>="3.5"
118+
flake8==3.9.2;python_version>="3.5" and python_version<"3.6"
119+
flake8==4.0.1;python_version>="3.6"
119120
pyflakes==2.1.1;python_version<"3.5"
120-
pyflakes==2.3.1;python_version>="3.5"
121+
pyflakes==2.3.1;python_version>="3.5" and python_version<"3.6"
122+
pyflakes==2.4.0;python_version>="3.6"
121123
pycodestyle==2.5.0;python_version<"3.5"
122-
pycodestyle==2.7.0;python_version>="3.5"
124+
pycodestyle==2.7.0;python_version>="3.5" and python_version<"3.6"
125+
pycodestyle==2.8.0;python_version>="3.6"

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "1.66.11"
2+
__version__ = "1.66.12"

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()

seleniumbase/console_scripts/sb_mkdir.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,8 @@ def main():
621621
file.close()
622622

623623
data = []
624+
data.append('""" Classic Page Object Model with BaseCase inheritance """')
625+
data.append("")
624626
data.append("from seleniumbase import BaseCase")
625627
data.append("")
626628
data.append("")
@@ -648,6 +650,9 @@ def main():
648650
file.close()
649651

650652
data = []
653+
data.append('""" Classic Page Object Model with the "sb" fixture """')
654+
data.append("")
655+
data.append("")
651656
data.append("class LoginPage:")
652657
data.append(" def login_to_swag_labs(self, sb, username):")
653658
data.append(' sb.open("https://www.saucedemo.com/")')
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)