Skip to content

Commit

Permalink
Use concrete Browser
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Oct 6, 2023
1 parent e3b4e06 commit 7678fe9
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 40 deletions.
20 changes: 16 additions & 4 deletions browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func customMappings() map[string]string {
"FrameAPI.id": "",
"FrameAPI.loaderID": "",
"JSHandleAPI.objectID": "",
"BrowserAPI.close": "",
"browserAPI.close": "",
"FrameAPI.evaluateWithContext": "",
// TODO: browser.on method is unexposed until more event
// types other than 'disconnect' are supported.
// See: https://github.com/grafana/xk6-browser/issues/913
"BrowserAPI.on": "",
"browserAPI.on": "",
}
}

Expand Down Expand Up @@ -123,7 +123,7 @@ func TestMappings(t *testing.T) {

for name, tt := range map[string]test{
"browser": {
apiInterface: (*common.BrowserAPI)(nil),
apiInterface: (*browserAPI)(nil),
mapp: func() mapping {
return mapBrowser(moduleVU{VU: vu})
},
Expand Down Expand Up @@ -244,11 +244,23 @@ func isCustomMapping(customMappings map[string]string, typ, method string) (stri
// JavaScript API definitions.
// ----------------------------------------------------------------------------

// browserAPI is the public interface of a CDP browser.
type browserAPI interface {
Close()
Context() *common.BrowserContext
IsConnected() bool
NewContext(opts goja.Value) (*common.BrowserContext, error)
NewPage(opts goja.Value) (common.PageAPI, error)
On(string) (bool, error)
UserAgent() string
Version() string
}

// browserContextAPI is the public interface of a CDP browser context.
type browserContextAPI interface {
AddCookies(cookies []*common.Cookie) error
AddInitScript(script goja.Value, arg goja.Value) error
Browser() common.BrowserAPI
Browser() *common.Browser
ClearCookies() error
ClearPermissions()
Close()
Expand Down
2 changes: 1 addition & 1 deletion browser/modulevu.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type moduleVU struct {
}

// browser returns the VU browser instance for the current iteration.
func (vu moduleVU) browser() (common.BrowserAPI, error) {
func (vu moduleVU) browser() (*common.Browser, error) {
return vu.browserRegistry.getBrowser(vu.State().Iteration)
}

Expand Down
14 changes: 7 additions & 7 deletions browser/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,21 @@ type browserRegistry struct {
vu k6modules.VU

mu sync.RWMutex
m map[int64]common.BrowserAPI
m map[int64]*common.Browser

buildFn browserBuildFunc

stopped atomic.Bool // testing purposes
}

type browserBuildFunc func(ctx context.Context) (common.BrowserAPI, error)
type browserBuildFunc func(ctx context.Context) (*common.Browser, error)

func newBrowserRegistry(vu k6modules.VU, remote *remoteRegistry, pids *pidRegistry) *browserRegistry {
bt := chromium.NewBrowserType(vu)
builder := func(ctx context.Context) (common.BrowserAPI, error) {
builder := func(ctx context.Context) (*common.Browser, error) {
var (
err error
b common.BrowserAPI
b *common.Browser
wsURL, isRemoteBrowser = remote.isRemoteBrowser()
)

Expand All @@ -206,7 +206,7 @@ func newBrowserRegistry(vu k6modules.VU, remote *remoteRegistry, pids *pidRegist

r := &browserRegistry{
vu: vu,
m: make(map[int64]common.BrowserAPI),
m: make(map[int64]*common.Browser),
buildFn: builder,
}

Expand Down Expand Up @@ -298,14 +298,14 @@ func (r *browserRegistry) handleExitEvent(exitCh <-chan *k6event.Event, unsubscr
r.clear()
}

func (r *browserRegistry) setBrowser(id int64, b common.BrowserAPI) {
func (r *browserRegistry) setBrowser(id int64, b *common.Browser) {
r.mu.Lock()
defer r.mu.Unlock()

r.m[id] = b
}

func (r *browserRegistry) getBrowser(id int64) (common.BrowserAPI, error) {
func (r *browserRegistry) getBrowser(id int64) (*common.Browser, error) {
r.mu.RLock()
defer r.mu.RUnlock()

Expand Down
6 changes: 3 additions & 3 deletions chromium/browser_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (b *BrowserType) initContext(ctx context.Context) context.Context {
}

// Connect attaches k6 browser to an existing browser instance.
func (b *BrowserType) Connect(ctx context.Context, wsEndpoint string) (common.BrowserAPI, error) {
func (b *BrowserType) Connect(ctx context.Context, wsEndpoint string) (*common.Browser, error) {
ctx, browserOpts, logger, err := b.init(ctx, true)
if err != nil {
return nil, fmt.Errorf("initializing browser type: %w", err)
Expand Down Expand Up @@ -152,7 +152,7 @@ func (b *BrowserType) link(

// Launch allocates a new Chrome browser process and returns a new Browser value,
// which can be used for controlling the Chrome browser.
func (b *BrowserType) Launch(ctx context.Context) (_ common.BrowserAPI, browserProcessID int, _ error) {
func (b *BrowserType) Launch(ctx context.Context) (_ *common.Browser, browserProcessID int, _ error) {
ctx, browserOpts, logger, err := b.init(ctx, false)
if err != nil {
return nil, 0, fmt.Errorf("initializing browser type: %w", err)
Expand Down Expand Up @@ -211,7 +211,7 @@ func (b *BrowserType) tmpdir() string {
}

// LaunchPersistentContext launches the browser with persistent storage.
func (b *BrowserType) LaunchPersistentContext(_ string, _ goja.Value) common.BrowserAPI {
func (b *BrowserType) LaunchPersistentContext(_ string, _ goja.Value) *common.Browser {
rt := b.vu.Runtime()
k6common.Throw(rt, errors.New("BrowserType.LaunchPersistentContext(userDataDir, opts) has not been implemented yet"))
return nil
Expand Down
12 changes: 0 additions & 12 deletions common/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,6 @@ const (
CookieSameSiteNone CookieSameSite = "None"
)

// BrowserAPI is the public interface of a CDP browser.
type BrowserAPI interface {
Close()
Context() *BrowserContext
IsConnected() bool
NewContext(opts goja.Value) (*BrowserContext, error)
NewPage(opts goja.Value) (PageAPI, error)
On(string) (bool, error)
UserAgent() string
Version() string
}

// ConsoleMessageAPI represents a page console message.
type ConsoleMessageAPI struct {
// Args represent the list of arguments passed to a console function call.
Expand Down
5 changes: 0 additions & 5 deletions common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
"github.com/gorilla/websocket"
)

// Ensure Browser implements the EventEmitter and Browser interfaces.
var (
_ BrowserAPI = &Browser{}
)

const (
BrowserStateOpen int64 = iota
BrowserStateClosed
Expand Down
2 changes: 1 addition & 1 deletion common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (b *BrowserContext) applyAllInitScripts(p *Page) error {
}

// Browser returns the browser instance that this browser context belongs to.
func (b *BrowserContext) Browser() BrowserAPI {
func (b *BrowserContext) Browser() *Browser {
return b.browser
}

Expand Down
10 changes: 3 additions & 7 deletions tests/test_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,16 @@ func newTestBrowser(tb testing.TB, opts ...func(*testBrowser)) *testBrowser {
if err != nil {
tb.Fatalf("testBrowser: %v", err)
}
cb, ok := b.(*common.Browser)
if !ok {
tb.Fatalf("testBrowser: unexpected browser %T", b)
}
tbr.Browser = cb
tbr.Browser = b
tbr.ctx = tbr.browserType.Ctx
tbr.pid = pid
tbr.wsURL = cb.WsURL()
tbr.wsURL = b.WsURL()
tb.Cleanup(func() {
select {
case <-tbr.vu.Context().Done():
default:
if !tbr.skipClose {
cb.Close()
b.Close()
}
}
})
Expand Down

0 comments on commit 7678fe9

Please sign in to comment.