-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathsb_recorder.py
More file actions
394 lines (361 loc) · 11.8 KB
/
Copy pathsb_recorder.py
File metadata and controls
394 lines (361 loc) · 11.8 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""
** recorder **
Launches the SeleniumBase Recorder Desktop App.
Usage:
seleniumbase recorder [OPTIONS]
sbase recorder [OPTIONS]
Options:
--uc / --undetected (Use undetectable mode.)
--cdp (Same as "--uc" and "--undetectable".)
--behave (Also output Behave/Gherkin files.)
Output:
Launches the SeleniumBase Recorder Desktop App.
"""
import colorama
import os
import subprocess
import sys
import tkinter as tk
from seleniumbase import config as sb_config
from seleniumbase.fixtures import page_utils
from seleniumbase.fixtures import shared_utils
from tkinter import messagebox
sb_config.rec_subprocess_p = None
sb_config.rec_subprocess_used = False
sys_executable = sys.executable
if " " in sys_executable:
sys_executable = "python"
def set_colors(use_colors):
c0 = ""
c1 = ""
c2 = ""
c3 = ""
c4 = ""
cr = ""
if use_colors:
c0 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
c2 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX
c3 = colorama.Fore.LIGHTRED_EX + colorama.Back.LIGHTYELLOW_EX
c4 = colorama.Fore.BLUE + colorama.Back.LIGHTYELLOW_EX
cr = colorama.Style.RESET_ALL
return c0, c1, c2, c3, c4, cr
def send_window_to_front(window):
window.lift()
window.attributes("-topmost", True)
window.after_idle(window.attributes, "-topmost", False)
def show_already_recording_warning():
messagebox.showwarning(
"SeleniumBase Recorder: Already Running!",
"Please finalize the active recording from the terminal\n"
'where you opened the Recorder: Type "c" and hit Enter.',
)
def file_name_error(file_name):
error_msg = None
if 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!"
return error_msg
def do_recording(file_name, url, overwrite_enabled, use_chrome, window):
poll = None
if sb_config.rec_subprocess_used:
poll = sb_config.rec_subprocess_p.poll()
if not sb_config.rec_subprocess_used or poll is not None:
pass
else:
show_already_recording_warning()
send_window_to_front(window)
poll = sb_config.rec_subprocess_p.poll()
if poll is None:
return
file_name = file_name.strip()
error_msg = file_name_error(file_name)
if error_msg:
messagebox.showwarning(
"Invalid filename", "Invalid filename: %s" % error_msg
)
return
url = url.strip()
if not page_utils.is_valid_url(url):
if page_utils.is_valid_url("https://" + url):
url = "https://" + url
if not page_utils.is_valid_url(url):
messagebox.showwarning(
"Invalid URL", "Enter a valid URL! (Eg. seleniumbase.io)"
)
else:
if os.path.exists(os.getcwd() + "/" + file_name):
if not overwrite_enabled:
msgbox = tk.messagebox.askquestion(
"Overwrite?",
'Are you sure you want to overwrite "%s"?' % file_name,
icon="warning",
)
if msgbox == "yes":
os.remove(file_name)
else:
tk.messagebox.showinfo("Cancelled", "Recording Cancelled!")
return
else:
os.remove(file_name)
add_on = ""
command_args = sys.argv[2:]
if (
"--rec-behave" in command_args
or "--behave" in command_args
or "--gherkin" in command_args
):
add_on = " --rec-behave"
command = (
"%s -m seleniumbase mkrec %s --url=%s --gui"
% (sys_executable, file_name, url)
)
if '"' not in url:
command = (
'%s -m seleniumbase mkrec %s --url="%s" --gui'
% (sys_executable, file_name, url)
)
elif "'" not in url:
command = (
"%s -m seleniumbase mkrec %s --url='%s' --gui"
% (sys_executable, file_name, url)
)
if not use_chrome:
command += " --edge"
elif "--opera" in command_args:
command += " --opera"
elif "--brave" in command_args:
command += " --brave"
elif "--comet" in command_args:
command += " --comet"
elif "--atlas" in command_args:
command += " --atlas"
elif "--use-chromium" in command_args:
command += " --use-chromium"
if (
"--uc" in command_args
or "--cdp" in command_args
or "--undetected" in command_args
or "--undetectable" in command_args
):
command += " --uc"
if "--ee" in command_args:
command += " --ee"
command += add_on
poll = None
if sb_config.rec_subprocess_used:
poll = sb_config.rec_subprocess_p.poll()
if not sb_config.rec_subprocess_used or poll is not None:
sb_config.rec_subprocess_p = subprocess.Popen(command, shell=True)
sb_config.rec_subprocess_used = True
else:
show_already_recording_warning()
send_window_to_front(window)
def do_playback(file_name, use_chrome, window, demo_mode=False):
file_name = file_name.strip()
error_msg = file_name_error(file_name)
if error_msg:
messagebox.showwarning(
"Invalid filename", "Invalid filename: %s" % error_msg
)
return
if not os.path.exists(os.getcwd() + "/" + file_name):
messagebox.showwarning(
"File does not exist",
'File "%s" does not exist in the current directory!' % file_name,
)
return
command = "%s -m pytest %s -q -s" % (sys_executable, file_name)
if shared_utils.is_linux():
command += " --gui"
if not use_chrome:
command += " --edge"
if demo_mode:
command += " --demo"
command_args = sys.argv[2:]
if (
"--uc" in command_args
or "--cdp" in command_args
or "--undetected" in command_args
or "--undetectable" in command_args
):
command += " --uc"
poll = None
if sb_config.rec_subprocess_used:
poll = sb_config.rec_subprocess_p.poll()
if not sb_config.rec_subprocess_used or poll is not None:
print(command)
subprocess.Popen(command, shell=True)
else:
messagebox.showwarning(
"SeleniumBase Recorder: Already Running!",
"Please finalize the active recording from the terminal\n"
'where you opened the Recorder: Type "c" and hit Enter.',
)
send_window_to_front(window)
def create_tkinter_gui():
default_file_name = "new_recording.py"
window = tk.Tk()
window.title("SeleniumBase Recorder App")
window.geometry("344x388")
frame = tk.Frame(window)
frame.pack()
tk.Label(window, text="").pack()
fname = tk.StringVar(value=default_file_name)
tk.Label(window, text="Enter filename to save recording as:").pack()
entry = tk.Entry(window, textvariable=fname)
entry.pack()
cbx = tk.IntVar()
chk = tk.Checkbutton(window, text="Overwrite existing files", variable=cbx)
chk.pack()
chk.select()
use_stealth = False
command_args = sys.argv[2:]
if (
"--uc" in command_args
or "--cdp" in command_args
or "--undetected" in command_args
or "--undetectable" in command_args
):
use_stealth = True
browser_display = "Use Chrome over Edge"
if "--opera" in command_args:
browser_display = "Use Opera over Edge"
elif "--brave" in command_args:
browser_display = "Use Brave over Edge"
elif "--comet" in command_args:
browser_display = "Use Comet over Edge"
elif "--atlas" in command_args:
browser_display = "Use Atlas over Edge"
cbb = tk.IntVar()
if not use_stealth:
chkb = tk.Checkbutton(window, text=browser_display, variable=cbb)
chkb.pack()
if "--edge" not in command_args:
chkb.select()
else:
chkb = tk.Checkbutton(
window, text="Stealthy Chrome Mode", variable=cbb
)
chkb.pack()
chkb.select()
chkb.config(state=tk.DISABLED)
tk.Label(window, text="").pack()
url = tk.StringVar()
tk.Label(window, text="Enter the URL to start recording on:").pack()
entry = tk.Entry(window, textvariable=url)
entry.pack()
entry.focus()
entry.bind(
"<Return>",
(
lambda _: do_recording(
fname.get(), url.get(), cbx.get(), cbb.get(), window
)
),
)
tk.Button(
window,
text="Record",
fg="red",
command=lambda: do_recording(
fname.get(), url.get(), cbx.get(), cbb.get(), window
),
).pack()
tk.Label(window, text="").pack()
tk.Label(window, text="Playback recording (Normal Mode):").pack()
tk.Button(
window,
text="Playback",
fg="green",
command=lambda: do_playback(fname.get(), cbb.get(), window),
).pack()
tk.Label(window, text="").pack()
tk.Label(window, text="Playback recording (Demo Mode):").pack()
try:
tk.Button(
window,
text="Playback (Demo Mode)",
fg="teal",
command=lambda: do_playback(
fname.get(), cbb.get(), window, demo_mode=True
),
).pack()
except Exception:
tk.Button(
window,
text="Playback (Demo Mode)",
fg="blue",
command=lambda: do_playback(
fname.get(), cbb.get(), window, demo_mode=True
),
).pack()
# Bring form window to front
send_window_to_front(window)
# Use decoy to set correct focus on main window
decoy = tk.Tk()
decoy.geometry("1x1")
decoy.iconify()
decoy.update()
decoy.deiconify()
decoy.destroy()
# Start tkinter
window.mainloop()
end_program()
def recorder_still_running():
poll = None
if sb_config.rec_subprocess_used:
try:
poll = sb_config.rec_subprocess_p.poll()
except Exception:
return False
else:
return False
if poll is not None:
return False
return True
def show_still_running_warning():
"""Give the user a chance to end the recording safely via the
pytest pdb Debug Mode so that processes such as chromedriver
and Python don't remain open and hanging in the background."""
messagebox.showwarning(
"SeleniumBase Recorder: Still Running!",
"Please finalize the active recording from the terminal\n"
'where you opened the Recorder: Type "c" and hit Enter.\n'
"(Then you can safely close this alert.)",
)
def end_program():
if recorder_still_running():
show_still_running_warning()
def main():
use_colors = True
if shared_utils.is_linux():
use_colors = False
c0, c1, c2, c3, c4, cr = set_colors(use_colors)
message = ""
message += c2
message += "*"
message += c4
message += " Starting the "
message += c0
message += "Selenium"
message += c1
message += "Base"
message += c2
message += " "
message += c3
message += "Recorder"
message += c4
message += " Desktop App"
message += c2
message += "..."
message += cr
print(message)
create_tkinter_gui()
if __name__ == "__main__":
print('To open the Recorder Desktop App: "seleniumbase recorder"')