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> <card>
<top>Submit a Link</top> <top>Submit a Link</top>
<middle> <middle>
<input name="stream_id" type="hidden" value="1"> <input id="stream_id" name="stream_id" type="hidden" value="1">
<input name="url" type="text" placeholder="Link Url"> <input id="url" name="url" type="text" placeholder="Link Url">
<input name="description" type="text" placeholder="Description"> <input id="description" name="description" type="text" placeholder="Description">
</middle> </middle>
</card> </card>
<buttons> <buttons>
<button type="submit">Send It</button> <button id="submit" type="submit">Send It</button>
</buttons> </buttons>
</form> </form>

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

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

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