-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_testing.go
More file actions
554 lines (474 loc) · 13.9 KB
/
cmd_testing.go
File metadata and controls
554 lines (474 loc) · 13.9 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
)
func cmdTest(args []string) {
if len(args) < 1 {
printTestUsage()
os.Exit(1)
}
sub := args[0]
switch sub {
case "cases":
cmdTestCases(args[1:])
case "scripts":
cmdTestScripts(args[1:])
case "run":
cmdTestRun(args[1:])
case "generate":
cmdTestGenerate(args[1:])
case "status":
cmdTestStatus(args[1:])
case "help", "--help", "-h":
printTestUsage()
default:
fmt.Fprintf(os.Stderr, "Unknown test subcommand: %s\n\n", sub)
printTestUsage()
os.Exit(1)
}
}
func printTestUsage() {
fmt.Println(`Usage: qmax test <subcommand> [flags]
Subcommands:
cases List test cases for a project
scripts List automation scripts for a project
run Execute test scripts (single or batch)
generate Generate Playwright code for a test case
status Check execution status
Examples:
qmax test cases --project-id 42
qmax test scripts --project-id 42
qmax test run --script-id 101
qmax test run --script-ids 101,102,103 --headless
qmax test generate --test-case-id 55
qmax test status --execution-id agent_exec_101_1710000000`)
}
// --- test cases ---
func cmdTestCases(args []string) {
fs := flag.NewFlagSet("test cases", flag.ExitOnError)
projectID := fs.Int("project-id", 0, "Project ID (required)")
limit := fs.Int("limit", 50, "Max results to return")
search := fs.String("search", "", "Search in title/description")
category := fs.String("category", "", "Filter by category")
status := fs.String("status", "", "Filter by status")
jsonOut := fs.Bool("json", false, "Output raw JSON")
_ = fs.Parse(args)
if *projectID == 0 {
fmt.Fprintln(os.Stderr, "Error: --project-id is required")
os.Exit(1)
}
cfg := mustLoadConfig()
apiURL := cfg.GetAPIBaseURL()
url := fmt.Sprintf("%s/api/test-cases/project/%d?limit=%d", apiURL, *projectID, *limit)
if *search != "" {
url += "&search=" + *search
}
if *category != "" {
url += "&category=" + *category
}
if *status != "" {
url += "&status=" + *status
}
body := authGet(cfg, url)
if *jsonOut {
fmt.Println(string(body))
return
}
var resp struct {
Success bool `json:"success"`
Count int `json:"count"`
Project struct {
ID json.Number `json:"id"`
Name string `json:"name"`
} `json:"project"`
TestCases []struct {
ID json.Number `json:"id"`
Title string `json:"title"`
Category string `json:"category"`
Priority int `json:"priority"`
Status string `json:"status"`
Automated bool `json:"automated"`
} `json:"test_cases"`
}
mustUnmarshal(body, &resp)
fmt.Printf("Project: %s (#%s)\n", resp.Project.Name, resp.Project.ID.String())
fmt.Printf("Test cases: %d\n\n", resp.Count)
if resp.Count == 0 {
fmt.Println("No test cases found.")
return
}
fmt.Printf("%-6s %-10s %-4s %-10s %-4s %s\n", "ID", "Category", "Pri", "Status", "Auto", "Title")
fmt.Printf("%-6s %-10s %-4s %-10s %-4s %s\n", "------", "----------", "----", "----------", "----", "-----")
for _, tc := range resp.TestCases {
auto := " "
if tc.Automated {
auto = "Y"
}
title := tc.Title
if len(title) > 60 {
title = title[:57] + "..."
}
fmt.Printf("%-6s %-10s %-4d %-10s %-4s %s\n",
tc.ID.String(), truncate(tc.Category, 10), tc.Priority, truncate(tc.Status, 10), auto, title)
}
}
// --- test scripts ---
func cmdTestScripts(args []string) {
fs := flag.NewFlagSet("test scripts", flag.ExitOnError)
projectID := fs.Int("project-id", 0, "Project ID (required)")
limit := fs.Int("limit", 50, "Max results to return")
jsonOut := fs.Bool("json", false, "Output raw JSON")
_ = fs.Parse(args)
if *projectID == 0 {
fmt.Fprintln(os.Stderr, "Error: --project-id is required")
os.Exit(1)
}
cfg := mustLoadConfig()
apiURL := cfg.GetAPIBaseURL()
url := fmt.Sprintf("%s/api/automation/scripts/project/%d?limit=%d", apiURL, *projectID, *limit)
body := authGet(cfg, url)
if *jsonOut {
fmt.Println(string(body))
return
}
var resp struct {
Success bool `json:"success"`
Count int `json:"count"`
Scripts []struct {
ID json.Number `json:"id"`
TestCaseID json.Number `json:"test_case_id"`
Name string `json:"name"`
Framework string `json:"framework"`
CreatedAt string `json:"created_at"`
} `json:"scripts"`
}
mustUnmarshal(body, &resp)
fmt.Printf("Scripts: %d\n\n", resp.Count)
if resp.Count == 0 {
fmt.Println("No scripts found.")
return
}
fmt.Printf("%-6s %-8s %-12s %s\n", "ID", "TC ID", "Framework", "Name")
fmt.Printf("%-6s %-8s %-12s %s\n", "------", "--------", "------------", "----")
for _, s := range resp.Scripts {
name := s.Name
if len(name) > 50 {
name = name[:47] + "..."
}
fmt.Printf("%-6s %-8s %-12s %s\n",
s.ID.String(), s.TestCaseID.String(), s.Framework, name)
}
}
// --- test run ---
func cmdTestRun(args []string) {
fs := flag.NewFlagSet("test run", flag.ExitOnError)
scriptID := fs.Int("script-id", 0, "Single script ID to execute")
scriptIDs := fs.String("script-ids", "", "Comma-separated script IDs for batch execution")
baseURL := fs.String("base-url", "", "Base URL for the application under test")
headless := fs.Bool("headless", true, "Run in headless mode (default: true)")
browser := fs.String("browser", "chromium", "Browser: chromium, firefox, webkit")
wait := fs.Bool("wait", false, "Wait for execution to complete and show result")
jsonOut := fs.Bool("json", false, "Output raw JSON")
_ = fs.Parse(args)
if *scriptID == 0 && *scriptIDs == "" {
fmt.Fprintln(os.Stderr, "Error: --script-id or --script-ids is required")
os.Exit(1)
}
cfg := mustLoadConfig()
apiURL := cfg.GetAPIBaseURL()
if *scriptIDs != "" {
// Batch execution
ids := parseIntList(*scriptIDs)
if len(ids) == 0 {
fmt.Fprintln(os.Stderr, "Error: --script-ids must be comma-separated integers")
os.Exit(1)
}
payload := map[string]interface{}{
"script_ids": ids,
}
if *baseURL != "" {
payload["custom_url"] = *baseURL
}
url := fmt.Sprintf("%s/api/automation/execute-batch", apiURL)
body := authPost(cfg, url, payload)
if *jsonOut {
fmt.Println(string(body))
return
}
var resp struct {
Success bool `json:"success"`
Message string `json:"message"`
}
mustUnmarshal(body, &resp)
if resp.Success {
fmt.Printf("Batch execution started for %d scripts\n", len(ids))
} else {
fmt.Fprintf(os.Stderr, "Error: %s\n", resp.Message)
os.Exit(1)
}
return
}
// Single script execution via playwright-execution endpoint
payload := map[string]interface{}{
"headless": *headless,
"browser": *browser,
"record_video": true,
"viewport_width": 1280,
"viewport_height": 720,
}
if *baseURL != "" {
payload["base_url"] = *baseURL
}
url := fmt.Sprintf("%s/api/playwright-execution/run/%d", apiURL, *scriptID)
body := authPost(cfg, url, payload)
if *jsonOut {
fmt.Println(string(body))
return
}
var resp struct {
Status string `json:"status"`
ExecutionID string `json:"execution_id"`
Message string `json:"message"`
ScriptID int `json:"script_id"`
ScriptName string `json:"script_name"`
}
mustUnmarshal(body, &resp)
fmt.Printf("Execution started: %s\n", resp.ExecutionID)
fmt.Printf("Script: %s (#%d)\n", resp.ScriptName, resp.ScriptID)
if *wait {
fmt.Println("\nWaiting for completion...")
pollExecution(cfg, apiURL, resp.ExecutionID, *jsonOut)
} else {
fmt.Printf("\nCheck status: qmax test status --execution-id %s\n", resp.ExecutionID)
}
}
// --- test generate ---
func cmdTestGenerate(args []string) {
fs := flag.NewFlagSet("test generate", flag.ExitOnError)
testCaseID := fs.Int("test-case-id", 0, "Test case ID to generate code for (required)")
force := fs.Bool("force", false, "Regenerate even if code already exists")
jsonOut := fs.Bool("json", false, "Output raw JSON")
_ = fs.Parse(args)
if *testCaseID == 0 {
fmt.Fprintln(os.Stderr, "Error: --test-case-id is required")
os.Exit(1)
}
cfg := mustLoadConfig()
apiURL := cfg.GetAPIBaseURL()
payload := map[string]interface{}{}
if *force {
payload["force"] = true
}
url := fmt.Sprintf("%s/api/test-cases/%d/generate-code", apiURL, *testCaseID)
body := authPost(cfg, url, payload)
if *jsonOut {
fmt.Println(string(body))
return
}
var resp struct {
Success bool `json:"success"`
Message string `json:"message"`
ScriptID json.Number `json:"script_id"`
Code string `json:"code"`
}
mustUnmarshal(body, &resp)
if resp.Success {
fmt.Printf("Code generated for test case #%d\n", *testCaseID)
if resp.ScriptID.String() != "" {
fmt.Printf("Script ID: %s\n", resp.ScriptID.String())
}
fmt.Printf("\nRun it: qmax test run --script-id %s\n", resp.ScriptID.String())
} else {
fmt.Fprintf(os.Stderr, "Error: %s\n", resp.Message)
os.Exit(1)
}
}
// --- test status ---
func cmdTestStatus(args []string) {
fs := flag.NewFlagSet("test status", flag.ExitOnError)
executionID := fs.String("execution-id", "", "Execution ID to check (required)")
jsonOut := fs.Bool("json", false, "Output raw JSON")
_ = fs.Parse(args)
if *executionID == "" {
fmt.Fprintln(os.Stderr, "Error: --execution-id is required")
os.Exit(1)
}
cfg := mustLoadConfig()
apiURL := cfg.GetAPIBaseURL()
pollExecution(cfg, apiURL, *executionID, *jsonOut)
}
// --- polling ---
func pollExecution(cfg *Config, apiURL, executionID string, jsonOut bool) {
url := fmt.Sprintf("%s/api/playwright-execution/status/%s", apiURL, executionID)
maxAttempts := 120 // 10 minutes at 5s intervals
for i := 0; i < maxAttempts; i++ {
body := authGet(cfg, url)
var resp struct {
Status string `json:"status"`
Phase string `json:"phase"`
Passed *bool `json:"passed"`
ErrorMessage string `json:"error_message"`
Screenshots []string `json:"screenshots"`
VideoURL string `json:"video_url"`
Duration float64 `json:"duration_seconds"`
}
if err := json.Unmarshal(body, &resp); err != nil {
// Might be an error response
fmt.Fprintf(os.Stderr, "Error parsing status: %v\n", err)
os.Exit(1)
}
if jsonOut {
fmt.Println(string(body))
}
switch resp.Status {
case "completed", "passed":
if !jsonOut {
fmt.Printf("Status: PASSED\n")
if resp.Duration > 0 {
fmt.Printf("Duration: %.1fs\n", resp.Duration)
}
if len(resp.Screenshots) > 0 {
fmt.Printf("Screenshots: %d captured\n", len(resp.Screenshots))
}
if resp.VideoURL != "" {
fmt.Printf("Video: %s\n", resp.VideoURL)
}
}
return
case "failed":
if !jsonOut {
fmt.Printf("Status: FAILED\n")
if resp.ErrorMessage != "" {
fmt.Printf("Error: %s\n", resp.ErrorMessage)
}
if resp.Duration > 0 {
fmt.Printf("Duration: %.1fs\n", resp.Duration)
}
}
os.Exit(1)
default:
if !jsonOut && i == 0 {
fmt.Printf("Status: %s", resp.Status)
if resp.Phase != "" {
fmt.Printf(" (%s)", resp.Phase)
}
fmt.Println()
} else if !jsonOut && i%6 == 0 { // Print every 30s
fmt.Printf(" ...still %s", resp.Status)
if resp.Phase != "" {
fmt.Printf(" (%s)", resp.Phase)
}
fmt.Println()
}
}
time.Sleep(5 * time.Second)
}
fmt.Fprintln(os.Stderr, "Timed out waiting for execution to complete")
os.Exit(1)
}
// =============================================================================
// Helpers
// =============================================================================
func mustLoadConfig() *Config {
cfg, err := LoadConfig()
if err != nil || cfg.Token == "" {
fmt.Fprintln(os.Stderr, "Error: not logged in. Run `qmax login` first.")
os.Exit(1)
}
return cfg
}
func authGet(cfg *Config, url string) []byte {
client := &http.Client{Timeout: 30 * time.Second}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.Token))
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
const maxBody = 10 * 1024 * 1024
body, err := io.ReadAll(io.LimitReader(resp.Body, maxBody))
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading response: %v\n", err)
os.Exit(1)
}
if resp.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "Error: %d - %s\n", resp.StatusCode, string(body))
os.Exit(1)
}
return body
}
func authPost(cfg *Config, url string, payload interface{}) []byte {
data, err := json.Marshal(payload)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
client := &http.Client{Timeout: 60 * time.Second}
req, err := http.NewRequest("POST", url, bytes.NewReader(data))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.Token))
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
const maxBody = 10 * 1024 * 1024
body, err := io.ReadAll(io.LimitReader(resp.Body, maxBody))
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading response: %v\n", err)
os.Exit(1)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
fmt.Fprintf(os.Stderr, "Error: %d - %s\n", resp.StatusCode, string(body))
os.Exit(1)
}
return body
}
func mustUnmarshal(data []byte, v interface{}) {
if err := json.Unmarshal(data, v); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing response: %v\n", err)
os.Exit(1)
}
}
func parseIntList(s string) []int {
parts := strings.Split(s, ",")
var result []int
for _, p := range parts {
p = strings.TrimSpace(p)
if p == "" {
continue
}
n, err := strconv.Atoi(p)
if err != nil {
return nil
}
result = append(result, n)
}
return result
}
func truncate(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
return s[:maxLen-3] + "..."
}