chromedp/chromedp_test.go

73 lines
1.6 KiB
Go
Raw Normal View History

2017-02-09 15:01:40 +00:00
package chromedp
import (
"context"
"fmt"
2017-02-09 15:01:40 +00:00
"os"
2017-12-27 02:30:28 +00:00
"path"
2017-02-09 15:01:40 +00:00
"testing"
)
var (
testdataDir string
allocCtx context.Context
allocOpts = []ExecAllocatorOption{
NoFirstRun,
NoDefaultBrowserCheck,
Headless,
DisableGPU,
}
)
2017-02-09 15:01:40 +00:00
func testAllocate(t *testing.T, path string) (_ context.Context, cancel func()) {
ctx, cancel := NewContext(allocCtx)
2017-02-09 15:01:40 +00:00
// Only navigate if we want a path, otherwise leave the blank page.
if path != "" {
if err := Run(ctx, Navigate(testdataDir+"/"+path)); err != nil {
t.Fatal(err)
}
}
//if err := WithLogf(t.Logf)(c.c); err != nil {
// t.Fatalf("could not set logf: %v", err)
//}
//if err := WithDebugf(t.Logf)(c.c); err != nil {
// t.Fatalf("could not set debugf: %v", err)
//}
//if err := WithErrorf(t.Errorf)(c.c); err != nil {
// t.Fatalf("could not set errorf: %v", err)
//}
return ctx, cancel
2017-02-09 15:01:40 +00:00
}
func TestMain(m *testing.M) {
2017-12-27 02:30:28 +00:00
wd, err := os.Getwd()
if err != nil {
panic(fmt.Sprintf("could not get working directory: %v", err))
2017-12-27 02:30:28 +00:00
}
testdataDir = "file://" + path.Join(wd, "testdata")
// it's worth noting that newer versions of chrome (64+) run much faster
// than older ones -- same for headless_shell ...
if execPath := os.Getenv("CHROMEDP_TEST_RUNNER"); execPath != "" {
allocOpts = append(allocOpts, ExecPath(execPath))
}
// not explicitly needed to be set, as this vastly speeds up unit tests
if noSandbox := os.Getenv("CHROMEDP_NO_SANDBOX"); noSandbox != "false" {
allocOpts = append(allocOpts, NoSandbox)
}
ctx, cancel := NewAllocator(context.Background(), WithExecAllocator(allocOpts...))
allocCtx = ctx
code := m.Run()
cancel()
FromContext(ctx).Wait()
os.Exit(code)
}