-
-
Notifications
You must be signed in to change notification settings - Fork 333
/
example_test.go
57 lines (45 loc) · 1.22 KB
/
example_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
package cdp_test
import (
"context"
"fmt"
"github.com/go-rod/rod/lib/cdp"
"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 ExampleClient() {
ctx := context.Background()
// launch a browser
url := launcher.New().MustLaunch()
// create a controller
client := cdp.New().Start(cdp.MustConnectWS(url))
go func() {
for range client.Event() {
// you must consume the events
utils.Noop()
}
}()
// Such as call this endpoint on the api doc:
// https://chromedevtools.github.io/devtools-protocol/tot/Page#method-navigate
// This will create a new tab and navigate to the test.com
res, err := client.Call(ctx, "", "Target.createTarget", map[string]string{
"url": "http://test.com",
})
utils.E(err)
fmt.Println(len(gson.New(res).Get("targetId").Str()))
// close browser by using the proto lib to encode json
_ = proto.BrowserClose{}.Call(client)
// Output: 32
}
func Example_customize_cdp_log() {
ws := cdp.MustConnectWS(launcher.New().MustLaunch())
cdp.New().
Logger(utils.Log(func(args ...interface{}) {
switch v := args[0].(type) {
case *cdp.Request:
fmt.Printf("id: %d", v.ID)
}
})).
Start(ws)
}