main
parent
77e4c3fa5b
commit
736095a5aa
@ -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…
Reference in new issue