diff options
author | Grail Finder (aider) <wohilas@gmail.com> | 2025-03-29 16:13:25 +0300 |
---|---|---|
committer | Grail Finder (aider) <wohilas@gmail.com> | 2025-03-29 16:13:25 +0300 |
commit | dc27342f9d28a82045d691351f38ce7f42712e79 (patch) | |
tree | d13ee70bb55bc5a85ba5da4d4808f63df95b7ee2 | |
parent | 6ea69ada199e71da462b9926aca87107d8258ffd (diff) |
test: add playwright e2e test for question flow
-rw-r--r-- | internal/handlers/handlers_test.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/internal/handlers/handlers_test.go b/internal/handlers/handlers_test.go new file mode 100644 index 0000000..a597d7a --- /dev/null +++ b/internal/handlers/handlers_test.go @@ -0,0 +1,76 @@ +package handlers + +import ( + "context" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/playwright-community/playwright-go" + "github.com/stretchr/testify/assert" +) + +func TestQuestionFlow(t *testing.T) { + assert := assert.New(t) + + // Setup test server + h := NewHandlers( + config.Config{}, + slog.New(slog.NewJSONHandler(os.Stdout, nil), + &MockRepo{}, + ) + ts := httptest.NewServer(http.HandlerFunc(h.MainPage)) + defer ts.Close() + + // Setup Playwright + pw, err := playwright.Run() + assert.NoError(err) + defer pw.Stop() + + browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{ + Headless: playwright.Bool(true), // Change to false for debugging + }) + assert.NoError(err) + defer browser.Close() + + page, err := browser.NewPage() + assert.NoError(err) + + // Test 1: Load page and verify question + _, err = page.Goto(ts.URL) + assert.NoError(err) + + questionText, err := page.Locator(".question-text").TextContent() + assert.NoError(err) + assert.Contains(questionText, "Zweifel") + + // Verify all options are present + options := []string{"Haben", "Hast", "Hat", "Habt"} + for _, opt := range options { + _, err := page.Locator("button:has-text('" + opt + "')").TextContent() + assert.NoError(err) + } + + // Test 2: Click correct answer + err = page.Locator("button:has-text('Hast')").Click() + assert.NoError(err) + + feedback, err := page.Locator("#feedback").TextContent() + assert.NoError(err) + assert.Contains(feedback, "Correct") + + // Test 3: Click wrong answer + err = page.Locator("button:has-text('Haben')").Click() + assert.NoError(err) + + feedback, err = page.Locator("#feedback").TextContent() + assert.NoError(err) + assert.Contains(feedback, "Wrong") +} + +type MockRepo struct{} + +func (m *MockRepo) DBGetDefaultsMap() (map[string]string, error) { + return map[string]string{}, nil +} |