diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index fbe0918f..0a0ed434 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -32,6 +32,10 @@ jobs: ORY_RATE_LIMIT_HEADER: ${{ secrets.ORY_RATE_LIMIT_HEADER }} ORY_CONSOLE_URL: https://console.staging.ory.dev ORY_ORYAPIS_URL: https://staging.oryapis.dev + - uses: actions/upload-artifact@v4 + with: + name: playwright-traces + path: playwright-traces docs: name: Generate docs diff --git a/cmd/cloudx/testhelpers/testhelpers.go b/cmd/cloudx/testhelpers/testhelpers.go index de9e8557..27c4d06f 100644 --- a/cmd/cloudx/testhelpers/testhelpers.go +++ b/cmd/cloudx/testhelpers/testhelpers.go @@ -211,13 +211,11 @@ func RegisterAccount(ctx context.Context, t testing.TB) (email, password, name, } func SetupPlaywright(t testing.TB) (playwright.Browser, playwright.Page, func()) { - _ = os.RemoveAll("./playwright-traces") - pw, err := playwright.Run() require.NoError(t, err) browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{ Headless: Ptr(true), - TracesDir: Ptr("./playwright-traces"), + TracesDir: Ptr(tracesDir), }) require.NoError(t, err) page, err := browser.NewPage(playwright.BrowserNewPageOptions{ @@ -240,6 +238,14 @@ func PlaywrightAcceptConsentBrowserHook(t testing.TB, page playwright.Page, emai Screenshots: Ptr(true), Snapshots: Ptr(true), })) + defer func() { + r := recover() + _ = page.Context().Tracing().Stop(filepath.Join(tracesDir, fmt.Sprintf("%s.zip", t.Name()))) + if r != nil { + panic(r) + } + }() + _, err := page.Goto(uri) require.NoError(t, err) @@ -257,7 +263,7 @@ func PlaywrightAcceptConsentBrowserHook(t testing.TB, page playwright.Page, emai } // we wait here for the button +1s because there is some console bug that can lead to form submissions before the form action is correctly set - require.NoError(t, page.Locator(`button:has-text("Accept")`).WaitFor()) + require.NoError(t, page.Locator(`button:has-text("Allow")`).WaitFor()) time.Sleep(time.Second) // accept consent @@ -265,8 +271,23 @@ func PlaywrightAcceptConsentBrowserHook(t testing.TB, page playwright.Page, emai t.Logf("consent successful") - require.NoError(t, page.Context().Tracing().Stop("playwright-traces/login-consent-trace.zip")) - return nil } } + +var tracesDir string + +func init() { + cwd, err := os.Getwd() + if err != nil { + panic(err) + } + dirs := strings.Split(cwd, string(os.PathSeparator)) + for i := range dirs { + if dirs[i] == "cloudx" { + dirs = dirs[:i-1] + break + } + } + tracesDir = string(os.PathSeparator) + filepath.Join(append(dirs, "playwright-traces")...) +} diff --git a/cmd/cloudx/testhelpers/testmain.go b/cmd/cloudx/testhelpers/testmain.go index 23eff45e..3471392e 100644 --- a/cmd/cloudx/testhelpers/testmain.go +++ b/cmd/cloudx/testhelpers/testmain.go @@ -37,6 +37,7 @@ func CreateDefaultAssetsBrowser() (ctx context.Context, defaultConfig, defaultWo UseStaging() t := MockTestingTForMain{} + defer t.ExitOnFailure() defaultConfig = NewConfigFile(t) @@ -65,6 +66,8 @@ func CreateDefaultAssetsBrowser() (ctx context.Context, defaultConfig, defaultWo return } +// MockTestingTForMain is a mock testing.TB implementation that is used in TestMain. +// Always defer t.ExitOnFailure() in the TestMain function. type MockTestingTForMain struct { testing.TB } @@ -84,8 +87,19 @@ func (MockTestingTForMain) Errorf(format string, args ...interface{}) { debug.PrintStack() } +type exitCode int + func (MockTestingTForMain) FailNow() { - os.Exit(1) + panic(exitCode(1)) +} + +func (MockTestingTForMain) ExitOnFailure() { + if r := recover(); r != nil { + if e, ok := r.(exitCode); ok { + os.Exit(int(e)) + } + panic(r) + } } func (MockTestingTForMain) TempDir() string {