From 411145f66ae92fe0bcad7eb1e8341109d1123114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 4 Nov 2024 10:00:37 -0500 Subject: [PATCH 1/6] Desobekify BrowserContext.SetGeolocation --- browser/browser_context_mapping.go | 10 +++++++--- common/browser_context.go | 7 +------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/browser/browser_context_mapping.go b/browser/browser_context_mapping.go index c60ce7625..b06e5fe83 100644 --- a/browser/browser_context_mapping.go +++ b/browser/browser_context_mapping.go @@ -84,10 +84,14 @@ 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 { return k6ext.Promise(vu.Context(), func() (any, error) { diff --git a/common/browser_context.go b/common/browser_context.go index d56b71848..e71347080 100644 --- a/common/browser_context.go +++ b/common/browser_context.go @@ -293,14 +293,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 { From 63372fbfec8a34f1ab2d40410e61fab71b9ef0fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 4 Nov 2024 10:32:51 -0500 Subject: [PATCH 2/6] Update SetGeolocation mapping test --- browser/mapping_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/mapping_test.go b/browser/mapping_test.go index 6d573124e..11a420d56 100644 --- a/browser/mapping_test.go +++ b/browser/mapping_test.go @@ -295,7 +295,7 @@ type browserContextAPI interface { Pages() []*common.Page SetDefaultNavigationTimeout(timeout int64) SetDefaultTimeout(timeout int64) - SetGeolocation(geolocation sobek.Value) error + SetGeolocation(geolocation *common.Geolocation) error SetHTTPCredentials(httpCredentials sobek.Value) error SetOffline(offline bool) error WaitForEvent(event string, optsOrPredicate sobek.Value) (any, error) From 0bf54e60809772939ee34116dd6996a0e90ddd68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 4 Nov 2024 10:15:30 -0500 Subject: [PATCH 3/6] Refactor Geolocation.Parse --- common/browser_context_options.go | 47 +++++++++++++++---------------- 1 file changed, 22 insertions(+), 25 deletions(-) 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 } From a27ed33cd7f252f1a1770691303fc3f63ccf138f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 4 Nov 2024 10:37:58 -0500 Subject: [PATCH 4/6] Desobekify BrowserContext.SetHTTPCredentials --- browser/browser_context_mapping.go | 10 +++++++--- common/browser_context.go | 10 ++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/browser/browser_context_mapping.go b/browser/browser_context_mapping.go index b06e5fe83..5ba69b95a 100644 --- a/browser/browser_context_mapping.go +++ b/browser/browser_context_mapping.go @@ -93,10 +93,14 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin 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/common/browser_context.go b/common/browser_context.go index e71347080..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" @@ -312,17 +311,12 @@ func (b *BrowserContext) SetGeolocation(g *Geolocation) 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) From 4155acc14ac78d5cd0ca9c4e4dbec11652ab99f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 4 Nov 2024 10:38:35 -0500 Subject: [PATCH 5/6] Update SetHTTPCredentials mapping test --- browser/mapping_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/mapping_test.go b/browser/mapping_test.go index 11a420d56..f9804da39 100644 --- a/browser/mapping_test.go +++ b/browser/mapping_test.go @@ -296,7 +296,7 @@ type browserContextAPI interface { SetDefaultNavigationTimeout(timeout int64) SetDefaultTimeout(timeout int64) SetGeolocation(geolocation *common.Geolocation) error - SetHTTPCredentials(httpCredentials sobek.Value) error + SetHTTPCredentials(httpCredentials *common.Credentials) error SetOffline(offline bool) error WaitForEvent(event string, optsOrPredicate sobek.Value) (any, error) } From 1e0a7ca23058ab8f44369e89e8ec56e061bc68a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 4 Nov 2024 10:41:24 -0500 Subject: [PATCH 6/6] Refactor Credentials.Parse --- common/network_manager.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) 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() } }