forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_helpers_test.go
97 lines (75 loc) · 2.28 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
package rod_test
import (
"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 (t T) Monitor() {
b := rod.New().MustConnect()
defer b.MustClose()
p := b.MustPage(t.blank()).MustWaitLoad()
b, cancel := b.WithCancel()
defer cancel()
host := b.Context(t.Context()).ServeMonitor("")
page := t.page.MustNavigate(host)
t.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 := t.Req("", host+"/screenshot").Bytes()
t.Gt(img.Len(), 10)
res := t.Req("", host+"/api/page/test")
t.Eq(400, res.StatusCode)
t.Eq(-32602, gson.New(res.Body).Get("code").Int())
}
func (t T) MonitorErr() {
l := launcher.New()
u := l.MustLaunch()
defer l.Kill()
t.Panic(func() {
rod.New().Monitor("abc").ControlURL(u).MustConnect()
})
}
func (t T) Trace() {
t.Eq(rod.TraceTypeInput.String(), "[input]")
var msg []interface{}
t.browser.Logger(utils.Log(func(list ...interface{}) { msg = list }))
t.browser.Trace(true).SlowMotion(time.Microsecond)
defer func() {
t.browser.Logger(rod.DefaultLogger)
t.browser.Trace(defaults.Trace).SlowMotion(defaults.Slow)
}()
p := t.page.MustNavigate(t.srcFile("fixtures/click.html")).MustWaitLoad()
t.Eq(rod.TraceTypeWait, msg[0])
t.Eq("load", msg[1])
t.Eq(p, msg[2])
el := p.MustElement("button")
el.MustClick()
t.Eq(rod.TraceTypeInput, msg[0])
t.Eq("left click", msg[1])
t.Eq(el, msg[2])
t.mc.stubErr(1, proto.RuntimeCallFunctionOn{})
_ = p.Mouse.Move(10, 10, 1)
}
func (t T) TraceLogs() {
t.browser.Logger(utils.LoggerQuiet)
t.browser.Trace(true)
defer func() {
t.browser.Logger(rod.DefaultLogger)
t.browser.Trace(defaults.Trace)
}()
p := t.page.MustNavigate(t.srcFile("fixtures/click.html"))
el := p.MustElement("button")
el.MustClick()
t.mc.stubErr(1, proto.RuntimeCallFunctionOn{})
p.Overlay(0, 0, 100, 30, "")
}
func (t T) ExposeHelpers() {
p := t.newPage(t.srcFile("fixtures/click.html"))
p.ExposeHelpers(js.ElementR)
t.Eq(p.MustElementByJS(`rod.elementR('button', 'click me')`).MustText(), "click me")
}