44 "fmt"
55 "os"
66 "path/filepath"
7+ "strings"
78 "testing"
89
910 "github.com/playwright-community/playwright-go"
@@ -20,27 +21,72 @@ func trace(t *testing.T, context playwright.BrowserContext, testName string) {
2021 })
2122 require .NoError (t , err )
2223
24+ tracePath := filepath .Join (getTraceDirectory (t ), fmt .Sprintf ("trace-%s.zip" , testName ))
25+
26+ t .Cleanup (func () {
27+ if t .Failed () {
28+ if err := context .Tracing ().Stop (tracePath ); err != nil {
29+ t .Logf ("failed to save trace: %v" , err )
30+ } else {
31+ t .Logf ("saved trace to %s" , tracePath )
32+ }
33+ }
34+ })
35+ }
36+
37+ func getTraceDirectory (t * testing.T ) string {
2338 dir , err := os .Getwd ()
2439 require .NoError (t , err )
25- tracePath := filepath .Join (dir , ".." , ".." , "trace" , fmt .Sprintf ("trace-%s.zip" , testName ))
40+
41+ dirName := "trace"
42+ getTraceDirectoryPath := filepath .Join (dir , ".." , ".." , dirName )
2643
2744 if os .Getenv ("RUNNING_IN_CONTAINER" ) == "true" {
28- tracePath = filepath .Join (os .Getenv ("E2E_REPO_PATH" ), "trace" , fmt . Sprintf ( "trace-%s.zip" , testName ) )
45+ getTraceDirectoryPath = filepath .Join (os .Getenv ("E2E_REPO_PATH" ), dirName )
2946 }
3047
31- if os .Getenv ("CI" ) == "true" {
48+ // Use ARTIFACT_DIR if set (CI environment)
49+ if artifactDir := os .Getenv ("ARTIFACT_DIR" ); artifactDir != "" {
3250 // save trace in the job CI artifact directory
33- // artifacts/e2e/test/artifacts/devsandbox-dashboard/trace/trace-%s.zip
34- tracePath = filepath .Join (os . Getenv ( "ARTIFACT_DIR" ) , "devsandbox-dashboard" , "trace" , fmt . Sprintf ( "trace-%s.zip" , testName ) )
51+ // artifacts/e2e/test/artifacts/devsandbox-dashboard/trace
52+ getTraceDirectoryPath = filepath .Join (artifactDir , "devsandbox-dashboard" , dirName )
3553 }
3654
55+ return getTraceDirectoryPath
56+ }
57+
58+ // handleRecordedVideo manages recorded videos for main pages and popups:
59+ // - On test failure: renames videos from auto-generated IDs to test names (popups include UUID suffix)
60+ // - On test success: removes videos
61+ func handleRecordedVideo (t * testing.T , page playwright.Page , targetVideoPath string ) {
3762 t .Cleanup (func () {
63+ videoPath , err := page .Video ().Path ()
64+ if err != nil || videoPath == "" {
65+ t .Logf ("failed to resolve video path %s: %v" , videoPath , err )
66+ return
67+ }
68+
69+ // Handle failed test - rename video
3870 if t .Failed () {
39- if err := context .Tracing ().Stop (tracePath ); err != nil {
40- t .Logf ("failed to save trace: %v" , err )
41- } else {
42- t .Logf ("saved trace to %s" , tracePath )
71+ // For popup videos, rename with UUID to avoid conflicts when multiple popups exist in the same test
72+ if strings .Contains (targetVideoPath , "popup" ) {
73+ uuid := filepath .Base (videoPath )
74+ uuid = strings .TrimSuffix (uuid , ".webm" )
75+ if len (uuid ) > 8 {
76+ uuid = uuid [:8 ] // Truncate to first 8 chars
77+ }
78+ targetVideoPath = strings .Replace (targetVideoPath , "popup" , fmt .Sprintf ("popup-%s" , uuid ), 1 )
79+ }
80+
81+ if err := os .Rename (videoPath , targetVideoPath ); err != nil {
82+ t .Logf ("failed to rename video %s: %v" , videoPath , err )
4383 }
84+ return
85+ }
86+
87+ // Test passed - remove video
88+ if err := os .Remove (videoPath ); err != nil {
89+ t .Logf ("failed to remove video %s: %v" , videoPath , err )
4490 }
4591 })
4692}
0 commit comments