2017-02-09 15:01:40 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-05 13:14:50 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2017-12-18 00:11:42 +00:00
|
|
|
var (
|
|
|
|
testdataDir string
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
allocCtx context.Context
|
2017-12-18 00:11:42 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
allocOpts = []ExecAllocatorOption{
|
|
|
|
NoFirstRun,
|
|
|
|
NoDefaultBrowserCheck,
|
|
|
|
Headless,
|
2019-03-20 17:41:47 +00:00
|
|
|
DisableGPU,
|
2017-12-18 00:11:42 +00:00
|
|
|
}
|
|
|
|
)
|
2017-02-09 15:01:40 +00:00
|
|
|
|
2019-03-05 13:14:50 +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
|
|
|
|
2019-03-20 16:56:03 +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)
|
|
|
|
}
|
2017-02-12 07:08:40 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
//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)
|
|
|
|
//}
|
2017-02-15 14:46:07 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
return ctx, cancel
|
2017-02-09 15:01:40 +00:00
|
|
|
}
|
2017-02-12 04:59:33 +00:00
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2017-12-27 02:30:28 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2019-03-05 13:14:50 +00:00
|
|
|
panic(fmt.Sprintf("could not get working directory: %v", err))
|
2017-12-27 02:30:28 +00:00
|
|
|
}
|
|
|
|
testdataDir = "file://" + path.Join(wd, "testdata")
|
2017-02-15 14:46:07 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
// it's worth noting that newer versions of chrome (64+) run much faster
|
2017-12-18 00:11:42 +00:00
|
|
|
// than older ones -- same for headless_shell ...
|
2019-03-05 13:14:50 +00:00
|
|
|
if execPath := os.Getenv("CHROMEDP_TEST_RUNNER"); execPath != "" {
|
|
|
|
allocOpts = append(allocOpts, ExecPath(execPath))
|
2017-12-18 00:11:42 +00:00
|
|
|
}
|
|
|
|
// not explicitly needed to be set, as this vastly speeds up unit tests
|
|
|
|
if noSandbox := os.Getenv("CHROMEDP_NO_SANDBOX"); noSandbox != "false" {
|
2019-03-05 13:14:50 +00:00
|
|
|
allocOpts = append(allocOpts, NoSandbox)
|
2017-12-18 00:11:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := NewAllocator(context.Background(), WithExecAllocator(allocOpts...))
|
|
|
|
allocCtx = ctx
|
2017-02-15 14:46:07 +00:00
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
cancel()
|
2019-03-21 15:20:27 +00:00
|
|
|
FromContext(ctx).Allocator.Wait()
|
2017-02-15 14:46:07 +00:00
|
|
|
os.Exit(code)
|
2017-02-12 04:59:33 +00:00
|
|
|
}
|