Skip to content

Commit

Permalink
test: update button text & upload pw traces
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Sep 23, 2024
1 parent 00b910b commit 18541ed
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 27 additions & 6 deletions cmd/cloudx/testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)

Expand All @@ -257,16 +263,31 @@ 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
require.NoError(t, page.Locator(`button:has-text("Allow")`).Click())

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")...)
}
16 changes: 15 additions & 1 deletion cmd/cloudx/testhelpers/testmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func CreateDefaultAssetsBrowser() (ctx context.Context, defaultConfig, defaultWo
UseStaging()

t := MockTestingTForMain{}
defer t.ExitOnFailure()

defaultConfig = NewConfigFile(t)

Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit 18541ed

Please sign in to comment.