2017-02-09 15:01:40 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var pool *Pool
|
|
|
|
var defaultContext = context.Background()
|
2017-02-15 14:46:07 +00:00
|
|
|
var testdataDir string
|
2017-02-09 15:01:40 +00:00
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
func testAllocate(t *testing.T, path string) *Res {
|
|
|
|
c, err := pool.Allocate(defaultContext)
|
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-02-15 14:46:07 +00:00
|
|
|
testdataDir = "file:" + os.Getenv("GOPATH") + "/src/github.com/knq/chromedp/testdata"
|
|
|
|
|
|
|
|
//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()
|
|
|
|
|
|
|
|
err = pool.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(code)
|
2017-02-12 04:59:33 +00:00
|
|
|
}
|