-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
58 lines (52 loc) · 1.45 KB
/
main.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
package cdp
import (
"log"
"os"
"sync"
"time"
)
// LogLevelValue is the type for loglevel information.
type LogLevelValue int
const (
// LogBasic records outgoing commands, their replies, and any specified events.
LogBasic = LogLevelValue(0)
// LogDetails records additional details about the reply from the server for a given command/event.
LogDetails = LogLevelValue(1)
// LogAll records everything.
LogAll = LogLevelValue(2)
)
func init() {
file, err := os.Create("cdp.log")
if err != nil {
panic(err)
}
log.SetFlags(log.Llongfile | log.LstdFlags | log.Lmicroseconds)
log.SetOutput(file)
}
// Start prepares required resources to begin automation.
func Start(browser *Browser, logLevel LogLevelValue) *Frame {
// If browser is nil, the chrome protocal testing will still function as long as a browser is already
// properly open and listening for chrome devtools protocol requests on port 9222.
port := 9222
if browser != nil {
port = browser.Port
}
frame := &Frame{
RWMutex: &sync.RWMutex{},
RequestID: RequestID{
RWMutex: &sync.RWMutex{},
Value: 11111,
},
Browser: browser,
Conn: GetWebsocket(browser.Log, port),
CurrentAction: &Action{},
CacheCompleteChan: make(chan struct{}),
ActionChan: make(chan []byte),
CommandChan: make(chan (<-chan time.Time)),
AllComplete: make(chan struct{}),
LogLevel: logLevel,
}
go Write(frame)
go Read(frame)
return frame
}