-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection_test.go
69 lines (55 loc) · 1.71 KB
/
connection_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
package cri_test
import (
"crypto/tls"
"testing"
"github.com/SKatiyar/cri"
"github.com/SKatiyar/cri/browser"
"github.com/SKatiyar/cri/emulation"
"github.com/SKatiyar/cri/page"
"github.com/stretchr/testify/assert"
)
func TestNewConnection(t *testing.T) {
assert := assert.New(t)
defaultConn, defaultConnErr := cri.NewConnection()
assert.Nil(defaultConnErr)
assert.NotNil(defaultConn, "connection object should returned for default address")
defaultCloseErr := defaultConn.Close()
assert.Nil(defaultCloseErr)
}
func ExampleNewConnection() {
// create connection with default options
cri.NewConnection()
// override the remote address
cri.NewConnection(cri.SetAddress("192.168.0.7:1290"))
// select a target with ssl connection
config := &tls.Config{ /* ...params */ }
cri.NewConnection(cri.SetTargetID("<target-id>"), cri.SetTLSConfig(config))
}
func ExampleConnection_Send() {
// create new connection
conn, _ := cri.NewConnection()
// send Page.enable command
conn.Send(page.Enable, nil, nil)
// send Emulation.setDeviceMetricsOverride command with parameters
conn.Send(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideRequest{}, nil)
// send Browser.getVersion command and decode response
var data browser.GetVersionResponse
conn.Send(browser.GetVersion, nil, &data)
}
func ExampleConnection_On() {
// create new connection
conn, _ := cri.NewConnection()
// listen for page load
// create close channel
closeChn := make(chan struct{})
// start listening
decoder := conn.On(page.LoadEventFired, closeChn)
for {
var params page.LoadEventFiredParams
if readErr := decoder(¶ms); readErr != nil {
// if error occurs, signal close.
close(closeChn)
break
}
}
}