Skip to content

Commit

Permalink
Add current iteration's browser getter method to VU
Browse files Browse the repository at this point in the history
Add a higher level helper method to VU in order to retrieve the current
VU iteration's browser instance. This improves the repetitive calls to
getBrowser() method passing the current iteration as input from the
mapping layer.
  • Loading branch information
ka3de committed Jun 22, 2023
1 parent 3ae1db0 commit 42bc287
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 6 additions & 6 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,21 +676,21 @@ func mapBrowser(vu moduleVU) mapping {
rt := vu.Runtime()
return mapping{
"contexts": func() ([]api.BrowserContext, error) {
b, err := vu.getBrowser(vu.State().Iteration)
b, err := vu.browser()
if err != nil {
return nil, err
}
return b.Contexts(), nil
},
"isConnected": func() (bool, error) {
b, err := vu.getBrowser(vu.State().Iteration)
b, err := vu.browser()
if err != nil {
return false, err
}
return b.IsConnected(), nil
},
"newContext": func(opts goja.Value) (*goja.Object, error) {
b, err := vu.getBrowser(vu.State().Iteration)
b, err := vu.browser()
if err != nil {
return nil, err
}
Expand All @@ -702,21 +702,21 @@ func mapBrowser(vu moduleVU) mapping {
return rt.ToValue(m).ToObject(rt), nil
},
"userAgent": func() (string, error) {
b, err := vu.getBrowser(vu.State().Iteration)
b, err := vu.browser()
if err != nil {
return "", err
}
return b.UserAgent(), nil
},
"version": func() (string, error) {
b, err := vu.getBrowser(vu.State().Iteration)
b, err := vu.browser()
if err != nil {
return "", err
}
return b.Version(), nil
},
"newPage": func(opts goja.Value) (mapping, error) {
b, err := vu.getBrowser(vu.State().Iteration)
b, err := vu.browser()
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions browser/modulevu.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package browser
import (
"context"

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/k6ext"

k6modules "go.k6.io/k6/js/modules"
Expand All @@ -19,6 +20,11 @@ type moduleVU struct {
*browserRegistry
}

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

func (vu moduleVU) Context() context.Context {
// promises and inner objects need the VU object to be
// able to use k6-core specific functionality.
Expand Down

0 comments on commit 42bc287

Please sign in to comment.