-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
dev_helpers_test.go
108 lines (81 loc) · 2.44 KB
/
dev_helpers_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package rod_test
import (
"testing"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/defaults"
"github.com/go-rod/rod/lib/js"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/gson"
)
func TestMonitor(t *testing.T) {
g := setup(t)
b := rod.New().MustConnect()
defer b.MustClose()
p := b.MustPage(g.blank()).MustWaitLoad()
b, cancel := b.WithCancel()
defer cancel()
host := b.Context(g.Context()).ServeMonitor("")
page := g.page.MustNavigate(host)
g.Has(page.MustElement("#targets a").MustParent().MustHTML(), string(p.TargetID))
page.MustNavigate(host + "/page/" + string(p.TargetID))
page.MustWait(`(id) => document.title.includes(id)`, p.TargetID)
img := g.Req("", host+"/screenshot").Bytes()
g.Gt(img.Len(), 10)
res := g.Req("", host+"/api/page/test")
g.Eq(400, res.StatusCode)
g.Eq(-32602, gson.New(res.Body).Get("code").Int())
}
func TestMonitorErr(t *testing.T) {
g := setup(t)
l := launcher.New()
u := l.MustLaunch()
defer l.Kill()
g.Panic(func() {
rod.New().Monitor("abc").ControlURL(u).MustConnect()
})
}
func TestTrace(t *testing.T) {
g := setup(t)
g.Eq(rod.TraceTypeInput.String(), "[input]")
var msg []interface{}
g.browser.Logger(utils.Log(func(list ...interface{}) { msg = list }))
g.browser.Trace(true).SlowMotion(time.Microsecond)
defer func() {
g.browser.Logger(rod.DefaultLogger)
g.browser.Trace(defaults.Trace).SlowMotion(defaults.Slow)
}()
p := g.page.MustNavigate(g.srcFile("fixtures/click.html")).MustWaitLoad()
g.Eq(rod.TraceTypeWait, msg[0])
g.Eq("load", msg[1])
g.Eq(p, msg[2])
el := p.MustElement("button")
el.MustClick()
g.Eq(rod.TraceTypeInput, msg[0])
g.Eq("left click", msg[1])
g.Eq(el, msg[2])
g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})
_ = p.Mouse.MoveTo(proto.NewPoint(10, 10))
}
func TestTraceLogs(t *testing.T) {
g := setup(t)
g.browser.Logger(utils.LoggerQuiet)
g.browser.Trace(true)
defer func() {
g.browser.Logger(rod.DefaultLogger)
g.browser.Trace(defaults.Trace)
}()
p := g.page.MustNavigate(g.srcFile("fixtures/click.html"))
el := p.MustElement("button")
el.MustClick()
g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})
p.Overlay(0, 0, 100, 30, "")
}
func TestExposeHelpers(t *testing.T) {
g := setup(t)
p := g.newPage(g.srcFile("fixtures/click.html"))
p.ExposeHelpers(js.ElementR)
g.Eq(p.MustElementByJS(`() => rod.elementR('button', 'click me')`).MustText(), "click me")
}