forked from nicholasvuono/xk6-playwright
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser_wrapper.go
53 lines (42 loc) · 1.24 KB
/
browser_wrapper.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
package playwright
import (
"log"
"github.com/playwright-community/playwright-go"
"go.k6.io/k6/js/modules"
)
type browserWrapper struct {
Browser playwright.Browser
vu modules.VU
}
func newBrowserWrapper(browser playwright.Browser, vu modules.VU) *browserWrapper {
return &browserWrapper{
Browser: browser,
vu: vu,
}
}
func (b *browserWrapper) Close() {
if err := b.Browser.Close(); err != nil {
Throw(b.vu.Runtime(), "Error closing browser", err)
}
}
func (b *browserWrapper) NewPage() *pageWrapper {
page, err := b.Browser.NewPage()
if err != nil {
Throw(b.vu.Runtime(), "Error creating new page", err)
}
return newPageWrapper(page, b.vu)
}
// Cookies wrapper around playwright cookies fetch function
func (b *browserWrapper) Cookies() []*playwright.Cookie {
if b.Browser == nil {
log.Panicf("It looks like there's no browser attached. please call `launch()` before accessing the cookies.")
}
if len(b.Browser.Contexts()) == 0 {
log.Panicf("It looks like there's no browser contexts are attached. please use `goto()` before accessing the cookies.")
}
cookies, err := b.Browser.Contexts()[0].Cookies()
if err != nil {
Throw(b.vu.Runtime(), "Error getting cookies from the default context", err)
}
return cookies
}