forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser_type.go
122 lines (115 loc) · 3.88 KB
/
browser_type.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package playwright
import (
"fmt"
)
type browserTypeImpl struct {
channelOwner
}
func (b *browserTypeImpl) Name() string {
return b.initializer["name"].(string)
}
func (b *browserTypeImpl) ExecutablePath() string {
return b.initializer["executablePath"].(string)
}
func (b *browserTypeImpl) Launch(options ...BrowserTypeLaunchOptions) (Browser, error) {
overrides := map[string]interface{}{}
if len(options) == 1 && options[0].Env != nil {
overrides["env"] = serializeMapToNameAndValue(options[0].Env)
options[0].Env = nil
}
channel, err := b.channel.Send("launch", overrides, options)
if err != nil {
return nil, fmt.Errorf("could not send message: %w", err)
}
return fromChannel(channel).(*browserImpl), nil
}
func (b *browserTypeImpl) LaunchPersistentContext(userDataDir string, options ...BrowserTypeLaunchPersistentContextOptions) (BrowserContext, error) {
overrides := map[string]interface{}{
"userDataDir": userDataDir,
}
if len(options) == 1 {
if options[0].ExtraHttpHeaders != nil {
overrides["extraHTTPHeaders"] = serializeMapToNameAndValue(options[0].ExtraHttpHeaders)
}
if options[0].Env != nil {
overrides["env"] = serializeMapToNameAndValue(options[0].Env)
options[0].Env = nil
}
if options[0].NoViewport != nil && *options[0].NoViewport {
overrides["noDefaultViewport"] = true
options[0].NoViewport = nil
}
if options[0].RecordHarPath != nil {
recordHar := map[string]interface{}{}
recordHar["path"] = *options[0].RecordHarPath
if options[0].RecordHarOmitContent != nil {
recordHar["omitContent"] = true
}
overrides["recordHar"] = recordHar
} else if options[0].RecordHarOmitContent != nil {
return nil, fmt.Errorf("recordHarOmitContent is set but recordHarPath is nil")
}
}
channel, err := b.channel.Send("launchPersistentContext", overrides, options)
if err != nil {
return nil, fmt.Errorf("could not send message: %w", err)
}
return fromChannel(channel).(*browserContextImpl), nil
}
func (b *browserTypeImpl) Connect(url string, options ...BrowserTypeConnectOptions) (Browser, error) {
overrides := map[string]interface{}{
"wsEndpoint": url,
}
pipe, err := b.channel.Send("connect", overrides, options)
if err != nil {
return nil, err
}
jsonPipe := fromChannel(pipe).(*jsonPipe)
connection := newConnection(jsonPipe.Close)
connection.isRemote = true
var browser *browserImpl
pipeClosed := func() {
for _, context := range browser.contexts {
pages := context.(*browserContextImpl).pages
for _, page := range pages {
page.(*pageImpl).onClose()
}
context.(*browserContextImpl).onClose()
}
browser.onClose()
}
jsonPipe.On("closed", pipeClosed)
connection.onmessage = func(message map[string]interface{}) error {
if err := jsonPipe.Send(message); err != nil {
pipeClosed()
return err
}
return nil
}
jsonPipe.On("message", connection.Dispatch)
playwright := connection.Start()
browser = fromChannel(playwright.initializer["preLaunchedBrowser"]).(*browserImpl)
browser.isConnectedOverWebSocket = true
return browser, nil
}
func (b *browserTypeImpl) ConnectOverCDP(endpointURL string, options ...BrowserTypeConnectOverCDPOptions) (Browser, error) {
overrides := map[string]interface{}{
"endpointURL": endpointURL,
}
response, err := b.channel.SendReturnAsDict("connectOverCDP", overrides, options)
if err != nil {
return nil, err
}
browser := fromChannel(response.(map[string]interface{})["browser"]).(*browserImpl)
if defaultContext, ok := response.(map[string]interface{})["defaultContext"]; ok {
context := fromChannel(defaultContext).(*browserContextImpl)
browser.contexts = append(browser.contexts, context)
context.browser = browser
}
return browser, nil
}
func newBrowserType(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *browserTypeImpl {
bt := &browserTypeImpl{}
bt.createChannelOwner(bt, parent, objectType, guid, initializer)
return bt
}