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