diff --git a/browser/browser_context_mapping.go b/browser/browser_context_mapping.go index c60ce7625..5ba69b95a 100644 --- a/browser/browser_context_mapping.go +++ b/browser/browser_context_mapping.go @@ -84,15 +84,23 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin }, "setDefaultNavigationTimeout": bc.SetDefaultNavigationTimeout, "setDefaultTimeout": bc.SetDefaultTimeout, - "setGeolocation": func(geolocation sobek.Value) *sobek.Promise { + "setGeolocation": func(geolocation sobek.Value) (*sobek.Promise, error) { + gl := common.NewGeolocation() + if err := gl.Parse(vu.Context(), geolocation); err != nil { + return nil, fmt.Errorf("parsing geo location: %w", err) + } return k6ext.Promise(vu.Context(), func() (any, error) { - return nil, bc.SetGeolocation(geolocation) //nolint:wrapcheck - }) + return nil, bc.SetGeolocation(gl) + }), nil }, - "setHTTPCredentials": func(httpCredentials sobek.Value) *sobek.Promise { + "setHTTPCredentials": func(httpCredentials sobek.Value) (*sobek.Promise, error) { + pc := common.NewCredentials() + if err := pc.Parse(vu.Context(), httpCredentials); err != nil { + return nil, fmt.Errorf("parsing HTTP credentials: %w", err) + } return k6ext.Promise(vu.Context(), func() (any, error) { - return nil, bc.SetHTTPCredentials(httpCredentials) //nolint:staticcheck,wrapcheck - }) + return nil, bc.SetHTTPCredentials(pc) //nolint:staticcheck + }), nil }, "setOffline": func(offline bool) *sobek.Promise { return k6ext.Promise(vu.Context(), func() (any, error) { diff --git a/browser/mapping_test.go b/browser/mapping_test.go index 6d573124e..f9804da39 100644 --- a/browser/mapping_test.go +++ b/browser/mapping_test.go @@ -295,8 +295,8 @@ type browserContextAPI interface { Pages() []*common.Page SetDefaultNavigationTimeout(timeout int64) SetDefaultTimeout(timeout int64) - SetGeolocation(geolocation sobek.Value) error - SetHTTPCredentials(httpCredentials sobek.Value) error + SetGeolocation(geolocation *common.Geolocation) error + SetHTTPCredentials(httpCredentials *common.Credentials) error SetOffline(offline bool) error WaitForEvent(event string, optsOrPredicate sobek.Value) (any, error) } diff --git a/common/browser_context.go b/common/browser_context.go index d56b71848..65d6bda52 100644 --- a/common/browser_context.go +++ b/common/browser_context.go @@ -13,7 +13,6 @@ import ( "github.com/chromedp/cdproto/network" "github.com/chromedp/cdproto/storage" "github.com/chromedp/cdproto/target" - "github.com/grafana/sobek" "github.com/grafana/xk6-browser/common/js" "github.com/grafana/xk6-browser/k6error" @@ -293,14 +292,9 @@ func (b *BrowserContext) SetDefaultTimeout(timeout int64) { } // SetGeolocation overrides the geo location of the user. -func (b *BrowserContext) SetGeolocation(geolocation sobek.Value) error { +func (b *BrowserContext) SetGeolocation(g *Geolocation) error { b.logger.Debugf("BrowserContext:SetGeolocation", "bctxid:%v", b.id) - g := NewGeolocation() - if err := g.Parse(b.ctx, geolocation); err != nil { - return fmt.Errorf("parsing geo location: %w", err) - } - b.opts.Geolocation = g for _, p := range b.browser.getPages() { if err := p.updateGeolocation(); err != nil { @@ -317,17 +311,12 @@ func (b *BrowserContext) SetGeolocation(geolocation sobek.Value) error { // See for details: // - https://github.com/microsoft/playwright/issues/2196#issuecomment-627134837 // - https://github.com/microsoft/playwright/pull/2763 -func (b *BrowserContext) SetHTTPCredentials(httpCredentials sobek.Value) error { +func (b *BrowserContext) SetHTTPCredentials(hc *Credentials) error { b.logger.Warnf("setHTTPCredentials", "setHTTPCredentials is deprecated."+ " Create a new BrowserContext with httpCredentials instead.") b.logger.Debugf("BrowserContext:SetHTTPCredentials", "bctxid:%v", b.id) - c := NewCredentials() - if err := c.Parse(b.ctx, httpCredentials); err != nil { - return fmt.Errorf("parsing HTTP credentials: %w", err) - } - - b.opts.HttpCredentials = c + b.opts.HttpCredentials = hc for _, p := range b.browser.getPages() { if err := p.updateHTTPCredentials(); err != nil { return fmt.Errorf("setting HTTP credentials in target ID %s: %w", p.targetID, err) diff --git a/common/browser_context_options.go b/common/browser_context_options.go index 995035727..c2926431d 100644 --- a/common/browser_context_options.go +++ b/common/browser_context_options.go @@ -24,39 +24,36 @@ func NewGeolocation() *Geolocation { } // Parse parses the geolocation options. -func (g *Geolocation) Parse(ctx context.Context, opts sobek.Value) error { //nolint:cyclop - rt := k6ext.Runtime(ctx) - longitude := 0.0 - latitude := 0.0 - accuracy := 0.0 +func (g *Geolocation) Parse(ctx context.Context, sopts sobek.Value) error { //nolint:cyclop + var newgl Geolocation - if opts != nil && !sobek.IsUndefined(opts) && !sobek.IsNull(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "accuracy": - accuracy = opts.Get(k).ToFloat() - case "latitude": - latitude = opts.Get(k).ToFloat() - case "longitude": - longitude = opts.Get(k).ToFloat() - } + if !sobekValueExists(sopts) { + return fmt.Errorf("geolocation options are required") + } + + opts := sopts.ToObject(k6ext.Runtime(ctx)) + for _, k := range opts.Keys() { + switch k { + case "accuracy": + newgl.Accurracy = opts.Get(k).ToFloat() + case "latitude": + newgl.Latitude = opts.Get(k).ToFloat() + case "longitude": + newgl.Longitude = opts.Get(k).ToFloat() } } - if longitude < -180 || longitude > 180 { - return fmt.Errorf(`invalid longitude "%.2f": precondition -180 <= LONGITUDE <= 180 failed`, longitude) + if newgl.Longitude < -180 || newgl.Longitude > 180 { + return fmt.Errorf(`invalid longitude "%.2f": precondition -180 <= LONGITUDE <= 180 failed`, newgl.Longitude) } - if latitude < -90 || latitude > 90 { - return fmt.Errorf(`invalid latitude "%.2f": precondition -90 <= LATITUDE <= 90 failed`, latitude) + if newgl.Latitude < -90 || newgl.Latitude > 90 { + return fmt.Errorf(`invalid latitude "%.2f": precondition -90 <= LATITUDE <= 90 failed`, newgl.Latitude) } - if accuracy < 0 { - return fmt.Errorf(`invalid accuracy "%.2f": precondition 0 <= ACCURACY failed`, accuracy) + if newgl.Accurracy < 0 { + return fmt.Errorf(`invalid accuracy "%.2f": precondition 0 <= ACCURACY failed`, newgl.Accurracy) } - g.Accurracy = accuracy - g.Latitude = latitude - g.Longitude = longitude + *g = newgl return nil } diff --git a/common/network_manager.go b/common/network_manager.go index e550f8fc3..84a114183 100644 --- a/common/network_manager.go +++ b/common/network_manager.go @@ -41,16 +41,16 @@ func NewCredentials() *Credentials { // Parse credentials details from a given sobek credentials value. func (c *Credentials) Parse(ctx context.Context, credentials sobek.Value) error { - rt := k6ext.Runtime(ctx) - if credentials != nil && !sobek.IsUndefined(credentials) && !sobek.IsNull(credentials) { - credentials := credentials.ToObject(rt) - for _, k := range credentials.Keys() { - switch k { - case "username": - c.Username = credentials.Get(k).String() - case "password": - c.Password = credentials.Get(k).String() - } + if !sobekValueExists(credentials) { + return errors.New("credentials are required") + } + o := credentials.ToObject(k6ext.Runtime(ctx)) + for _, k := range o.Keys() { + switch k { + case "username": + c.Username = o.Get(k).String() + case "password": + c.Password = o.Get(k).String() } }