2017-02-09 15:01:40 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"os"
|
2017-12-18 02:55:04 +00:00
|
|
|
"os/exec"
|
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
|
|
|
"time"
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
"github.com/chromedp/chromedp/runner"
|
2017-02-09 15:01:40 +00:00
|
|
|
)
|
|
|
|
|
2017-12-18 00:11:42 +00:00
|
|
|
var (
|
|
|
|
pool *Pool
|
|
|
|
testdataDir string
|
|
|
|
|
|
|
|
defaultContext, defaultCancel = context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
cliOpts = []runner.CommandLineOption{
|
|
|
|
runner.NoDefaultBrowserCheck,
|
|
|
|
runner.NoFirstRun,
|
|
|
|
}
|
|
|
|
)
|
2017-02-09 15:01:40 +00:00
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
func testAllocate(t *testing.T, path string) *Res {
|
2017-12-18 00:11:42 +00:00
|
|
|
c, err := pool.Allocate(defaultContext, cliOpts...)
|
2017-02-09 15:01:40 +00:00
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
t.Fatalf("could not allocate from pool: %v", err)
|
2017-02-09 15:01:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
err = WithLogf(t.Logf)(c.c)
|
2017-02-09 15:01:40 +00:00
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
t.Fatalf("could not set logf: %v", err)
|
2017-02-09 15:01:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
err = WithDebugf(t.Logf)(c.c)
|
2017-02-09 15:01:40 +00:00
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
t.Fatalf("could not set debugf: %v", err)
|
2017-02-09 15:01:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
err = WithErrorf(t.Errorf)(c.c)
|
2017-02-09 15:01:40 +00:00
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
t.Fatalf("could not set errorf: %v", err)
|
2017-02-09 15:01:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
h := c.c.GetHandlerByIndex(0)
|
|
|
|
th, ok := h.(*TargetHandler)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("handler is invalid type")
|
2017-02-09 15:01:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
th.logf = t.Logf
|
|
|
|
th.debugf = t.Logf
|
|
|
|
th.errorf = func(s string, v ...interface{}) {
|
|
|
|
t.Logf("target handler error: "+s, v...)
|
2017-02-12 07:08:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
if path != "" {
|
|
|
|
err = c.Run(defaultContext, Navigate(testdataDir+"/"+path))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not navigate to testdata/%s: %v", path, err)
|
|
|
|
}
|
2017-02-09 15:01:40 +00:00
|
|
|
}
|
2017-02-15 14:46:07 +00:00
|
|
|
|
|
|
|
return c
|
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-02-12 04:59:33 +00:00
|
|
|
var err error
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not get working directory: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
testdataDir = "file://" + path.Join(wd, "testdata")
|
2017-02-15 14:46:07 +00:00
|
|
|
|
2017-12-18 00:11:42 +00:00
|
|
|
// its worth noting that newer versions of chrome (64+) run much faster
|
|
|
|
// than older ones -- same for headless_shell ...
|
2017-12-18 02:55:04 +00:00
|
|
|
execPath := runner.DefaultChromePath
|
2017-12-18 00:11:42 +00:00
|
|
|
if testRunner := os.Getenv("CHROMEDP_TEST_RUNNER"); testRunner != "" {
|
2017-12-18 02:55:04 +00:00
|
|
|
execPath = testRunner
|
|
|
|
} else {
|
|
|
|
// use headless_shell, if on path
|
|
|
|
var hsPath string
|
|
|
|
hsPath, err = exec.LookPath("headless_shell")
|
|
|
|
if err == nil {
|
|
|
|
execPath = hsPath
|
|
|
|
}
|
2017-12-18 00:11:42 +00:00
|
|
|
}
|
2017-12-18 02:55:04 +00:00
|
|
|
cliOpts = append(cliOpts, runner.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" {
|
|
|
|
cliOpts = append(cliOpts, runner.NoSandbox)
|
|
|
|
}
|
|
|
|
// must be explicitly set, as disabling gpu slows unit tests
|
|
|
|
if disableGPU := os.Getenv("CHROMEDP_DISABLE_GPU"); disableGPU != "" && disableGPU != "false" {
|
|
|
|
cliOpts = append(cliOpts, runner.DisableGPU)
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetTimeout := os.Getenv("CHROMEDP_TARGET_TIMEOUT"); targetTimeout != "" {
|
|
|
|
defaultNewTargetTimeout, _ = time.ParseDuration(targetTimeout)
|
|
|
|
}
|
|
|
|
if defaultNewTargetTimeout == 0 {
|
|
|
|
defaultNewTargetTimeout = 30 * time.Second
|
|
|
|
}
|
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
//pool, err = NewPool(PoolLog(log.Printf, log.Printf, log.Printf))
|
|
|
|
pool, err = NewPool()
|
2017-02-12 04:59:33 +00:00
|
|
|
if err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
log.Fatal(err)
|
2017-02-12 04:59:33 +00:00
|
|
|
}
|
2017-02-15 14:46:07 +00:00
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
2017-02-18 06:11:46 +00:00
|
|
|
defaultCancel()
|
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
err = pool.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(code)
|
2017-02-12 04:59:33 +00:00
|
|
|
}
|