A small project that collects various nice things to get started with Go Web Development.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.0 KiB

package tests
import (
"testing"
"context"
"fmt"
"log"
"time"
"runtime"
"github.com/stretchr/testify/require"
browser "github.com/chromedp/chromedp"
"MY/webapp/config"
)
func init() {
config.Load("config.json")
}
type ZedBrowser struct {
ctx context.Context
require *require.Assertions
}
func (z *ZedBrowser) ClickOn(id string) {
err := browser.Run(z.ctx, browser.WaitVisible(id),)
z.require.NoError(err)
resp, err := browser.RunResponse(z.ctx,
browser.WaitVisible(id),
browser.Click(id))
z.require.NoError(err)
z.require.Equal(int64(200), resp.Status)
}
func (z *ZedBrowser) GoTo(path string, expect string) {
url := fmt.Sprintf(`http://%s%s`, config.Settings.Server.HostPort, path)
fmt.Println("URL: ", url)
err := browser.Run(z.ctx,
browser.Navigate(url),
browser.WaitVisible(`body > footer`),
browser.WaitVisible(expect))
z.require.NoError(err)
}
func (z *ZedBrowser) WaitFor(expect string) {
err := browser.Run(z.ctx, browser.WaitVisible(expect))
z.require.NoError(err)
}
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 (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
}