-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathsb_commander.py
More file actions
467 lines (421 loc) · 12.8 KB
/
sb_commander.py
File metadata and controls
467 lines (421 loc) · 12.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"""
Launches SeleniumBase Commander | GUI for pytest.
Usage:
seleniumbase commander [OPTIONAL PATH or TEST FILE]
sbase commander [OPTIONAL PATH or TEST FILE]
seleniumbase gui [OPTIONAL PATH or TEST FILE]
sbase gui [OPTIONAL PATH or TEST FILE]
Examples:
sbase gui
sbase gui -k agent
sbase gui -m marker2
sbase gui test_suite.py
sbase gui offline_examples/
Output:
Launches SeleniumBase Commander | GUI for pytest.
"""
import colorama
import os
import subprocess
import sys
import tkinter as tk
from seleniumbase.fixtures import shared_utils
from tkinter.scrolledtext import ScrolledText
def set_colors(use_colors):
c0 = ""
c1 = ""
c2 = ""
c3 = ""
c4 = ""
c5 = ""
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.BLACK + colorama.Back.LIGHTGREEN_EX
c4 = colorama.Fore.BLUE + colorama.Back.LIGHTYELLOW_EX
c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX
cr = colorama.Style.RESET_ALL
return c0, c1, c2, c3, c4, c5, cr
def send_window_to_front(root):
root.lift()
root.attributes("-topmost", True)
root.after_idle(root.attributes, "-topmost", False)
def do_pytest_run(
root,
tests,
selected_tests,
command_string,
browser_string,
rs_string,
thread_string,
verbose,
demo_mode,
mobile_mode,
dashboard,
html_report,
headless,
save_screenshots,
additional_options,
):
cleaned_tests = []
for test in tests:
if test.startswith("(FILE) "):
clean_test = test.split("(FILE) ")[1].split(" => ")[0]
cleaned_tests.append(clean_test)
else:
cleaned_tests.append(test)
tests = cleaned_tests
total_tests = len(tests)
total_selected_tests = 0
for selected_test in selected_tests:
if selected_tests[selected_test].get():
total_selected_tests += 1
full_run_command = '"%s" -m pytest' % sys.executable
if total_selected_tests == 0 or total_tests == total_selected_tests:
if command_string:
full_run_command += " "
full_run_command += command_string
else:
for test_number, test in enumerate(tests):
if selected_tests[test_number].get():
full_run_command += " "
if ' ' not in test:
full_run_command += test
elif '"' not in test:
full_run_command += '"%s"' % test
else:
full_run_command += test.replace(" ", "\\ ")
if "(--edge)" in browser_string:
full_run_command += " --edge"
elif "(--firefox)" in browser_string:
full_run_command += " --firefox"
elif "(--safari)" in browser_string:
full_run_command += " --safari"
if "(--rs)" in rs_string:
full_run_command += " --rs"
elif "(--rs --crumbs)" in rs_string:
full_run_command += " --rs --crumbs"
elif "(--rcs)" in rs_string:
full_run_command += " --rcs"
elif "(--rcs --crumbs)" in rs_string:
full_run_command += " --rcs --crumbs"
if "(-n=2)" in thread_string:
full_run_command += " -n=2"
elif "(-n=3)" in thread_string:
full_run_command += " -n=3"
elif "(-n=4)" in thread_string:
full_run_command += " -n=4"
elif "(-n=5)" in thread_string:
full_run_command += " -n=5"
elif "(-n=6)" in thread_string:
full_run_command += " -n=6"
elif "(-n=7)" in thread_string:
full_run_command += " -n=7"
elif "(-n=8)" in thread_string:
full_run_command += " -n=8"
if demo_mode:
full_run_command += " --demo"
if mobile_mode:
full_run_command += " --mobile"
if dashboard:
full_run_command += " --dashboard"
if html_report:
full_run_command += " --html=report.html"
if headless:
full_run_command += " --headless"
elif shared_utils.is_linux():
full_run_command += " --gui"
if save_screenshots:
full_run_command += " --screenshot"
capture_needed = False
if "--capture" not in additional_options:
capture_needed = True
additional_options = additional_options.strip()
if additional_options:
full_run_command += " "
full_run_command += additional_options
if verbose:
full_run_command += " -v"
if capture_needed:
full_run_command += " --capture=tee-sys"
print(full_run_command)
subprocess.Popen(full_run_command, shell=True)
send_window_to_front(root)
def create_tkinter_gui(tests, command_string, files, solo_tests):
root = tk.Tk()
root.title("SeleniumBase Commander | GUI for pytest")
if shared_utils.is_windows():
root.minsize(820, 696)
else:
root.minsize(820, 702)
tk.Label(root, text="").pack()
options_list = [
"Use Chrome Browser (Default)",
"Use Edge Browser (--edge)",
"Use Firefox Browser (--firefox)",
]
if shared_utils.is_mac():
options_list.append("Use Safari Browser (--safari)")
brx = tk.StringVar(root)
brx.set(options_list[0])
question_menu = tk.OptionMenu(root, brx, *options_list)
question_menu.pack()
options_list = [
"New Session Per Test (Default)",
"Reuse Session for ALL tests in thread (--rs)",
"Reuse Session and also clear cookies (--rs --crumbs)",
"Reuse Session for tests with same CLASS (--rcs)",
"Reuse Session for class and clear cookies (--rcs --crumbs)",
]
rsx = tk.StringVar(root)
rsx.set(options_list[0])
question_menu = tk.OptionMenu(root, rsx, *options_list)
question_menu.pack()
options_list = [
"Number of Threads: 1 (Default)",
"Number of Threads: 2 (-n=2)",
"Number of Threads: 3 (-n=3)",
"Number of Threads: 4 (-n=4)",
]
try:
if int(os.cpu_count()) >= 8:
options_list.append("Number of Threads: 5 (-n=5)")
options_list.append("Number of Threads: 6 (-n=6)")
options_list.append("Number of Threads: 7 (-n=7)")
options_list.append("Number of Threads: 8 (-n=8)")
except Exception:
pass
ntx = tk.StringVar(root)
ntx.set(options_list[0])
question_menu = tk.OptionMenu(root, ntx, *options_list)
question_menu.pack()
vox = tk.IntVar()
chk = tk.Checkbutton(
root, text="Verbose Output (-v)", variable=vox, pady=0
)
chk.pack()
chk.select()
dmx = tk.IntVar()
chk = tk.Checkbutton(
root, text="Demo Mode (--demo)", variable=dmx, pady=0
)
chk.pack()
mmx = tk.IntVar()
chk = tk.Checkbutton(
root, text="Mobile Mode (--mobile)", variable=mmx, pady=0
)
chk.pack()
dbx = tk.IntVar()
chk = tk.Checkbutton(
root, text="Dashboard (--dashboard)", variable=dbx, pady=0
)
chk.pack()
chk.select()
hrx = tk.IntVar()
chk = tk.Checkbutton(
root, text="Report (--html=report.html)", variable=hrx, pady=0
)
chk.pack()
chk.select()
hbx = tk.IntVar()
chk = tk.Checkbutton(
root, text="Headless Browser (--headless)", variable=hbx, pady=0
)
chk.pack()
ssx = tk.IntVar()
chk = tk.Checkbutton(
root, text="Save Screenshots (--screenshot)", variable=ssx, pady=0
)
chk.pack()
tk.Label(root, text="").pack()
run_display = (
"Select from %s rows (%s files with %s tests): "
"(All tests will run if none are selected)"
% (len(tests), len(files), len(solo_tests))
)
if len(solo_tests) == 1:
run_display = "Only ONE TEST was found and will be run:"
tests = solo_tests
elif len(files) == 1:
run_display = (
"Select from %s tests: "
"(All tests will run if none are selected)"
% (len(solo_tests))
)
tests = solo_tests
tk.Label(root, text=run_display, bg="yellow", fg="magenta").pack()
text_area = ScrolledText(
root, width=100, height=12, wrap="word", state=tk.DISABLED
)
text_area.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
count = 0
ara = {}
for row in tests:
row += " " * 200
ara[count] = tk.IntVar()
cb = None
if shared_utils.is_windows():
cb = tk.Checkbutton(
text_area,
text=(row),
bg="white",
fg="black",
anchor="w",
pady=0,
borderwidth=1,
highlightthickness=1,
variable=ara[count],
)
else:
cb = tk.Checkbutton(
text_area,
text=(row),
bg="white",
fg="black",
anchor="w",
pady=0,
variable=ara[count],
)
text_area.window_create("end", window=cb)
text_area.insert("end", "\n")
count += 1
tk.Label(root, text="").pack()
additional_options = ""
aopts = tk.StringVar(value=additional_options)
tk.Label(
root,
text='Additional "pytest" Options: (Eg. "--incognito --slow")',
bg="yellow", fg="blue",
).pack()
entry = tk.Entry(root, textvariable=aopts)
entry.pack()
entry.focus()
entry.bind(
"<Return>",
(
lambda _: do_pytest_run(
root,
tests,
ara,
command_string,
brx.get(),
rsx.get(),
ntx.get(),
vox.get(),
dmx.get(),
mmx.get(),
dbx.get(),
hrx.get(),
hbx.get(),
ssx.get(),
aopts.get(),
)
),
)
tk.Button(
root,
text="Run Selected Tests",
fg="green",
command=lambda: do_pytest_run(
root,
tests,
ara,
command_string,
brx.get(),
rsx.get(),
ntx.get(),
vox.get(),
dmx.get(),
mmx.get(),
dbx.get(),
hrx.get(),
hbx.get(),
ssx.get(),
aopts.get(),
),
).pack()
tk.Label(root, text="\n").pack()
# Bring form window to front
send_window_to_front(root)
# 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
root.mainloop()
def main():
use_colors = True
if shared_utils.is_linux():
use_colors = False
c0, c1, c2, c3, c4, c5, cr = set_colors(use_colors)
command_args = sys.argv[2:]
command_string = " ".join(command_args)
message = ""
message += c2
message += "*"
message += c4
message += " Starting the "
message += c0
message += "Selenium"
message += c1
message += "Base"
message += c2
message += " "
message += c3
message += "Commander"
message += c4
message += " Desktop App"
message += c2
message += "..."
message += cr
print(message)
proc = subprocess.Popen(
'"%s" -m pytest --collect-only -q --rootdir="./" %s'
% (sys.executable, command_string),
stdout=subprocess.PIPE,
shell=True,
)
(output, error) = proc.communicate()
if error:
error_msg = "Error collecting tests: %s" % str(error)
error_msg = c5 + error_msg + cr
print(error_msg)
return
tests = []
if shared_utils.is_windows():
output = output.decode("latin1")
else:
output = output.decode("utf-8")
for row in output.replace("\r", "").split("\n"):
if ("::") in row:
tests.append(row)
if not tests:
error_msg = "No tests found! Exiting SeleniumBase Commander..."
error_msg = c5 + "ERROR: " + error_msg + cr
print(error_msg)
return
groups = []
for row in tests:
if row.count("::") >= 1:
g_name = "(FILE) %s" % row.split("::")[0]
groups.append(g_name)
files = []
used_files = []
for row in groups:
if row not in used_files:
used_files.append(row)
plural = "s"
if groups.count(row) == 1:
plural = ""
f_row = "%s => (%s Test%s)" % (row, groups.count(row), plural)
files.append(f_row)
solo_tests = tests
tests = [*files, *tests]
create_tkinter_gui(tests, command_string, files, solo_tests)
if __name__ == "__main__":
print('To open SBase Commander, type "sbase commander" or "sbase gui"')