diff --git a/go.mod b/go.mod index 0e20fbaa705..7fda9544a6c 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/golang/protobuf v1.5.4 github.com/gorilla/websocket v1.5.1 github.com/grafana/sobek v0.0.0-20240626131919-5be1e93c7efc - github.com/grafana/xk6-browser v1.6.0 + github.com/grafana/xk6-browser v1.6.1-0.20240701105714-29f6ef3049fe github.com/grafana/xk6-dashboard v0.7.4 github.com/grafana/xk6-output-prometheus-remote v0.4.0 github.com/grafana/xk6-redis v0.3.0 diff --git a/go.sum b/go.sum index 4b6c2cb0b11..bc848f19a66 100644 --- a/go.sum +++ b/go.sum @@ -42,7 +42,7 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/dlclark/regexp2 v1.9.0 h1:pTK/l/3qYIKaRXuHnEnIf7Y5NxfRPfpb7dis6/gdlVI= github.com/dlclark/regexp2 v1.9.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= -github.com/dop251/goja v0.0.0-20240516125602-ccbae20bcec2 h1:OFTHt+yJDo/uaIKMGjEKzc3DGhrpQZoqvMUIloZv6ZY= +github.com/dop251/goja v0.0.0-20240610225006-393f6d42497b h1:fMKDnOAKCGXSZBphY/ilLtu7cmwMnjqE+xJxUkfkpCY= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -84,8 +84,8 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/ github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= github.com/grafana/sobek v0.0.0-20240626131919-5be1e93c7efc h1:4aFnXIV4UQu+5NL3ZlmYxTZ5hVUXu/U/TKsvBUt5wsA= github.com/grafana/sobek v0.0.0-20240626131919-5be1e93c7efc/go.mod h1:tUEHKWaMrxFGrMgjeAH85OEceCGQiSl6a/6Wckj/Vf4= -github.com/grafana/xk6-browser v1.6.0 h1:x8ZfBwiUJRRKNEw+Asr5ae9o2gFvYU1Ll/4dDMNIPZ8= -github.com/grafana/xk6-browser v1.6.0/go.mod h1:xLaGGhTMHIRsMvkVWFYh9RPy87kG2n4L4Or6DeI8U+o= +github.com/grafana/xk6-browser v1.6.1-0.20240701105714-29f6ef3049fe h1:uE7e1Lzris7YuM5mRLulkUGBIL9WmPHvLrA2RyZLyy8= +github.com/grafana/xk6-browser v1.6.1-0.20240701105714-29f6ef3049fe/go.mod h1:TNngUsbmV3I5NDVklGxjpAR2znEjYEsBtHQirGw8nAI= github.com/grafana/xk6-dashboard v0.7.4 h1:0ZRPTAXW+6A3Xqq/a/OaIZhxUt1SOMwUFff0IPwBHrs= github.com/grafana/xk6-dashboard v0.7.4/go.mod h1:300QyQ+OQAYz/L/AzB5tKzPeBY5eKh2wl1NsRmCbsx4= github.com/grafana/xk6-output-prometheus-remote v0.4.0 h1:7k3xjuKaD9BwcX8iuu5v6PtAK1L53kvx1r8BaTUfRH4= diff --git a/vendor/github.com/grafana/xk6-browser/browser/browser_mapping.go b/vendor/github.com/grafana/xk6-browser/browser/browser_mapping.go index bfab5190b31..19569c09cf0 100644 --- a/vendor/github.com/grafana/xk6-browser/browser/browser_mapping.go +++ b/vendor/github.com/grafana/xk6-browser/browser/browser_mapping.go @@ -10,7 +10,7 @@ import ( ) // mapBrowser to the JS module. -func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop +func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop,gocognit return mapping{ "context": func() (mapping, error) { b, err := vu.browser() @@ -36,23 +36,24 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop return b.IsConnected(), nil }, "newContext": func(opts sobek.Value) (*sobek.Promise, error) { + popts := common.NewBrowserContextOptions() + if err := popts.Parse(vu.Context(), opts); err != nil { + return nil, fmt.Errorf("parsing browser.newContext options: %w", err) + } return k6ext.Promise(vu.Context(), func() (any, error) { b, err := vu.browser() if err != nil { return nil, err } - bctx, err := b.NewContext(opts) + bctx, err := b.NewContext(popts) if err != nil { return nil, err //nolint:wrapcheck } - if err := initBrowserContext(bctx, vu.testRunID); err != nil { return nil, err } - m := mapBrowserContext(vu, bctx) - - return m, nil + return mapBrowserContext(vu, bctx), nil }), nil }, "userAgent": func() (string, error) { @@ -69,23 +70,26 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop } return b.Version(), nil }, - "newPage": func(opts sobek.Value) *sobek.Promise { + "newPage": func(opts sobek.Value) (*sobek.Promise, error) { + popts := common.NewBrowserContextOptions() + if err := popts.Parse(vu.Context(), opts); err != nil { + return nil, fmt.Errorf("parsing browser.newPage options: %w", err) + } return k6ext.Promise(vu.Context(), func() (any, error) { b, err := vu.browser() if err != nil { return nil, err } - page, err := b.NewPage(opts) + page, err := b.NewPage(popts) if err != nil { return nil, err //nolint:wrapcheck } - if err := initBrowserContext(b.Context(), vu.testRunID); err != nil { return nil, err } return mapPage(vu, page), nil - }) + }), nil }, } } diff --git a/vendor/github.com/grafana/xk6-browser/browser/sync_browser_mapping.go b/vendor/github.com/grafana/xk6-browser/browser/sync_browser_mapping.go index 400e31ca64b..689e610a1bc 100644 --- a/vendor/github.com/grafana/xk6-browser/browser/sync_browser_mapping.go +++ b/vendor/github.com/grafana/xk6-browser/browser/sync_browser_mapping.go @@ -1,7 +1,11 @@ package browser import ( + "fmt" + "github.com/grafana/sobek" + + "github.com/grafana/xk6-browser/common" ) // syncMapBrowser is like mapBrowser but returns synchronous functions. @@ -30,11 +34,16 @@ func syncMapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop return b.IsConnected(), nil }, "newContext": func(opts sobek.Value) (*sobek.Object, error) { + popts := common.NewBrowserContextOptions() + if err := popts.Parse(vu.Context(), opts); err != nil { + return nil, fmt.Errorf("parsing browser.newContext options: %w", err) + } + b, err := vu.browser() if err != nil { return nil, err } - bctx, err := b.NewContext(opts) + bctx, err := b.NewContext(popts) if err != nil { return nil, err //nolint:wrapcheck } @@ -62,11 +71,16 @@ func syncMapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop return b.Version(), nil }, "newPage": func(opts sobek.Value) (mapping, error) { + popts := common.NewBrowserContextOptions() + if err := popts.Parse(vu.Context(), opts); err != nil { + return nil, fmt.Errorf("parsing browser.newContext options: %w", err) + } + b, err := vu.browser() if err != nil { return nil, err } - page, err := b.NewPage(opts) + page, err := b.NewPage(popts) if err != nil { return nil, err //nolint:wrapcheck } diff --git a/vendor/github.com/grafana/xk6-browser/common/browser.go b/vendor/github.com/grafana/xk6-browser/common/browser.go index c1b9bc98c3b..c8e617c0cc8 100644 --- a/vendor/github.com/grafana/xk6-browser/common/browser.go +++ b/vendor/github.com/grafana/xk6-browser/common/browser.go @@ -14,7 +14,6 @@ import ( "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/target" "github.com/gorilla/websocket" - "github.com/grafana/sobek" "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" @@ -569,7 +568,7 @@ func (b *Browser) IsConnected() bool { } // NewContext creates a new incognito-like browser context. -func (b *Browser) NewContext(opts sobek.Value) (*BrowserContext, error) { +func (b *Browser) NewContext(opts *BrowserContextOptions) (*BrowserContext, error) { _, span := TraceAPICall(b.ctx, "", "browser.newContext") defer span.End() @@ -588,14 +587,7 @@ func (b *Browser) NewContext(opts sobek.Value) (*BrowserContext, error) { return nil, err } - browserCtxOpts := NewBrowserContextOptions() - if err := browserCtxOpts.Parse(b.ctx, opts); err != nil { - err := fmt.Errorf("parsing newContext options: %w", err) - spanRecordError(span, err) - return nil, err - } - - browserCtx, err := NewBrowserContext(b.ctx, b, browserContextID, browserCtxOpts, b.logger) + browserCtx, err := NewBrowserContext(b.ctx, b, browserContextID, opts, b.logger) if err != nil { err := fmt.Errorf("new context: %w", err) spanRecordError(span, err) @@ -610,7 +602,7 @@ func (b *Browser) NewContext(opts sobek.Value) (*BrowserContext, error) { } // NewPage creates a new tab in the browser window. -func (b *Browser) NewPage(opts sobek.Value) (*Page, error) { +func (b *Browser) NewPage(opts *BrowserContextOptions) (*Page, error) { _, span := TraceAPICall(b.ctx, "", "browser.newPage") defer span.End() diff --git a/vendor/github.com/grafana/xk6-browser/common/browser_context.go b/vendor/github.com/grafana/xk6-browser/common/browser_context.go index 30a10a89aea..b2a0ef8ab70 100644 --- a/vendor/github.com/grafana/xk6-browser/common/browser_context.go +++ b/vendor/github.com/grafana/xk6-browser/common/browser_context.go @@ -90,6 +90,11 @@ type BrowserContext struct { func NewBrowserContext( ctx context.Context, browser *Browser, id cdp.BrowserContextID, opts *BrowserContextOptions, logger *log.Logger, ) (*BrowserContext, error) { + // set the default options if none provided. + if opts == nil { + opts = NewBrowserContextOptions() + } + b := BrowserContext{ BaseEventEmitter: NewBaseEventEmitter(ctx), ctx: ctx, @@ -101,7 +106,7 @@ func NewBrowserContext( timeoutSettings: NewTimeoutSettings(nil), } - if opts != nil && len(opts.Permissions) > 0 { + if len(opts.Permissions) > 0 { err := b.GrantPermissions(opts.Permissions, NewGrantPermissionsOptions()) if err != nil { return nil, err diff --git a/vendor/github.com/grafana/xk6-browser/common/helpers.go b/vendor/github.com/grafana/xk6-browser/common/helpers.go index 8ff709a2903..64eb1e32866 100644 --- a/vendor/github.com/grafana/xk6-browser/common/helpers.go +++ b/vendor/github.com/grafana/xk6-browser/common/helpers.go @@ -144,16 +144,25 @@ func createWaitForEventHandler( if stringSliceContains(events, ev.typ) { if predicateFn != nil { if predicateFn(ev.data) { - ch <- ev.data + select { + case ch <- ev.data: + case <-evCancelCtx.Done(): + return + } } } else { - ch <- nil + select { + case ch <- nil: + case <-evCancelCtx.Done(): + return + } } close(ch) // We wait for one matching event only, // then remove event handler by cancelling context and stopping goroutine. evCancelFn() + return } } @@ -185,9 +194,12 @@ func createWaitForEventPredicateHandler( case ev := <-chEvHandler: if stringSliceContains(events, ev.typ) && predicateFn != nil && predicateFn(ev.data) { - ch <- ev.data - close(ch) - evCancelFn() + select { + case ch <- ev.data: + close(ch) + evCancelFn() + case <-evCancelCtx.Done(): + } return } } diff --git a/vendor/modules.txt b/vendor/modules.txt index 2e3a0308e03..19620585ffa 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -173,7 +173,7 @@ github.com/grafana/sobek/ftoa/internal/fast github.com/grafana/sobek/parser github.com/grafana/sobek/token github.com/grafana/sobek/unistring -# github.com/grafana/xk6-browser v1.6.0 +# github.com/grafana/xk6-browser v1.6.1-0.20240701105714-29f6ef3049fe ## explicit; go 1.20 github.com/grafana/xk6-browser/browser github.com/grafana/xk6-browser/chromium