From fecfc8ee84f95821f9cea0deaea11e8e1f2cd42c Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 23 Jun 2025 13:08:25 -0400 Subject: [PATCH] chromedp completely fails to reliably click on links when running in non-headless mode on Windows. About 50% of the time it just hangs thinking it clicked when it didn't. So, on Windows need headless only, and then design tests on Linux where it (maybe) works. --- Makefile | 11 +++++-- tests/base_chrome.go | 59 ----------------------------------- tests/base_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 61 deletions(-) delete mode 100644 tests/base_chrome.go create mode 100644 tests/base_test.go diff --git a/Makefile b/Makefile index cf24318..9b95799 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,10 @@ +GO_IS_STUPID_EXE= + +ifeq '$(OS)' 'Windows_NT' + GO_IS_STUPID_EXE=.exe +endif + + build: go build . @@ -11,8 +18,8 @@ docs: go tool pkgsite --open test: - go test zedshaw.games/webapp/tests -c -o runtests - ./runtests + go test zedshaw.games/webapp/tests -c -o runtests$(GO_IS_STUPID_EXE) + ./runtests$(GO_IS_STUPID_EXE) dev: go tool air -build.stop_on_error "true" diff --git a/tests/base_chrome.go b/tests/base_chrome.go deleted file mode 100644 index 1c4f2a1..0000000 --- a/tests/base_chrome.go +++ /dev/null @@ -1,59 +0,0 @@ -package tests - -import ( - "testing" - "context" - "log" - "time" - "github.com/stretchr/testify/assert" - browser "github.com/chromedp/chromedp" -) - -func Run(assert *assert.Assertions, ctx context.Context, actions ...browser.Action) { - err := browser.Run(ctx, actions...) - assert.NoError(err) -} - -func Setup(timeout time.Duration) (context.Context, context.CancelFunc) { - - opts := append(browser.DefaultExecAllocatorOptions[:], - browser.Flag("headless", false),) - - allocCtx, _ := browser.NewExecAllocator(context.Background(), opts...) - - ctx, _ := browser.NewContext(allocCtx, browser.WithLogf(log.Printf)) - - return context.WithTimeout(ctx, timeout * time.Second) -} - -func TestLogin(t *testing.T) { - assert := assert.New(t) - - ctx, cancel := Setup(5); defer cancel() - - Run(assert, ctx, - browser.Navigate(`http://127.0.0.1:5002/login/`), - browser.WaitVisible(`body > footer`), - browser.WaitVisible(`[data-testid="login-page"]`), - browser.SendKeys(`#username`, `zedshaw`), - browser.SendKeys(`#password`, `1234`), - browser.Click(`#login-submit`, browser.NodeVisible), - browser.WaitVisible(`body > footer`), - browser.WaitVisible(`[data-testid="home-page"]`)) -} - -func TestStreamPage(t *testing.T) { - assert := assert.New(t) - - ctx, cancel := Setup(20); defer cancel() - - var example string - - Run(assert, ctx, - browser.Navigate(`http://127.0.0.1:5002`), - browser.WaitVisible(`body > footer`), - browser.Click(`#streams`, browser.NodeVisible), - browser.Text(`#streams-title`, &example)) - - assert.Equal(example, "Past Streams") -} diff --git a/tests/base_test.go b/tests/base_test.go new file mode 100644 index 0000000..f7b490f --- /dev/null +++ b/tests/base_test.go @@ -0,0 +1,74 @@ +package tests + +import ( + "testing" + "context" + "log" + "time" + "github.com/stretchr/testify/assert" + browser "github.com/chromedp/chromedp" +) + +func Run(assert *assert.Assertions, ctx context.Context, actions ...browser.Action) { + err := browser.Run(ctx, actions...) + assert.NoError(err) +} + +func Setup(timeout time.Duration) (context.Context, context.CancelFunc) { + opts := append(browser.DefaultExecAllocatorOptions[:], + browser.Flag("headless", true),) + + ctx, cancel := browser.NewExecAllocator(context.Background(), opts...) + + ctx, _ = browser.NewContext(ctx, browser.WithLogf(log.Printf)) + + ctx, _ = context.WithTimeout(ctx, timeout * time.Second) + + return ctx, cancel +} + +/* +func TestLogin(t *testing.T) { + assert := assert.New(t) + + ctx, cancel := Setup(5); defer cancel() + + Run(assert, ctx, + browser.Navigate(`http://127.0.0.1:5002/login/`), + browser.WaitVisible(`body > footer`), + browser.WaitVisible(`[data-testid="login-page"]`), + browser.SendKeys(`#username`, `zedshaw`), + browser.SendKeys(`#password`, `1234`), + browser.Click(`#login-submit`, browser.NodeVisible), + browser.WaitVisible(`body > footer`), + browser.WaitVisible(`[data-testid="home-page"]`)) +} +*/ + +func TestStreamPage(t *testing.T) { + assert := assert.New(t) + + ctx, cancel := Setup(2); + defer cancel(); + + var example string + + err := browser.Run(ctx, + browser.Navigate(`http://127.0.0.1:5002`), + browser.WaitVisible(`body > footer`), + browser.WaitVisible(`#streams`), + ) + assert.NoError(err) + + resp, err := browser.RunResponse(ctx, browser.Click(`#streams`, browser.ByID)) + assert.Equal(resp.Status, int64(200)) + assert.NoError(err) + + err = browser.Run(ctx, browser.WaitVisible(`#streams-title`)) + assert.NoError(err) + + err = browser.Run(ctx, browser.Text(`#streams-title`, &example)) + assert.NoError(err) + + assert.Equal(example, "Past Streams") +}