Nicer testing setup with my own interface and methods that abstract away the weird browser API.

main
Zed A. Shaw 7 days ago
parent 77e4c3fa5b
commit 736095a5aa
  1. 8
      pages/live/index.html
  2. 8
      public/live/index.html
  3. 84
      tests/base_test.go
  4. 95
      tests/tools.go

@ -23,13 +23,13 @@
<card>
<top>Submit a Link</top>
<middle>
<input name="stream_id" type="hidden" value="1">
<input name="url" type="text" placeholder="Link Url">
<input name="description" type="text" placeholder="Description">
<input id="stream_id" name="stream_id" type="hidden" value="1">
<input id="url" name="url" type="text" placeholder="Link Url">
<input id="description" name="description" type="text" placeholder="Description">
</middle>
</card>
<buttons>
<button type="submit">Send It</button>
<button id="submit" type="submit">Send It</button>
</buttons>
</form>

@ -48,13 +48,13 @@
<card>
<top>Submit a Link</top>
<middle>
<input name="stream_id" type="hidden" value="1">
<input name="url" type="text" placeholder="Link Url">
<input name="description" type="text" placeholder="Description">
<input id="stream_id" name="stream_id" type="hidden" value="1">
<input id="url" name="url" type="text" placeholder="Link Url">
<input id="description" name="description" type="text" placeholder="Description">
</middle>
</card>
<buttons>
<button type="submit">Send It</button>
<button id="submit" type="submit">Send It</button>
</buttons>
</form>

