Files
kopia/tests/htmlui_e2e_test/context_test.go
Jarek Kowalski 9cad0edb53 test(ui): added end-to-end HTML UI test (#1686)
* test(general): refactored parsing of server output

* test(ui): added experimental end-to-end test using chromedp
2022-01-29 01:34:45 -08:00

69 lines
1.4 KiB
Go

package htmluie2e_test
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/chromedp/chromedp"
"github.com/pkg/errors"
)
type TestContext struct {
t *testing.T
snapshotCounter int
screenshotsDir string
expectedDialogText string
dialogResponse bool
downloadFinished chan string
}
func (tc *TestContext) expectDialogText(txt string, respond bool) chromedp.Action {
return chromedp.ActionFunc(func(c context.Context) error {
tc.expectedDialogText = txt
tc.dialogResponse = respond
return nil
})
}
func (tc *TestContext) log(msg string) chromedp.Action {
return chromedp.ActionFunc(func(c context.Context) error {
tc.t.Log(msg)
return nil
})
}
func (tc *TestContext) waitForDownload(waitTime time.Duration) chromedp.Action {
return chromedp.ActionFunc(func(c context.Context) error {
// wait for download
select {
case <-tc.downloadFinished:
tc.t.Logf("file downloaded, good!")
case <-time.After(waitTime):
return errors.Errorf("download did not complete")
}
return nil
})
}
func (tc *TestContext) captureScreenshot(fname string) chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) error {
var b []byte
if err := chromedp.CaptureScreenshot(&b).Do(ctx); err != nil {
return err
}
tc.snapshotCounter++
return os.WriteFile(filepath.Join(tc.screenshotsDir, fmt.Sprintf("%04v.%v.png", tc.snapshotCounter, fname)), b, 0o600)
})
}