Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desobekify BrowserContext methods #1504

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions browser/browser_context_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
17 changes: 3 additions & 14 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
47 changes: 22 additions & 25 deletions common/browser_context_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
inancgumus marked this conversation as resolved.
Show resolved Hide resolved

return nil
}
Expand Down
20 changes: 10 additions & 10 deletions common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
Loading