forked from chromedp/chromedp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chromedp_test.go
78 lines (61 loc) · 1.41 KB
/
chromedp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package chromedp
import (
"context"
"log"
"os"
"testing"
)
var pool *Pool
var defaultContext, defaultCancel = context.WithCancel(context.Background())
var testdataDir string
func testAllocate(t *testing.T, path string) *Res {
c, err := pool.Allocate(defaultContext)
if err != nil {
t.Fatalf("could not allocate from pool: %v", err)
}
err = WithLogf(t.Logf)(c.c)
if err != nil {
t.Fatalf("could not set logf: %v", err)
}
err = WithDebugf(t.Logf)(c.c)
if err != nil {
t.Fatalf("could not set debugf: %v", err)
}
err = WithErrorf(t.Errorf)(c.c)
if err != nil {
t.Fatalf("could not set errorf: %v", err)
}
h := c.c.GetHandlerByIndex(0)
th, ok := h.(*TargetHandler)
if !ok {
t.Fatalf("handler is invalid type")
}
th.logf = t.Logf
th.debugf = t.Logf
th.errorf = func(s string, v ...interface{}) {
t.Logf("target handler error: "+s, v...)
}
if path != "" {
err = c.Run(defaultContext, Navigate(testdataDir+"/"+path))
if err != nil {
t.Fatalf("could not navigate to testdata/%s: %v", path, err)
}
}
return c
}
func TestMain(m *testing.M) {
var err error
testdataDir = "file://" + os.Getenv("GOPATH") + "/src/github.com/knq/chromedp/testdata"
//pool, err = NewPool(PoolLog(log.Printf, log.Printf, log.Printf))
pool, err = NewPool()
if err != nil {
log.Fatal(err)
}
code := m.Run()
defaultCancel()
err = pool.Shutdown()
if err != nil {
log.Fatal(err)
}
os.Exit(code)
}