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.
60 lines
1.7 KiB
60 lines
1.7 KiB
package tests
|
|
|
|
import (
|
|
"context"
|
|
"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
|
|
}
|
|
|
|
func ClickOn(require *require.Assertions, ctx context.Context, id string) {
|
|
err := browser.Run(ctx, browser.WaitVisible(id),)
|
|
require.NoError(err)
|
|
|
|
resp, err := browser.RunResponse(ctx,
|
|
browser.WaitVisible(id),
|
|
browser.Click(id))
|
|
|
|
require.NoError(err)
|
|
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`),
|
|
browser.WaitVisible(`body > footer`),
|
|
browser.WaitVisible(expect))
|
|
require.NoError(err)
|
|
}
|
|
|
|
func WaitFor(require *require.Assertions, ctx context.Context, expect string) {
|
|
err := browser.Run(ctx, browser.WaitVisible(expect))
|
|
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 Run(require *require.Assertions, ctx context.Context, actions ...browser.Action) {
|
|
err := browser.Run(ctx, actions...)
|
|
require.NoError(err)
|
|
}
|
|
|