@ -2,8 +2,7 @@ package tests
import (
"testing"
"github.com/stretchr/testify/require"
browser "github.com/chromedp/chromedp"
// "github.com/stretchr/testify/require"
"zedshaw.games/webapp/data"
sq "github.com/Masterminds/squirrel"
)
@ -14,71 +13,58 @@ func deleteTestUser(username string) {
}
func TestLogin(t *testing.T) {
require := require.New(t)
deleteTestUser("testerzed")
ctx, cancel := Setup(5); defer cancel()
Run(require, ctx,
browser.Navigate(`http://127.0.0.1:5002/register/`),
browser.WaitVisible(`body > footer`),
browser.WaitVisible(`[data-testid="register-index-page"]`),
browser.SendKeys(`#username`, `testerzed`),
browser.SendKeys(`#email`, `zed@test.com`),
browser.SendKeys(`#password`, `iamdumb`),
browser.Click(`#register-submit`, browser.NodeVisible),
browser.WaitVisible(`body > footer`),
browser.WaitVisible(`[data-testid="login-index-page"]`))
Run(require, ctx,
browser.Navigate(`http://127.0.0.1:5002/login/`),
browser.WaitVisible(`body > footer`),
browser.WaitVisible(`[data-testid="login-index-page"]`),
browser.SendKeys(`#username`, `testerzed`),
browser.SendKeys(`#password`, `iamdumb`),
browser.Click(`#login-submit`, browser.NodeVisible),
browser.WaitVisible(`body > footer`),
browser.WaitVisible(`[data-testid="index-page"]`))
z, cancel := Setup(t, 5)
defer cancel()
z.GoTo("/register/", `[data-testid="register-index-page"]`)
z.TypeIn(`#username`, `testerzed`)
z.TypeIn(`#email`, `zed@test.com`)
z.TypeIn(`#password`, `iamdumbass`)
z.ClickOn(`#register-submit`)
z.WaitFor(`[data-testid="login-index-page"]`)
z.GoTo("/login/", `[data-testid="login-index-page"]`)
z.TypeIn(`#username`, `testerzed`)
z.TypeIn(`#password`, `iamdumbass`)
z.ClickOn(`#login-submit`)
z.WaitFor(`[data-testid="index-page"]`)
/// delete the user here
deleteTestUser("testerzed")
}
func TestStreamsPage(t *testing.T) {
require := require.New(t)
z, cancel := Setup(t, 2)
defer cancel()
ctx, cancel := Setup(2);
defer cancel();
GoTo(require, ctx, "/", `#streams`)
ClickOn(require, ctx, `#streams`)
WaitFor(require, ctx, `[data-testid="stream-index-page"]`)
ExpectText(require, ctx, `#page-title`, "Past Streams")
z.GoTo("/", `#streams`)
z.ClickOn(`#streams`)
z.WaitFor(`[data-testid="stream-index-page"]`)
z.ExpectText(`#page-title`, "Past Streams")
}
func TestLivePage(t *testing.T) {
require := require.New(t)
ctx, cancel := Setup(2);
z, cancel := Setup(t, 2);
defer cancel();
GoTo(require, ctx, "/", `#live`)
ClickOn(require, ctx, `#live`)
WaitFor(require, ctx, `[data-testid="live-index-page"]`)
z.GoTo("/", `#live`)
z.ClickOn(`#live`)
z.WaitFor(`[data-testid="live-index-page"]`)
z.TypeIn("#url", "https://test.com/")
z.TypeIn("#description", "A test URL.")
z.ClickOn(`#submit`)
}
func TestGamePage(t *testing.T) {
require := require.New(t)
ctx, cancel := Setup(2);
z, cancel := Setup(t, 2);
defer cancel();
GoTo(require, ctx, "/", `#game`)
ClickOn(require, ctx, `#game`)
WaitFor(require, ctx, `[data-testid="game-index-page"]`)
ExpectText(require, ctx, `#page-title`, "Zed's Trash Ass Games")
ClickOn(require, ctx, `[data-testid="game-link"]`)
z.GoTo("/", `#game`)
z.ClickOn(`#game`)
z.WaitFor(`[data-testid="game-index-page"]`)
z.ExpectText(`#page-title`, "Zed's Trash Ass Games")
z.ClickOn(`[data-testid="game-link"]`)
}
func TestMain(m *testing.M) {

@ -1,60 +1,75 @@
package tests
import (
"context"
"log"
"time"
"runtime"
"github.com/stretchr/testify/require"
browser "github.com/chromedp/chromedp"
"testing"
"context"
"fmt"
"log"
"time"
"runtime"
"github.com/stretchr/testify/require"
browser "github.com/chromedp/chromedp"
)
func Setup(timeout time.Duration) (context.Context, context.CancelFunc) {
opts := append(browser.DefaultExecAllocatorOptions[:],
browser.Flag("headless", runtime.GOOS == "windows"),)
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
type ZedBrowser struct {
ctx context.Context
require *require.Assertions
}
func ClickOn(require *require.Assertions, ctx context.Context, id string) {
err := browser.Run(ctx, browser.WaitVisible(id),)
require.NoError(err)
func (z *ZedBrowser) ClickOn(id string) {
err := browser.Run(z.ctx, browser.WaitVisible(id),)
z.require.NoError(err)
resp, err := browser.RunResponse(ctx,
browser.WaitVisible(id),
browser.Click(id))
resp, err := browser.RunResponse(z.ctx,
browser.WaitVisible(id),
browser.Click(id))
require.NoError(err)
require.Equal(resp.Status, int64(200))
z.require.NoError(err)
z.require.Equal(resp.Status, int64(200))
}
func GoTo(require *require.Assertions, ctx context.Context, path string, expect string) {
err := browser.Run(ctx,
browser.Navigate(`http://127.0.0.1:5002`),
func (z *ZedBrowser) GoTo(path string, expect string) {
err := browser.Run(z.ctx,
browser.Navigate(fmt.Sprintf(`http://127.0.0.1:5002%s`, path)),
browser.WaitVisible(`body > footer`),
browser.WaitVisible(expect))
require.NoError(err)
z.require.NoError(err)
}
func WaitFor(require *require.Assertions, ctx context.Context, expect string) {
err := browser.Run(ctx, browser.WaitVisible(expect))
require.NoError(err)
func (z *ZedBrowser) WaitFor(expect string) {
err := browser.Run(z.ctx, browser.WaitVisible(expect))
z.require.NoError(err)
}
func ExpectText(require *require.Assertions, ctx context.Context, target string, expect string) {
var extracted string
err := browser.Run(ctx, browser.Text(target, &extracted))
require.NoError(err)
require.Equal(expect, extracted)
func (z *ZedBrowser) ExpectText(target string, expect string) {
var extracted string
err := browser.Run(z.ctx, browser.Text(target, &extracted))
z.require.NoError(err)
z.require.Equal(expect, extracted)
}
func Run(require *require.Assertions, ctx context.Context, actions ...browser.Action) {
err := browser.Run(ctx, actions...)
require.NoError(err)
func (z *ZedBrowser) Run(actions ...browser.Action) {
err := browser.Run(z.ctx, actions...)
z.require.NoError(err)
}
func (z *ZedBrowser) TypeIn(id string, text string) {
z.WaitFor(id)
err := browser.Run(z.ctx, browser.SendKeys(id, text))
z.require.NoError(err)
}
func Setup(t *testing.T, timeout time.Duration) (*ZedBrowser, context.CancelFunc) {
opts := append(browser.DefaultExecAllocatorOptions[:],
browser.Flag("headless", runtime.GOOS == "windows"),)
ctx, cancel := browser.NewExecAllocator(context.Background(), opts...)
ctx, _ = browser.NewContext(ctx, browser.WithLogf(log.Printf))
ctx, _ = context.WithTimeout(ctx, timeout * time.Second)
req := require.New(t)
return &ZedBrowser{ctx, req}, cancel
}

Loading…
Cancel
Save