-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathsb_mkrec.py
More file actions
348 lines (326 loc) · 12.5 KB
/
sb_mkrec.py
File metadata and controls
348 lines (326 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
** mkrec / record / codegen **
Creates a new SeleniumBase test file using the Recorder.
Usage:
seleniumbase mkrec [FILE.py] [OPTIONS]
sbase mkrec [FILE.py] [OPTIONS]
seleniumbase record [FILE.py] [OPTIONS]
sbase record [FILE.py] [OPTIONS]
seleniumbase codegen [FILE.py] [OPTIONS]
sbase codegen [FILE.py] [OPTIONS]
Examples:
sbase mkrec new_test.py
sbase mkrec new_test.py --url=seleniumbase.io
sbase codegen new_test.py
sbase codegen new_test.py --url=wikipedia.org
Options:
--url=URL (Sets the initial start page URL.)
--edge (Use Edge browser instead of Chrome.)
--gui / --headed (Use headed mode on Linux.)
--uc / --undetected (Use undetectable mode.)
--overwrite (Overwrite file when it exists.)
--behave (Also output Behave/Gherkin files.)
Output:
Creates a new SeleniumBase test using the Recorder.
If the filename already exists, an error is raised.
"""
import colorama
import shutil
import os
import sys
def invalid_run_command(msg=None):
exp = " ** mkrec / record / codegen **\n\n"
exp += " Usage:\n"
exp += " seleniumbase mkrec [FILE.py]\n"
exp += " OR: sbase mkrec [FILE.py]\n"
exp += " Examples:\n"
exp += " sbase mkrec 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 += " --uc / --undetected (Use undetectable mode.)\n"
exp += " --overwrite (Overwrite file when it exists.)\n"
exp += " --behave (Also output Behave/Gherkin files.)\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"
if not msg:
raise Exception("INVALID RUN COMMAND!\n\n%s" % exp)
elif msg == "help":
print("\n%s" % exp)
sys.exit()
else:
raise Exception("INVALID RUN COMMAND!\n\n%s\n%s\n" % (exp, msg))
def set_colors(use_colors):
c0 = ""
c1 = ""
c2 = ""
c5 = ""
c7 = ""
cr = ""
if use_colors:
c0 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
c1 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX
c2 = colorama.Fore.LIGHTRED_EX + colorama.Back.LIGHTYELLOW_EX
c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX
c7 = colorama.Fore.BLACK + colorama.Back.MAGENTA
cr = colorama.Style.RESET_ALL
return c0, c1, c2, c5, c7, cr
def main():
help_me = False
error_msg = None
invalid_cmd = None
use_edge = False
use_opera = False
use_brave = False
use_comet = False
use_atlas = False
use_chromium = False
use_uc = False
esc_end = False
start_page = None
next_is_url = False
use_colors = True
force_gui = False
rec_behave = False
sys_executable = sys.executable
if " " in sys_executable:
sys_executable = "python"
if "linux" in sys.platform:
use_colors = False
c0, c1, c2, c5, c7, cr = set_colors(use_colors)
command_args = sys.argv[2:]
file_name = command_args[0]
if file_name == "-h" or file_name == "--help":
invalid_run_command("help")
elif not file_name.endswith(".py"):
error_msg = 'File name must end with ".py"!'
elif "*" in file_name or len(str(file_name)) < 4:
error_msg = "Invalid file name!"
elif file_name.startswith("-"):
error_msg = 'File name cannot start with "-"!'
elif "/" in str(file_name) or "\\" in str(file_name):
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!'
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:
if option.lower() == "-h" or option.lower() == "--help":
help_me = True
elif option.lower() == "--edge":
use_edge = True
elif option.lower() == "--opera":
use_opera = True
elif option.lower() == "--brave":
use_brave = True
elif option.lower() == "--comet":
use_comet = True
elif option.lower() == "--atlas":
use_atlas = True
elif option.lower() == "--use-chromium":
use_chromium = True
elif option.lower() == "--ee":
esc_end = True
elif option.lower() in ("--gui", "--headed"):
if "linux" in sys.platform:
force_gui = True
elif option.lower() in (
"--uc", "--cdp", "--undetected", "--undetectable"
):
use_uc = True
elif option.lower() in ("--rec-behave", "--behave", "--gherkin"):
rec_behave = True
elif option.lower().startswith("--url="):
start_page = option[len("--url="):]
elif option.lower() == "--url":
next_is_url = True
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 + " ")
invalid_cmd = invalid_cmd.replace(" <<", " " + cr + "<<")
invalid_cmd = invalid_cmd.replace(">>", c7 + ">>" + cr)
invalid_cmd = invalid_cmd.replace("<<", c7 + "<<" + cr)
help_me = True
break
if help_me:
invalid_run_command(invalid_cmd)
data = []
data.append("from seleniumbase import BaseCase")
data.append("")
data.append("")
data.append("class RecorderTest(BaseCase):")
data.append(" def test_recording(self):")
if use_uc:
data.append(" if self.undetectable:")
if (
start_page
and (
start_page.startswith("http:")
or start_page.startswith("https:")
or start_page.startswith("file:")
)
):
used_sp = start_page
if '"' not in start_page:
used_sp = '"%s"' % start_page
elif "'" not in start_page:
used_sp = "'%s'" % start_page
data.append(
" self.activate_cdp_mode(\n"
" %s,\n"
" recorder=True,\n"
" )" % used_sp
)
else:
data.append(" self.disconnect()")
data.append(" if self.recorder_ext:")
data.append(" # When done recording actions,")
data.append(' # type "c", and press [Enter].')
data.append(" breakpoint()")
data.append("")
if esc_end:
msg = ">>> Use [SHIFT + ESC] in the browser to end recording!"
d2 = []
d2.append("from seleniumbase import BaseCase")
d2.append("")
d2.append("")
d2.append("class RecorderTest(BaseCase):")
d2.append(" def test_recording(self):")
d2.append(" if self.recorder_ext:")
d2.append(" print(")
d2.append(' "\\n\\n%s\\n"' % msg)
d2.append(" )")
d2.append(' script = self._get_rec_shift_esc_script()')
d2.append(' esc = "return document.sb_esc_end;"')
d2.append(" start_time = self.time()")
d2.append(" last_handles_num = self._get_num_handles()")
d2.append(" for i in range(1200):")
d2.append(" try:")
d2.append(" self.execute_script(script)")
d2.append(" handles_num = self._get_num_handles()")
d2.append(" if handles_num < 1:")
d2.append(" return")
d2.append(" elif handles_num != last_handles_num:")
d2.append(" self.switch_to_window(-1)")
d2.append(" last_handles_num = handles_num")
d2.append(' if self.execute_script(esc) == "yes":')
d2.append(" return")
d2.append(" elif self.time() - start_time > 600:")
d2.append(" return")
d2.append(" self.sleep(0.5)")
d2.append(" except Exception:")
d2.append(" return")
d2.append("")
data = d2
file = open(file_path, mode="w+", encoding="utf-8")
file.writelines("\r\n".join(data))
file.close()
success = (
"\n" + c0 + "* RECORDING initialized:" + cr + " "
"" + c1 + file_name + "" + cr + "\n"
)
print(success)
run_cmd = None
if (
not start_page
or (
use_uc
and (
start_page.startswith("http:")
or start_page.startswith("https:")
or start_page.startswith("file:")
)
and not esc_end
)
):
run_cmd = "%s -m pytest %s --rec -q -s" % (sys_executable, file_name)
else:
run_cmd = "%s -m pytest %s --rec -q -s --url=%s" % (
sys_executable, file_name, start_page
)
if '"' not in start_page:
run_cmd = '%s -m pytest %s --rec -q -s --url="%s"' % (
sys_executable, file_name, start_page
)
elif "'" not in start_page:
run_cmd = "%s -m pytest %s --rec -q -s --url='%s'" % (
sys_executable, file_name, start_page
)
if use_edge:
run_cmd += " --edge"
elif use_opera:
run_cmd += " --opera"
elif use_brave:
run_cmd += " --brave"
elif use_comet:
run_cmd += " --comet"
elif use_atlas:
run_cmd += " --atlas"
elif use_chromium:
run_cmd += " --use-chromium"
if force_gui:
run_cmd += " --gui"
if use_uc:
run_cmd += " --uc"
if rec_behave:
run_cmd += " --rec-behave"
print(run_cmd)
os.system(run_cmd)
if os.path.exists(file_path):
os.remove(file_path)
recorded_filename = file_name[:-3] + "_rec.py"
recordings_dir = os.path.join(dir_name, "recordings")
recorded_file = os.path.join(recordings_dir, recorded_filename)
prefix = "%s -m " % sys_executable
if " " not in recorded_file:
os.system("%sseleniumbase print %s -n" % (prefix, recorded_file))
elif '"' not in recorded_file:
os.system('%sseleniumbase print "%s" -n' % (prefix, recorded_file))
else:
os.system("%sseleniumbase print '%s' -n" % (prefix, recorded_file))
shutil.copy(recorded_file, file_path)
success = (
"\n" + c2 + "***" + cr + " RECORDING COPIED to: "
"" + c1 + file_name + cr + "\n"
)
print(success)
if rec_behave:
recorded_filename = file_name[:-3] + "_rec.feature"
recordings_dir = os.path.join(dir_name, "recordings")
features_dir = os.path.join(recordings_dir, "features")
recorded_file = os.path.join(features_dir, recorded_filename)
if " " not in recorded_file:
os.system("%sseleniumbase print %s -n" % (prefix, recorded_file))
elif '"' not in recorded_file:
os.system('%sseleniumbase print "%s" -n' % (prefix, recorded_file))
else:
os.system("%sseleniumbase print '%s' -n" % (prefix, recorded_file))
success = (
"\n" + c2 + "***" + cr + " BEHAVE RECORDING at: "
"" + c1 + os.path.relpath(recorded_file) + cr + "\n"
)
print(success)
if __name__ == "__main__":
invalid_run_command()