shiori/e2e/playwright/testhelper.go
Felipe Martin 73a5239753
refactor(apiv1): accounts api (#825)
* list account and create account

* deleteaccount (wip)

* remove old accounts code

* fix from merge

* remove serve method from makefile

* ListAccounts, password hash on domain

* make lint

* more permissive assertion

* rename test

* update account

* Authorization

* updated api calls

* apis, pointers, auth

* swagger

* stylecheck

* domain validation

* tests

* swagger

* error handling

* fix system account changes

* Cleanup database interface

* test cleanup

* fixed nil references

* feat: Add logout endpoint to auth routes

* feat: Add logoutHandler for stateless JWT token logout

* fixed some bug catched in tests

* auth/account patch

* prettier

* remove test logs

* fixed incorrect number of parameters

* fixed swagger docs

* enable swagger in dev environment

* errors.Wrap -> fmt.Errorf

* test: Add comprehensive test cases for accounts API handlers

* fix: Resolve test failures in accounts_test.go

* test: Add tests for duplicate username handling in account creation and update

* feat: Add username uniqueness checks for account creation and update

refactor: Improve username existence checks in SQLite account methods

* linted

* test: Add comprehensive tests for auth domain token and credential validation

* test: Add comprehensive test cases for auth domain token creation and validation

* test: Add comprehensive error handling test cases for accounts domain

* refactor: Remove `SaveAccountSettings` method from database implementations

* test: Add test cases for password update functionality

* test(e2e): auth login

* lint

* send regular context to domain

* fixed e2e auth tests

* test: Add auth_test.go for end-to-end authentication testing

* feat: Add comprehensive authentication tests using Playwright and testcontainers

* fix: Handle multiple return values in Playwright test methods

* error message

* e2e playwrigth tests

* ci: setup playwrigth

* refactor: Update Playwright tests to use locator-based API

* refactor: Remove unnecessary alias for playwright-go expect import

* refactor: Replace deprecated expect package with WaitFor() method in Playwright tests

* fix: Resolve linting issues in e2e Playwright tests

* remove npm ci from e2e ci

* make playwright available in path

* typo

* re enabled ci

* base e2e accounts test

* more account e2e

* feat: Add HTML test reporter with screenshots and detailed results

* feat: Embed screenshots as base64 in HTML test report

* refactor: Remove GitHub step summary functionality from test helper

* refactor: Make reporter global to share test results across test helpers

* refactor: Add HandleSuccess method to TestHelper for consistent test result reporting

* feat: Add descriptive messages to all test assertions in TestHelper

* test: Add descriptive messages to assertions in accounts_test.go

* test: Add descriptive error messages to assertions in accounts_test.go

* feat: Add descriptive messages to assertions in accounts_test.go

* refactor: Update assertion functions to receive *testing.T as first argument

* refactor: Update accounts_test.go assertions to pass *testing.T argument

* refactor: Update accounts_test.go assertions to use *testing.T argument

* refactor: Update `accounts_test.go` to use `*testing.T` argument in `Require()` calls

* refactor: Update `th.Require()` calls with `t *testing.T` argument in accounts_test.go

* assert helper

* refactor: Refactor `False` test helper to use `Assert` function consistently

* refactor: Refactor `Equal` test helper to use `Assert` function

* refactor: Simplify Error test helper to use Assert function

* refactor: Refactor `NoError` to use `Assert` function for consistent error handling

* typo

* refactor: Differentiate between test cases and assertions in reporter

* refactor: Simplify AddResult method signature and use error message for assertion

* refactor: Simplify test report with focused failure details and screenshots

* refactor: Ensure assertions are always called in PlaywrightRequire helper methods

* refactor: Update test error messages to be action-oriented

* refactor: Update error messages to be more action-oriented in accounts_test.go

* refactor: Update error messages to be action-oriented in accounts_test.go

* refactor: Improve error messages in auth_test.go for better test readability

* refactor: Improve screenshot handling and test result reporting in Playwright test helper

* fix: Improve test reporting with detailed error messages and logging

* refactor: Remove unused runningInCI field from TestHelper struct

* fix: Improve message formatting in Assert method for better reporting

* assertions

* test: Add `Require()` calls to 007 test for improved error handling

* refactor: Update test reporter to include error details and improve HTML rendering

* fix: Properly escape and render base64 screenshot in HTML report

* fix: Correct base64 screenshot rendering in test reporter

* fixed tests + html report

* feat: Add artifact upload for e2e test report

* make lint

* chore: use correct version in user agent

* ci: run e2e after other checks

* chore: remove pre-commit
2025-02-22 20:38:36 +01:00

208 lines
5.9 KiB
Go

package playwright
import (
"fmt"
"os"
"path"
"path/filepath"
"testing"
"time"
"github.com/playwright-community/playwright-go"
"github.com/stretchr/testify/require"
)
// TestHelper wraps common test functionality
type TestHelper struct {
name string
page playwright.Page
browser playwright.Browser
context playwright.BrowserContext
t require.TestingT
}
// NewTestHelper creates a new test helper instance
func NewTestHelper(t require.TestingT, name string) (*TestHelper, error) {
pw, err := playwright.Run()
if err != nil {
return nil, fmt.Errorf("could not start playwright: %v", err)
}
browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
Headless: playwright.Bool(true),
})
if err != nil {
return nil, fmt.Errorf("could not launch browser: %v", err)
}
context, err := browser.NewContext()
if err != nil {
return nil, fmt.Errorf("could not create context: %v", err)
}
page, err := context.NewPage()
if err != nil {
return nil, fmt.Errorf("could not create page: %v", err)
}
return &TestHelper{
name: name,
page: page,
browser: browser,
context: context,
t: t,
}, nil
}
// Require returns a custom assertion object that takes screenshots on failure
func (th *TestHelper) Require() *PlaywrightRequire {
return &PlaywrightRequire{
Assertions: require.New(th.t),
helper: th,
}
}
func (th *TestHelper) HandleError(t *testing.T, screenshotPath string, msg, err string) {
GetReporter().AddResult(t.Name(), false, screenshotPath, msg, err)
t.Error(msg) // Also log the error to the test output
}
func (th *TestHelper) HandleSuccess(t *testing.T, message string) {
GetReporter().AddResult(t.Name(), true, "", message, "")
}
// PlaywrightRequire wraps require.Assertions to add screenshot capability
type PlaywrightRequire struct {
*require.Assertions
helper *TestHelper
}
// captureScreenshot saves a screenshot to the screenshots directory
func (th *TestHelper) captureScreenshot(testName string) string {
timestamp := time.Now().Format("20060102-150405")
tmpDir, err := os.MkdirTemp("", "playwright-screenshots")
if err != nil {
th.t.Errorf("Failed to create temporary directory: %v\n", err)
return ""
}
filePath := filepath.Join(tmpDir, fmt.Sprintf("%s-%s.png", testName, timestamp))
// Get the full path without the filename from `filename` and create the directories
if err := os.MkdirAll(path.Dir(filePath), 0755); err != nil {
th.t.Errorf("Failed to create screenshots directory: %v\n", err)
return ""
}
// Create screenshots directory if it doesn't exist
if err := os.MkdirAll("screenshots", 0755); err != nil {
th.t.Errorf("Failed to create screenshots directory: %v\n", err)
return ""
}
// Take screenshot
if _, err := th.page.Screenshot(playwright.PageScreenshotOptions{
Path: playwright.String(filePath),
FullPage: playwright.Bool(true),
}); err != nil {
th.t.Errorf("Failed to capture screenshot: %v\n", err)
return ""
}
fmt.Printf("Screenshot saved: %s\n", filePath)
return filePath
}
func (pr *PlaywrightRequire) Assert(t *testing.T, assertFn func() error, msgAndArgs ...interface{}) {
err := assertFn()
var msg string
if len(msgAndArgs) > 0 {
if format, ok := msgAndArgs[0].(string); ok && len(msgAndArgs) > 1 {
msg = fmt.Sprintf(format, msgAndArgs[1:]...)
} else {
msg = fmt.Sprint(msgAndArgs...)
}
}
if err == nil {
pr.helper.HandleSuccess(t, msg)
} else {
screenshotPath := pr.helper.captureScreenshot(t.Name())
pr.helper.HandleError(t, screenshotPath, msg, err.Error())
}
}
// True asserts that the specified value is true and takes a screenshot on failure
func (pr *PlaywrightRequire) True(t *testing.T, value bool, msgAndArgs ...interface{}) {
pr.Assert(t, func() error {
var err error
if !value {
err = fmt.Errorf("Expected value to be true but got false in test '%s'", t.Name())
}
return err
}, msgAndArgs...)
pr.Assertions.True(value, msgAndArgs...)
}
// False asserts that the specified value is false and takes a screenshot on failure
func (pr *PlaywrightRequire) False(t *testing.T, value bool, msgAndArgs ...interface{}) {
pr.Assert(t, func() error {
var err error
if value {
err = fmt.Errorf("Expected value to be false but got true in test '%s'", t.Name())
}
return err
}, msgAndArgs...)
pr.Assertions.False(value, msgAndArgs...)
}
// Equal asserts that two objects are equal and takes a screenshot on failure
func (pr *PlaywrightRequire) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
pr.Assert(t, func() error {
var err error
if expected != actual {
err = fmt.Errorf("Expected values to be equal in test '%s':\nexpected: %v\nactual: %v", t.Name(), expected, actual)
}
return err
}, msgAndArgs...)
pr.Assertions.Equal(expected, actual, msgAndArgs...)
}
// NoError asserts that a function returned no error and takes a screenshot on failure
func (pr *PlaywrightRequire) NoError(t *testing.T, err error, msgAndArgs ...interface{}) {
pr.Assert(t, func() error {
var assertErr error
if err != nil {
assertErr = fmt.Errorf("Expected no error but got error in test '%s': %v", t.Name(), err)
}
return assertErr
}, msgAndArgs...)
pr.Assertions.NoError(err, msgAndArgs...)
}
// Error asserts that a function returned an error and takes a screenshot on failure
func (pr *PlaywrightRequire) Error(t *testing.T, err error, msgAndArgs ...interface{}) {
pr.Assert(t, func() error {
var assertErr error
if err == nil {
assertErr = fmt.Errorf("Expected error but got none in test '%s'", t.Name())
}
return assertErr
}, msgAndArgs...)
pr.Assertions.Error(err, msgAndArgs...)
}
// Close cleans up resources and generates the report
func (th *TestHelper) Close() {
if err := GetReporter().GenerateHTML(); err != nil {
fmt.Printf("Failed to generate HTML report: %v\n", err)
}
if th.page != nil {
th.page.Close()
}
if th.context != nil {
th.context.Close()
}
if th.browser != nil {
th.browser.Close()
}
}