From bde4a8b1686e9379bf1f8900c9887b74c66c0185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 31 May 2022 17:27:40 +0300 Subject: [PATCH] Rename k6 pkg to k6ext Due to a bug in xk6 (grafana/xk6#45), we can't have a directory or a file with a name k6. As discussed internally, @imiric suggested renaming the k6 pkg to k6ext: "k6ext follows the k6 convention of doing the same: errext, netext, etc., so let's stick to that.". --- .github/workflows/e2e.yml | 6 +- chromium/browser_type.go | 10 +- chromium/browser_type_test.go | 1 - common/browser.go | 29 ++--- common/browser_context.go | 44 ++++---- common/browser_context_options.go | 4 +- common/browser_context_options_test.go | 2 +- common/element_handle.go | 102 +++++++++--------- common/element_handle_options.go | 25 +++-- common/execution_context.go | 4 +- common/frame.go | 140 ++++++++++++------------- common/frame_manager.go | 10 +- common/frame_options.go | 33 +++--- common/frame_options_test.go | 2 +- common/frame_session.go | 16 +-- common/frame_test.go | 2 +- common/helpers.go | 5 +- common/js_handle.go | 16 +-- common/keyboard.go | 16 +-- common/keyboard_options.go | 5 +- common/launch.go | 5 +- common/launch_test.go | 2 +- common/mouse.go | 22 ++-- common/mouse_options.go | 11 +- common/network_manager.go | 15 +-- common/network_manager_test.go | 2 +- common/page.go | 64 +++++------ common/page_options.go | 9 +- common/remote_object.go | 8 +- common/remote_object_test.go | 2 +- common/request.go | 12 +-- common/request_test.go | 2 +- common/response.go | 16 +-- common/touchscreen.go | 4 +- common/types.go | 12 +-- {k6 => k6ext}/context.go | 2 +- {k6 => k6ext}/k6test/vu.go | 4 +- {k6 => k6ext}/metrics.go | 2 +- {k6 => k6ext}/panic.go | 2 +- main.go | 10 +- tests/test_browser.go | 10 +- 41 files changed, 341 insertions(+), 347 deletions(-) rename {k6 => k6ext}/context.go (99%) rename {k6 => k6ext}/k6test/vu.go (95%) rename {k6 => k6ext}/metrics.go (98%) rename {k6 => k6ext}/panic.go (98%) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 4f8dd2728..79a2fd5f0 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -42,9 +42,9 @@ jobs: go version GOPRIVATE="go.k6.io/k6" xk6 build latest \ - --output ./k6ext \ + --output ./k6extension \ --with github.com/grafana/xk6-browser=. - ./k6ext version + ./k6extension version - name: Install Chrome dependencies if: runner.os == 'Linux' run: "${GITHUB_WORKSPACE}/.github/bin/install_chrome_deps_linux.sh" @@ -66,7 +66,7 @@ jobs: set -x export XK6_HEADLESS=true for f in examples/*.js; do - ./k6ext run "$f" + ./k6extension run "$f" done - name: Check screenshot # TODO: Do something more sophisticated? diff --git a/chromium/browser_type.go b/chromium/browser_type.go index 0ba64d174..9372c6229 100644 --- a/chromium/browser_type.go +++ b/chromium/browser_type.go @@ -16,7 +16,7 @@ import ( "github.com/grafana/xk6-browser/api" "github.com/grafana/xk6-browser/common" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" k6common "go.k6.io/k6/js/common" @@ -48,7 +48,7 @@ type BrowserType struct { // - Initializes the goja runtime. func NewBrowserType(ctx context.Context) api.BrowserType { var ( - vu = k6.GetVU(ctx) + vu = k6ext.GetVU(ctx) rt = vu.Runtime() hooks = common.NewHooks() ) @@ -151,8 +151,8 @@ func (b *BrowserType) Launch(opts goja.Value) api.Browser { // attach the browser process ID to the context // so that we can kill it afterward if it lingers - // see: k6.Panic function. - b.Ctx = k6.WithProcessID(b.Ctx, browserProc.Pid()) + // see: k6ext.Panic function. + b.Ctx = k6ext.WithProcessID(b.Ctx, browserProc.Pid()) browser, err := common.NewBrowser(b.Ctx, b.CancelFn, browserProc, launchOpts, logger) if err != nil { k6common.Throw(rt, err) @@ -408,7 +408,7 @@ func parseWebsocketURL(ctx context.Context, rc io.Reader) (wsURL string, _ error // makeLogger makes and returns an extension wide logger. func makeLogger(ctx context.Context, launchOpts *common.LaunchOptions) (*log.Logger, error) { var ( - k6Logger = k6.GetVU(ctx).State().Logger + k6Logger = k6ext.GetVU(ctx).State().Logger reCategoryFilter, _ = regexp.Compile(launchOpts.LogCategoryFilter) logger = log.New(k6Logger, launchOpts.Debug, reCategoryFilter) ) diff --git a/chromium/browser_type_test.go b/chromium/browser_type_test.go index 4fc550dbc..6faa1f16b 100644 --- a/chromium/browser_type_test.go +++ b/chromium/browser_type_test.go @@ -13,7 +13,6 @@ import ( "github.com/stretchr/testify/require" ) -//nolint:funlen func TestBrowserTypePrepareFlags(t *testing.T) { t.Parallel() diff --git a/common/browser.go b/common/browser.go index 79fa91b64..3e31f650e 100644 --- a/common/browser.go +++ b/common/browser.go @@ -27,17 +27,18 @@ import ( "sync" "sync/atomic" + "github.com/grafana/xk6-browser/api" + "github.com/grafana/xk6-browser/k6ext" + "github.com/grafana/xk6-browser/log" + + k6modules "go.k6.io/k6/js/modules" + "github.com/chromedp/cdproto" cdpbrowser "github.com/chromedp/cdproto/browser" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/target" "github.com/dop251/goja" "github.com/gorilla/websocket" - k6modules "go.k6.io/k6/js/modules" - - "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" - "github.com/grafana/xk6-browser/log" ) // Ensure Browser implements the EventEmitter and Browser interfaces. @@ -119,7 +120,7 @@ func newBrowser( contexts: make(map[cdp.BrowserContextID]*BrowserContext), pages: make(map[target.ID]*Page), sessionIDtoTargetID: make(map[target.SessionID]target.ID), - vu: k6.GetVU(ctx), + vu: k6ext.GetVU(ctx), logger: logger, } } @@ -259,7 +260,7 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) { ev.SessionID, evti.TargetID, b.ctx.Err()) return // ignore default: - k6.Panic(b.ctx, "cannot create NewPage for background_page event: %w", err) + k6ext.Panic(b.ctx, "cannot create NewPage for background_page event: %w", err) } } @@ -298,7 +299,7 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) { ev.SessionID, evti.TargetID, b.ctx.Err()) return // ignore default: - k6.Panic(b.ctx, "cannot create NewPage for page event: %w", err) + k6ext.Panic(b.ctx, "cannot create NewPage for page event: %w", err) } } @@ -413,7 +414,7 @@ func (b *Browser) Close() { action := cdpbrowser.Close() if err := action.Do(cdp.WithExecutor(b.ctx, b.conn)); err != nil { if _, ok := err.(*websocket.CloseError); !ok { - k6.Panic(b.ctx, "unable to execute %T: %v", action, err) + k6ext.Panic(b.ctx, "unable to execute %T: %v", action, err) } } @@ -450,12 +451,12 @@ func (b *Browser) NewContext(opts goja.Value) api.BrowserContext { browserContextID, err := action.Do(cdp.WithExecutor(b.ctx, b.conn)) b.logger.Debugf("Browser:NewContext", "bctxid:%v", browserContextID) if err != nil { - k6.Panic(b.ctx, "cannot create browser context (%s): %w", browserContextID, err) + k6ext.Panic(b.ctx, "cannot create browser context (%s): %w", browserContextID, err) } browserCtxOpts := NewBrowserContextOptions() if err := browserCtxOpts.Parse(b.ctx, opts); err != nil { - k6.Panic(b.ctx, "failed parsing options: %w", err) + k6ext.Panic(b.ctx, "failed parsing options: %w", err) } b.contextsMu.Lock() @@ -476,7 +477,7 @@ func (b *Browser) NewPage(opts goja.Value) api.Page { // The only accepted event value is "disconnected". func (b *Browser) On(event string) *goja.Promise { if event != EventBrowserDisconnected { - k6.Panic(b.ctx, "unknown browser event: %q, must be %q", event, EventBrowserDisconnected) + k6ext.Panic(b.ctx, "unknown browser event: %q, must be %q", event, EventBrowserDisconnected) } rt := b.vu.Runtime() @@ -506,7 +507,7 @@ func (b *Browser) UserAgent() string { action := cdpbrowser.GetVersion() _, _, _, ua, _, err := action.Do(cdp.WithExecutor(b.ctx, b.conn)) if err != nil { - k6.Panic(b.ctx, "unable to get browser user agent: %w", err) + k6ext.Panic(b.ctx, "unable to get browser user agent: %w", err) } return ua } @@ -516,7 +517,7 @@ func (b *Browser) Version() string { action := cdpbrowser.GetVersion() _, product, _, _, _, err := action.Do(cdp.WithExecutor(b.ctx, b.conn)) if err != nil { - k6.Panic(b.ctx, "unable to get browser version: %w", err) + k6ext.Panic(b.ctx, "unable to get browser version: %w", err) } i := strings.Index(product, "/") if i == -1 { diff --git a/common/browser_context.go b/common/browser_context.go index f4b4519c1..8055f7c3c 100644 --- a/common/browser_context.go +++ b/common/browser_context.go @@ -27,7 +27,7 @@ import ( "time" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" k6modules "go.k6.io/k6/js/modules" @@ -72,7 +72,7 @@ func NewBrowserContext( id: id, opts: opts, logger: logger, - vu: k6.GetVU(ctx), + vu: k6ext.GetVU(ctx), timeoutSettings: NewTimeoutSettings(nil), } @@ -84,7 +84,7 @@ func NewBrowserContext( } func (b *BrowserContext) AddCookies(cookies goja.Value) { - k6.Panic(b.ctx, "BrowserContext.addCookies(cookies) has not been implemented yet") + k6ext.Panic(b.ctx, "BrowserContext.addCookies(cookies) has not been implemented yet") } // AddInitScript adds a script that will be initialized on all new pages. @@ -134,7 +134,7 @@ func (b *BrowserContext) ClearCookies() { action := storage.ClearCookies().WithBrowserContextID(b.id) if err := action.Do(b.ctx); err != nil { - k6.Panic(b.ctx, "unable to clear cookies permissions: %w", err) + k6ext.Panic(b.ctx, "unable to clear cookies permissions: %w", err) } } @@ -144,7 +144,7 @@ func (b *BrowserContext) ClearPermissions() { action := cdpbrowser.ResetPermissions().WithBrowserContextID(b.id) if err := action.Do(b.ctx); err != nil { - k6.Panic(b.ctx, "unable to clear override permissions: %w", err) + k6ext.Panic(b.ctx, "unable to clear override permissions: %w", err) } } @@ -153,24 +153,24 @@ func (b *BrowserContext) Close() { b.logger.Debugf("BrowserContext:Close", "bctxid:%v", b.id) if b.id == "" { - k6.Panic(b.ctx, "default browser context can't be closed") + k6ext.Panic(b.ctx, "default browser context can't be closed") } if err := b.browser.disposeContext(b.id); err != nil { - k6.Panic(b.ctx, "cannot dispose browser context: %w", err) + k6ext.Panic(b.ctx, "cannot dispose browser context: %w", err) } } func (b *BrowserContext) Cookies() []goja.Object { - k6.Panic(b.ctx, "BrowserContext.cookies() has not been implemented yet") + k6ext.Panic(b.ctx, "BrowserContext.cookies() has not been implemented yet") return nil } func (b *BrowserContext) ExposeBinding(name string, callback goja.Callable, opts goja.Value) { - k6.Panic(b.ctx, "BrowserContext.exposeBinding(name, callback, opts) has not been implemented yet") + k6ext.Panic(b.ctx, "BrowserContext.exposeBinding(name, callback, opts) has not been implemented yet") } func (b *BrowserContext) ExposeFunction(name string, callback goja.Callable) { - k6.Panic(b.ctx, "BrowserContext.exposeFunction(name, callback) has not been implemented yet") + k6ext.Panic(b.ctx, "BrowserContext.exposeFunction(name, callback) has not been implemented yet") } // GrantPermissions enables the specified permissions, all others will be disabled. @@ -214,13 +214,13 @@ func (b *BrowserContext) GrantPermissions(permissions []string, opts goja.Value) action := cdpbrowser.GrantPermissions(perms).WithOrigin(origin).WithBrowserContextID(b.id) if err := action.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil { - k6.Panic(b.ctx, "override permissions: %w", err) + k6ext.Panic(b.ctx, "override permissions: %w", err) } } // NewCDPSession returns a new CDP session attached to this target. func (b *BrowserContext) NewCDPSession() api.CDPSession { - k6.Panic(b.ctx, "BrowserContext.newCDPSession() has not been implemented yet") + k6ext.Panic(b.ctx, "BrowserContext.newCDPSession() has not been implemented yet") return nil } @@ -230,7 +230,7 @@ func (b *BrowserContext) NewPage() api.Page { p, err := b.browser.newPageInContext(b.id) if err != nil { - k6.Panic(b.ctx, "newPageInContext: %w", err) + k6ext.Panic(b.ctx, "newPageInContext: %w", err) } var ( @@ -258,7 +258,7 @@ func (b *BrowserContext) Pages() []api.Page { } func (b *BrowserContext) Route(url goja.Value, handler goja.Callable) { - k6.Panic(b.ctx, "BrowserContext.route(url, handler) has not been implemented yet") + k6ext.Panic(b.ctx, "BrowserContext.route(url, handler) has not been implemented yet") } // SetDefaultNavigationTimeout sets the default navigation timeout in milliseconds. @@ -276,7 +276,7 @@ func (b *BrowserContext) SetDefaultTimeout(timeout int64) { } func (b *BrowserContext) SetExtraHTTPHeaders(headers map[string]string) { - k6.Panic(b.ctx, "BrowserContext.setExtraHTTPHeaders(headers) has not been implemented yet") + k6ext.Panic(b.ctx, "BrowserContext.setExtraHTTPHeaders(headers) has not been implemented yet") } // SetGeolocation overrides the geo location of the user. @@ -285,13 +285,13 @@ func (b *BrowserContext) SetGeolocation(geolocation goja.Value) { g := NewGeolocation() if err := g.Parse(b.ctx, geolocation); err != nil { - k6.Panic(b.ctx, "cannot parse geo location: %v", err) + k6ext.Panic(b.ctx, "cannot parse geo location: %v", err) } b.opts.Geolocation = g for _, p := range b.browser.getPages() { if err := p.updateGeolocation(); err != nil { - k6.Panic(b.ctx, "cannot update geo location in target (%s): %w", p.targetID, err) + k6ext.Panic(b.ctx, "cannot update geo location in target (%s): %w", p.targetID, err) } } } @@ -309,7 +309,7 @@ func (b *BrowserContext) SetHTTPCredentials(httpCredentials goja.Value) { c := NewCredentials() if err := c.Parse(b.ctx, httpCredentials); err != nil { - k6.Panic(b.ctx, "cannot set HTTP credentials: %w", err) + k6ext.Panic(b.ctx, "cannot set HTTP credentials: %w", err) } b.opts.HttpCredentials = c @@ -329,11 +329,11 @@ func (b *BrowserContext) SetOffline(offline bool) { } func (b *BrowserContext) StorageState(opts goja.Value) { - k6.Panic(b.ctx, "BrowserContext.storageState(opts) has not been implemented yet") + k6ext.Panic(b.ctx, "BrowserContext.storageState(opts) has not been implemented yet") } func (b *BrowserContext) Unroute(url goja.Value, handler goja.Callable) { - k6.Panic(b.ctx, "BrowserContext.unroute(url, handler) has not been implemented yet") + k6ext.Panic(b.ctx, "BrowserContext.unroute(url, handler) has not been implemented yet") } func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value) interface{} { @@ -355,7 +355,7 @@ func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value) case "predicate": predicateFn, isCallable = goja.AssertFunction(opts.Get(k)) if !isCallable { - k6.Panic(b.ctx, "expected callable predicate") + k6ext.Panic(b.ctx, "expected callable predicate") } case "timeout": timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond @@ -364,7 +364,7 @@ func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value) default: predicateFn, isCallable = goja.AssertFunction(optsOrPredicate) if !isCallable { - k6.Panic(b.ctx, "expected callable predicate") + k6ext.Panic(b.ctx, "expected callable predicate") } } } diff --git a/common/browser_context_options.go b/common/browser_context_options.go index 51638c652..285c45d13 100644 --- a/common/browser_context_options.go +++ b/common/browser_context_options.go @@ -24,7 +24,7 @@ import ( "context" "fmt" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/dop251/goja" ) @@ -69,7 +69,7 @@ func NewBrowserContextOptions() *BrowserContextOptions { } func (b *BrowserContextOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { diff --git a/common/browser_context_options_test.go b/common/browser_context_options_test.go index 6fe2a8dd2..f2fecc506 100644 --- a/common/browser_context_options_test.go +++ b/common/browser_context_options_test.go @@ -3,7 +3,7 @@ package common import ( "testing" - "github.com/grafana/xk6-browser/k6/k6test" + "github.com/grafana/xk6-browser/k6ext/k6test" "github.com/stretchr/testify/assert" ) diff --git a/common/element_handle.go b/common/element_handle.go index 6ee793616..0752a50e8 100644 --- a/common/element_handle.go +++ b/common/element_handle.go @@ -11,7 +11,7 @@ import ( "github.com/grafana/xk6-browser/api" "github.com/grafana/xk6-browser/common/js" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/dom" @@ -743,7 +743,7 @@ func (h *ElementHandle) Check(opts goja.Value) { func (h *ElementHandle) Click(opts goja.Value) { actionOpts := NewElementHandleClickOptions(h.defaultTimeout()) if err := actionOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element click options: %v", err) + k6ext.Panic(h.ctx, "cannot parse element click options: %v", err) } fn := func(apiCtx context.Context, handle *ElementHandle, p *Position) (interface{}, error) { return nil, handle.click(p, actionOpts.ToMouseClickOptions()) @@ -751,7 +751,7 @@ func (h *ElementHandle) Click(opts goja.Value) { pointerFn := h.newPointerAction(fn, &actionOpts.ElementHandleBasePointerOptions) _, err := callApiWithTimeout(h.ctx, pointerFn, actionOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot click on element: %v", err) + k6ext.Panic(h.ctx, "cannot click on element: %v", err) } applySlowMo(h.ctx) } @@ -763,7 +763,7 @@ func (h *ElementHandle) ContentFrame() api.Frame { ) action := dom.DescribeNode().WithObjectID(h.remoteObject.ObjectID) if node, err = action.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil { - k6.Panic(h.ctx, "cannot get remote node %q: %w", h.remoteObject.ObjectID, err) + k6ext.Panic(h.ctx, "cannot get remote node %q: %w", h.remoteObject.ObjectID, err) } if node == nil || node.FrameID == "" { return nil @@ -775,7 +775,7 @@ func (h *ElementHandle) ContentFrame() api.Frame { func (h *ElementHandle) Dblclick(opts goja.Value) { actionOpts := NewElementHandleDblclickOptions(h.defaultTimeout()) if err := actionOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element double click options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element double click options: %w", err) } fn := func(apiCtx context.Context, handle *ElementHandle, p *Position) (interface{}, error) { return nil, handle.dblClick(p, actionOpts.ToMouseClickOptions()) @@ -783,7 +783,7 @@ func (h *ElementHandle) Dblclick(opts goja.Value) { pointerFn := h.newPointerAction(fn, &actionOpts.ElementHandleBasePointerOptions) _, err := callApiWithTimeout(h.ctx, pointerFn, actionOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot double click on element: %w", err) + k6ext.Panic(h.ctx, "cannot double click on element: %w", err) } applySlowMo(h.ctx) } @@ -796,7 +796,7 @@ func (h *ElementHandle) DispatchEvent(typ string, eventInit goja.Value) { actFn := h.newAction([]string{}, fn, opts.Force, opts.NoWaitAfter, opts.Timeout) _, err := callApiWithTimeout(h.ctx, actFn, opts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot dispatch element event: %w", err) + k6ext.Panic(h.ctx, "cannot dispatch element event: %w", err) } applySlowMo(h.ctx) } @@ -804,7 +804,7 @@ func (h *ElementHandle) DispatchEvent(typ string, eventInit goja.Value) { func (h *ElementHandle) Fill(value string, opts goja.Value) { actionOpts := NewElementHandleBaseOptions(h.defaultTimeout()) if err := actionOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element fill options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element fill options: %w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { return handle.fill(apiCtx, value) @@ -813,7 +813,7 @@ func (h *ElementHandle) Fill(value string, opts goja.Value) { fn, actionOpts.Force, actionOpts.NoWaitAfter, actionOpts.Timeout) _, err := callApiWithTimeout(h.ctx, actFn, actionOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot handle element fill action: %w", err) + k6ext.Panic(h.ctx, "cannot handle element fill action: %w", err) } applySlowMo(h.ctx) } @@ -827,7 +827,7 @@ func (h *ElementHandle) Focus() { actFn := h.newAction([]string{}, fn, opts.Force, opts.NoWaitAfter, opts.Timeout) _, err := callApiWithTimeout(h.ctx, actFn, opts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot focus on element: %w", err) + k6ext.Panic(h.ctx, "cannot focus on element: %w", err) } applySlowMo(h.ctx) } @@ -841,7 +841,7 @@ func (h *ElementHandle) GetAttribute(name string) goja.Value { actFn := h.newAction([]string{}, fn, opts.Force, opts.NoWaitAfter, opts.Timeout) value, err := callApiWithTimeout(h.ctx, actFn, opts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot get attribute of %q: %q", name, err) + k6ext.Panic(h.ctx, "cannot get attribute of %q: %q", name, err) } applySlowMo(h.ctx) return value.(goja.Value) @@ -851,7 +851,7 @@ func (h *ElementHandle) GetAttribute(name string) goja.Value { func (h *ElementHandle) Hover(opts goja.Value) { actionOpts := NewElementHandleHoverOptions(h.defaultTimeout()) if err := actionOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element hover options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element hover options: %w", err) } fn := func(apiCtx context.Context, handle *ElementHandle, p *Position) (interface{}, error) { return nil, handle.hover(apiCtx, p) @@ -859,7 +859,7 @@ func (h *ElementHandle) Hover(opts goja.Value) { pointerFn := h.newPointerAction(fn, &actionOpts.ElementHandleBasePointerOptions) _, err := callApiWithTimeout(h.ctx, pointerFn, actionOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot hover on element: %w", err) + k6ext.Panic(h.ctx, "cannot hover on element: %w", err) } applySlowMo(h.ctx) } @@ -873,7 +873,7 @@ func (h *ElementHandle) InnerHTML() string { actFn := h.newAction([]string{}, fn, opts.Force, opts.NoWaitAfter, opts.Timeout) value, err := callApiWithTimeout(h.ctx, actFn, opts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot get element's inner HTML: %w", err) + k6ext.Panic(h.ctx, "cannot get element's inner HTML: %w", err) } applySlowMo(h.ctx) return value.(goja.Value).String() @@ -888,7 +888,7 @@ func (h *ElementHandle) InnerText() string { actFn := h.newAction([]string{}, fn, opts.Force, opts.NoWaitAfter, opts.Timeout) value, err := callApiWithTimeout(h.ctx, actFn, opts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot get element's inner text: %w", err) + k6ext.Panic(h.ctx, "cannot get element's inner text: %w", err) } applySlowMo(h.ctx) return value.(goja.Value).String() @@ -897,7 +897,7 @@ func (h *ElementHandle) InnerText() string { func (h *ElementHandle) InputValue(opts goja.Value) string { actionOpts := NewElementHandleBaseOptions(h.defaultTimeout()) if err := actionOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element input value options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element input value options: %w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { return handle.inputValue(apiCtx) @@ -905,7 +905,7 @@ func (h *ElementHandle) InputValue(opts goja.Value) string { actFn := h.newAction([]string{}, fn, actionOpts.Force, actionOpts.NoWaitAfter, actionOpts.Timeout) value, err := callApiWithTimeout(h.ctx, actFn, actionOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot get element's input value: %w", err) + k6ext.Panic(h.ctx, "cannot get element's input value: %w", err) } applySlowMo(h.ctx) return value.(goja.Value).String() @@ -915,7 +915,7 @@ func (h *ElementHandle) InputValue(opts goja.Value) string { func (h *ElementHandle) IsChecked() bool { result, err := h.isChecked(h.ctx, 0) if err != nil && err != ErrTimedOut { // We don't care anout timeout errors here! - k6.Panic(h.ctx, "cannot handle element is checked: %w", err) + k6ext.Panic(h.ctx, "cannot handle element is checked: %w", err) } return result } @@ -924,7 +924,7 @@ func (h *ElementHandle) IsChecked() bool { func (h *ElementHandle) IsDisabled() bool { result, err := h.isDisabled(h.ctx, 0) if err != nil && err != ErrTimedOut { // We don't care anout timeout errors here! - k6.Panic(h.ctx, "cannot handle element is disabled: %w", err) + k6ext.Panic(h.ctx, "cannot handle element is disabled: %w", err) } return result } @@ -933,7 +933,7 @@ func (h *ElementHandle) IsDisabled() bool { func (h *ElementHandle) IsEditable() bool { result, err := h.isEditable(h.ctx, 0) if err != nil && err != ErrTimedOut { // We don't care anout timeout errors here! - k6.Panic(h.ctx, "cannot handle element is editable: %w", err) + k6ext.Panic(h.ctx, "cannot handle element is editable: %w", err) } return result } @@ -942,7 +942,7 @@ func (h *ElementHandle) IsEditable() bool { func (h *ElementHandle) IsEnabled() bool { result, err := h.isEnabled(h.ctx, 0) if err != nil && err != ErrTimedOut { // We don't care anout timeout errors here! - k6.Panic(h.ctx, "cannot handle element is enabled: %w", err) + k6ext.Panic(h.ctx, "cannot handle element is enabled: %w", err) } return result } @@ -951,7 +951,7 @@ func (h *ElementHandle) IsEnabled() bool { func (h *ElementHandle) IsHidden() bool { result, err := h.isHidden(h.ctx, 0) if err != nil && err != ErrTimedOut { // We don't care anout timeout errors here! - k6.Panic(h.ctx, "cannot handle element is hidden: %w", err) + k6ext.Panic(h.ctx, "cannot handle element is hidden: %w", err) } return result } @@ -960,7 +960,7 @@ func (h *ElementHandle) IsHidden() bool { func (h *ElementHandle) IsVisible() bool { result, err := h.isVisible(h.ctx, 0) if err != nil && err != ErrTimedOut { // We don't care anout timeout errors here! - k6.Panic(h.ctx, "cannot check element is visible: %w", err) + k6ext.Panic(h.ctx, "cannot check element is visible: %w", err) } return result } @@ -978,7 +978,7 @@ func (h *ElementHandle) OwnerFrame() api.Frame { } res, err := h.evalWithScript(h.ctx, opts, fn) if err != nil { - k6.Panic(h.ctx, "cannot get document element: %w", err) + k6ext.Panic(h.ctx, "cannot get document element: %w", err) } if res == nil { return nil @@ -993,7 +993,7 @@ func (h *ElementHandle) OwnerFrame() api.Frame { var node *cdp.Node action := dom.DescribeNode().WithObjectID(documentHandle.remoteObject.ObjectID) if node, err = action.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil { - k6.Panic(h.ctx, "cannot describe owner frame DOM node: %w", err) + k6ext.Panic(h.ctx, "cannot describe owner frame DOM node: %w", err) } if node == nil || node.FrameID == "" { return nil @@ -1005,7 +1005,7 @@ func (h *ElementHandle) OwnerFrame() api.Frame { func (h *ElementHandle) Press(key string, opts goja.Value) { parsedOpts := NewElementHandlePressOptions(h.defaultTimeout()) if err := parsedOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse press options: %v", err) + k6ext.Panic(h.ctx, "cannot parse press options: %v", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { return nil, handle.press(apiCtx, key, NewKeyboardOptions()) @@ -1013,7 +1013,7 @@ func (h *ElementHandle) Press(key string, opts goja.Value) { actFn := h.newAction([]string{}, fn, false, parsedOpts.NoWaitAfter, parsedOpts.Timeout) _, err := callApiWithTimeout(h.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot handle element key (%q) press: %v", key, err) + k6ext.Panic(h.ctx, "cannot handle element key (%q) press: %v", key, err) } applySlowMo(h.ctx) } @@ -1023,7 +1023,7 @@ func (h *ElementHandle) Press(key string, opts goja.Value) { func (h *ElementHandle) Query(selector string) api.ElementHandle { parsedSelector, err := NewSelector(selector) if err != nil { - k6.Panic(h.ctx, "cannot parse selector (%q) in element query: %w", selector, err) + k6ext.Panic(h.ctx, "cannot parse selector (%q) in element query: %w", selector, err) } fn := ` (node, injected, selector) => { @@ -1036,7 +1036,7 @@ func (h *ElementHandle) Query(selector string) api.ElementHandle { } result, err := h.evalWithScript(h.ctx, opts, fn, parsedSelector) if err != nil { - k6.Panic(h.ctx, "cannot query element for selector (%q): %w", selector, err) + k6ext.Panic(h.ctx, "cannot query element for selector (%q): %w", selector, err) } if result == nil { return nil @@ -1061,7 +1061,7 @@ func (h *ElementHandle) QueryAll(selector string) []api.ElementHandle { handles, err := h.queryAll(selector, h.evalWithScript) if err != nil { - k6.Panic(h.ctx, "QueryAll: %w", err) + k6ext.Panic(h.ctx, "QueryAll: %w", err) } return handles @@ -1110,13 +1110,13 @@ func (h *ElementHandle) Screenshot(opts goja.Value) goja.ArrayBuffer { rt := h.execCtx.vu.Runtime() parsedOpts := NewElementHandleScreenshotOptions(h.defaultTimeout()) if err := parsedOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element screenshot options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element screenshot options: %w", err) } s := newScreenshotter(h.ctx) buf, err := s.screenshotElement(h, parsedOpts) if err != nil { - k6.Panic(h.ctx, "cannot take screenshot: %w", err) + k6ext.Panic(h.ctx, "cannot take screenshot: %w", err) } return rt.NewArrayBuffer(*buf) } @@ -1124,11 +1124,11 @@ func (h *ElementHandle) Screenshot(opts goja.Value) goja.ArrayBuffer { func (h *ElementHandle) ScrollIntoViewIfNeeded(opts goja.Value) { actionOpts := NewElementHandleBaseOptions(h.defaultTimeout()) if err := actionOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element scroll into view options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element scroll into view options: %w", err) } err := h.waitAndScrollIntoViewIfNeeded(h.ctx, actionOpts.Force, actionOpts.NoWaitAfter, actionOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot handle element scroll into view: %w", err) + k6ext.Panic(h.ctx, "cannot handle element scroll into view: %w", err) } applySlowMo(h.ctx) } @@ -1137,7 +1137,7 @@ func (h *ElementHandle) SelectOption(values goja.Value, opts goja.Value) []strin rt := h.execCtx.vu.Runtime() actionOpts := NewElementHandleBaseOptions(h.defaultTimeout()) if err := actionOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element selection options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element selection options: %w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { return handle.selectOption(apiCtx, values) @@ -1145,11 +1145,11 @@ func (h *ElementHandle) SelectOption(values goja.Value, opts goja.Value) []strin actFn := h.newAction([]string{}, fn, actionOpts.Force, actionOpts.NoWaitAfter, actionOpts.Timeout) selectedOptions, err := callApiWithTimeout(h.ctx, actFn, actionOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot handle element select option: %w", err) + k6ext.Panic(h.ctx, "cannot handle element select option: %w", err) } var returnVal []string if err := rt.ExportTo(selectedOptions.(goja.Value), &returnVal); err != nil { - k6.Panic(h.ctx, "cannot unpack options in element select option: %w", err) + k6ext.Panic(h.ctx, "cannot unpack options in element select option: %w", err) } applySlowMo(h.ctx) return returnVal @@ -1158,7 +1158,7 @@ func (h *ElementHandle) SelectOption(values goja.Value, opts goja.Value) []strin func (h *ElementHandle) SelectText(opts goja.Value) { actionOpts := NewElementHandleBaseOptions(h.defaultTimeout()) if err := actionOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element select text options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element select text options: %w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { return nil, handle.selectText(apiCtx) @@ -1166,7 +1166,7 @@ func (h *ElementHandle) SelectText(opts goja.Value) { actFn := h.newAction([]string{}, fn, actionOpts.Force, actionOpts.NoWaitAfter, actionOpts.Timeout) _, err := callApiWithTimeout(h.ctx, actFn, actionOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot select element text: %w", err) + k6ext.Panic(h.ctx, "cannot select element text: %w", err) } applySlowMo(h.ctx) } @@ -1176,7 +1176,7 @@ func (h *ElementHandle) SetChecked(checked bool, opts goja.Value) { parsedOpts := NewElementHandleSetCheckedOptions(h.defaultTimeout()) err := parsedOpts.Parse(h.ctx, opts) if err != nil { - k6.Panic(h.ctx, "cannot parse element set checked options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element set checked options: %w", err) } fn := func(apiCtx context.Context, handle *ElementHandle, p *Position) (interface{}, error) { @@ -1185,21 +1185,21 @@ func (h *ElementHandle) SetChecked(checked bool, opts goja.Value) { pointerFn := h.newPointerAction(fn, &parsedOpts.ElementHandleBasePointerOptions) _, err = callApiWithTimeout(h.ctx, pointerFn, parsedOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot check element: %w", err) + k6ext.Panic(h.ctx, "cannot check element: %w", err) } applySlowMo(h.ctx) } func (h *ElementHandle) SetInputFiles(files goja.Value, opts goja.Value) { // TODO: implement - k6.Panic(h.ctx, "ElementHandle.setInputFiles() has not been implemented yet") + k6ext.Panic(h.ctx, "ElementHandle.setInputFiles() has not been implemented yet") } func (h *ElementHandle) Tap(opts goja.Value) { parsedOpts := NewElementHandleTapOptions(h.defaultTimeout()) err := parsedOpts.Parse(h.ctx, opts) if err != nil { - k6.Panic(h.ctx, "cannot parse element tap options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element tap options: %w", err) } fn := func(apiCtx context.Context, handle *ElementHandle, p *Position) (interface{}, error) { @@ -1208,7 +1208,7 @@ func (h *ElementHandle) Tap(opts goja.Value) { pointerFn := h.newPointerAction(fn, &parsedOpts.ElementHandleBasePointerOptions) _, err = callApiWithTimeout(h.ctx, pointerFn, parsedOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot tap element: %w", err) + k6ext.Panic(h.ctx, "cannot tap element: %w", err) } applySlowMo(h.ctx) } @@ -1221,7 +1221,7 @@ func (h *ElementHandle) TextContent() string { actFn := h.newAction([]string{}, fn, opts.Force, opts.NoWaitAfter, opts.Timeout) value, err := callApiWithTimeout(h.ctx, actFn, opts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot get text content of element: %w", err) + k6ext.Panic(h.ctx, "cannot get text content of element: %w", err) } applySlowMo(h.ctx) return value.(goja.Value).String() @@ -1231,7 +1231,7 @@ func (h *ElementHandle) TextContent() string { func (h *ElementHandle) Type(text string, opts goja.Value) { parsedOpts := NewElementHandleTypeOptions(h.defaultTimeout()) if err := parsedOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element handle type options: %v", err) + k6ext.Panic(h.ctx, "cannot parse element handle type options: %v", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { return nil, handle.typ(apiCtx, text, NewKeyboardOptions()) @@ -1239,7 +1239,7 @@ func (h *ElementHandle) Type(text string, opts goja.Value) { actFn := h.newAction([]string{}, fn, false, parsedOpts.NoWaitAfter, parsedOpts.Timeout) _, err := callApiWithTimeout(h.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "cannot type (%q) into element: %w", text, err) + k6ext.Panic(h.ctx, "cannot type (%q) into element: %w", text, err) } applySlowMo(h.ctx) } @@ -1253,23 +1253,23 @@ func (h *ElementHandle) WaitForElementState(state string, opts goja.Value) { parsedOpts := NewElementHandleWaitForElementStateOptions(h.defaultTimeout()) err := parsedOpts.Parse(h.ctx, opts) if err != nil { - k6.Panic(h.ctx, "cannot parse element wait for state options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element wait for state options: %w", err) } _, err = h.waitForElementState(h.ctx, []string{state}, parsedOpts.Timeout) if err != nil { - k6.Panic(h.ctx, "error while waiting for state: %w", err) + k6ext.Panic(h.ctx, "error while waiting for state: %w", err) } } func (h *ElementHandle) WaitForSelector(selector string, opts goja.Value) api.ElementHandle { parsedOpts := NewFrameWaitForSelectorOptions(h.defaultTimeout()) if err := parsedOpts.Parse(h.ctx, opts); err != nil { - k6.Panic(h.ctx, "cannot parse element wait for selector options: %w", err) + k6ext.Panic(h.ctx, "cannot parse element wait for selector options: %w", err) } handle, err := h.waitForSelector(h.ctx, selector, parsedOpts) if err != nil { - k6.Panic(h.ctx, "error while waiting for selector (%q): %w", selector, err) + k6ext.Panic(h.ctx, "error while waiting for selector (%q): %w", selector, err) } return handle diff --git a/common/element_handle_options.go b/common/element_handle_options.go index 13b90f784..72d7a7f41 100644 --- a/common/element_handle_options.go +++ b/common/element_handle_options.go @@ -25,9 +25,8 @@ import ( "strings" "time" - "github.com/grafana/xk6-browser/k6" - "github.com/dop251/goja" + "github.com/grafana/xk6-browser/k6ext" ) type ElementHandleBaseOptions struct { @@ -137,7 +136,7 @@ func NewElementHandleBaseOptions(defaultTimeout time.Duration) *ElementHandleBas } func (o *ElementHandleBaseOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -163,7 +162,7 @@ func NewElementHandleBasePointerOptions(defaultTimeout time.Duration) *ElementHa } func (o *ElementHandleBasePointerOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil { return err } @@ -207,7 +206,7 @@ func NewElementHandleClickOptions(defaultTimeout time.Duration) *ElementHandleCl } func (o *ElementHandleClickOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { return err } @@ -251,7 +250,7 @@ func NewElementHandleDblclickOptions(defaultTimeout time.Duration) *ElementHandl } func (o *ElementHandleDblclickOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { return err } @@ -291,7 +290,7 @@ func NewElementHandleHoverOptions(defaultTimeout time.Duration) *ElementHandleHo } func (o *ElementHandleHoverOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { return err } @@ -320,7 +319,7 @@ func NewElementHandlePressOptions(defaultTimeout time.Duration) *ElementHandlePr } func (o *ElementHandlePressOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -356,7 +355,7 @@ func NewElementHandleScreenshotOptions(defaultTimeout time.Duration) *ElementHan } func (o *ElementHandleScreenshotOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { formatSpecified := false opts := opts.ToObject(rt) @@ -396,7 +395,7 @@ func NewElementHandleSetCheckedOptions(defaultTimeout time.Duration) *ElementHan } func (o *ElementHandleSetCheckedOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { return err @@ -422,7 +421,7 @@ func NewElementHandleTapOptions(defaultTimeout time.Duration) *ElementHandleTapO } func (o *ElementHandleTapOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { return err } @@ -451,7 +450,7 @@ func NewElementHandleTypeOptions(defaultTimeout time.Duration) *ElementHandleTyp } func (o *ElementHandleTypeOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -483,7 +482,7 @@ func NewElementHandleWaitForElementStateOptions(defaultTimeout time.Duration) *E } func (o *ElementHandleWaitForElementStateOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { diff --git a/common/execution_context.go b/common/execution_context.go index 38112d0d9..e20c00a67 100644 --- a/common/execution_context.go +++ b/common/execution_context.go @@ -28,7 +28,7 @@ import ( "regexp" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" k6modules "go.k6.io/k6/js/modules" @@ -90,7 +90,7 @@ func NewExecutionContext( frame: f, id: id, injectedScript: nil, - vu: k6.GetVU(ctx), + vu: k6ext.GetVU(ctx), logger: l, } if s != nil { diff --git a/common/frame.go b/common/frame.go index 31f140316..bb2480527 100644 --- a/common/frame.go +++ b/common/frame.go @@ -8,7 +8,7 @@ import ( "time" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" k6modules "go.k6.io/k6/js/modules" @@ -100,7 +100,7 @@ func NewFrame( parentFrame: parentFrame, childFrames: make(map[api.Frame]bool), id: frameID, - vu: k6.GetVU(ctx), + vu: k6ext.GetVU(ctx), lifecycleEvents: make(map[LifecycleEvent]bool), subtreeLifecycleEvents: make(map[LifecycleEvent]bool), inflightRequests: make(map[network.RequestID]bool), @@ -671,12 +671,12 @@ func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptio } func (f *Frame) AddScriptTag(opts goja.Value) { - k6.Panic(f.ctx, "Frame.AddScriptTag() has not been implemented yet") + k6ext.Panic(f.ctx, "Frame.AddScriptTag() has not been implemented yet") applySlowMo(f.ctx) } func (f *Frame) AddStyleTag(opts goja.Value) { - k6.Panic(f.ctx, "Frame.AddStyleTag() has not been implemented yet") + k6ext.Panic(f.ctx, "Frame.AddStyleTag() has not been implemented yet") applySlowMo(f.ctx) } @@ -687,7 +687,7 @@ func (f *Frame) Check(selector string, opts goja.Value) { parsedOpts := NewFrameCheckOptions(f.defaultTimeout()) err := parsedOpts.Parse(f.ctx, opts) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle, p *Position) (interface{}, error) { @@ -698,7 +698,7 @@ func (f *Frame) Check(selector string, opts goja.Value) { ) _, err = callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -722,10 +722,10 @@ func (f *Frame) Click(selector string, opts goja.Value) { popts := NewFrameClickOptions(f.defaultTimeout()) if err := popts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } if err := f.click(selector, popts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) } @@ -767,7 +767,7 @@ func (f *Frame) Dblclick(selector string, opts goja.Value) { parsedOpts := NewFrameDblClickOptions(f.defaultTimeout()) err := parsedOpts.Parse(f.ctx, opts) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle, p *Position) (interface{}, error) { @@ -778,7 +778,7 @@ func (f *Frame) Dblclick(selector string, opts goja.Value) { ) _, err = callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -789,7 +789,7 @@ func (f *Frame) DispatchEvent(selector string, typ string, eventInit goja.Value, parsedOpts := NewFrameDblClickOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -801,7 +801,7 @@ func (f *Frame) DispatchEvent(selector string, typ string, eventInit goja.Value, ) _, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -819,7 +819,7 @@ func (f *Frame) Evaluate(pageFunc goja.Value, args ...goja.Value) interface{} { } result, err := f.evaluate(f.ctx, mainWorld, opts, pageFunc, args...) if err != nil { - k6.Panic(f.ctx, "error calling evaluate: %w", err) + k6ext.Panic(f.ctx, "error calling evaluate: %w", err) } applySlowMo(f.ctx) @@ -838,13 +838,13 @@ func (f *Frame) EvaluateHandle(pageFunc goja.Value, args ...goja.Value) (handle { ec := f.executionContexts[mainWorld] if ec == nil { - k6.Panic(f.ctx, "cannot find execution context: %q", mainWorld) + k6ext.Panic(f.ctx, "cannot find execution context: %q", mainWorld) } handle, err = ec.EvalHandle(f.ctx, pageFunc, args...) } f.executionContextMu.RUnlock() if err != nil { - k6.Panic(f.ctx, "error evaluating handle: %w", err) + k6ext.Panic(f.ctx, "error evaluating handle: %w", err) } applySlowMo(f.ctx) @@ -856,7 +856,7 @@ func (f *Frame) Fill(selector string, value string, opts goja.Value) { parsedOpts := NewFrameFillOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -868,7 +868,7 @@ func (f *Frame) Fill(selector string, value string, opts goja.Value) { ) _, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -880,7 +880,7 @@ func (f *Frame) Focus(selector string, opts goja.Value) { parsedOpts := NewFrameBaseOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -891,7 +891,7 @@ func (f *Frame) Focus(selector string, opts goja.Value) { ) _, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -902,7 +902,7 @@ func (f *Frame) FrameElement() api.ElementHandle { element, err := f.page.getFrameElement(f) if err != nil { - k6.Panic(f.ctx, "error getting frame element: %w", err) + k6ext.Panic(f.ctx, "error getting frame element: %w", err) } return element } @@ -912,7 +912,7 @@ func (f *Frame) GetAttribute(selector string, name string, opts goja.Value) goja parsedOpts := NewFrameBaseOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "error getting attribute: %w", err) + k6ext.Panic(f.ctx, "error getting attribute: %w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -923,7 +923,7 @@ func (f *Frame) GetAttribute(selector string, name string, opts goja.Value) goja ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "error getting attribute: %w", err) + k6ext.Panic(f.ctx, "error getting attribute: %w", err) } applySlowMo(f.ctx) @@ -944,7 +944,7 @@ func (f *Frame) Hover(selector string, opts goja.Value) { parsedOpts := NewFrameHoverOptions(f.defaultTimeout()) err := parsedOpts.Parse(f.ctx, opts) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle, p *Position) (interface{}, error) { @@ -955,7 +955,7 @@ func (f *Frame) Hover(selector string, opts goja.Value) { ) _, err = callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -967,7 +967,7 @@ func (f *Frame) InnerHTML(selector string, opts goja.Value) string { parsedOpts := NewFrameInnerHTMLOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -978,7 +978,7 @@ func (f *Frame) InnerHTML(selector string, opts goja.Value) string { ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -989,7 +989,7 @@ func (f *Frame) InnerHTML(selector string, opts goja.Value) string { val, ok := value.(goja.Value) if !ok { - k6.Panic(f.ctx, "unexpected innerHTML value type: %T", value) + k6ext.Panic(f.ctx, "unexpected innerHTML value type: %T", value) } return val.ToString().String() @@ -1001,7 +1001,7 @@ func (f *Frame) InnerText(selector string, opts goja.Value) string { parsedOpts := NewFrameInnerTextOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1012,7 +1012,7 @@ func (f *Frame) InnerText(selector string, opts goja.Value) string { ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1023,7 +1023,7 @@ func (f *Frame) InnerText(selector string, opts goja.Value) string { val, ok := value.(goja.Value) if !ok { - k6.Panic(f.ctx, "unexpected innerText value type: %T", value) + k6ext.Panic(f.ctx, "unexpected innerText value type: %T", value) } return val.ToString().String() @@ -1034,7 +1034,7 @@ func (f *Frame) InputValue(selector string, opts goja.Value) string { parsedOpts := NewFrameInputValueOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1045,7 +1045,7 @@ func (f *Frame) InputValue(selector string, opts goja.Value) string { ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1057,7 +1057,7 @@ func (f *Frame) IsChecked(selector string, opts goja.Value) bool { parsedOpts := NewFrameIsCheckedOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1072,7 +1072,7 @@ func (f *Frame) IsChecked(selector string, opts goja.Value) bool { ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1100,7 +1100,7 @@ func (f *Frame) IsDisabled(selector string, opts goja.Value) bool { parsedOpts := NewFrameIsDisabledOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1115,7 +1115,7 @@ func (f *Frame) IsDisabled(selector string, opts goja.Value) bool { ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1127,7 +1127,7 @@ func (f *Frame) IsEditable(selector string, opts goja.Value) bool { parsedOpts := NewFrameIsEditableOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1142,7 +1142,7 @@ func (f *Frame) IsEditable(selector string, opts goja.Value) bool { ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1154,7 +1154,7 @@ func (f *Frame) IsEnabled(selector string, opts goja.Value) bool { parsedOpts := NewFrameIsEnabledOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1169,7 +1169,7 @@ func (f *Frame) IsEnabled(selector string, opts goja.Value) bool { ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1181,7 +1181,7 @@ func (f *Frame) IsHidden(selector string, opts goja.Value) bool { parsedOpts := NewFrameIsHiddenOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1196,7 +1196,7 @@ func (f *Frame) IsHidden(selector string, opts goja.Value) bool { ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1208,7 +1208,7 @@ func (f *Frame) IsVisible(selector string, opts goja.Value) bool { parsedOpts := NewFrameIsVisibleOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1223,7 +1223,7 @@ func (f *Frame) IsVisible(selector string, opts goja.Value) bool { ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1268,7 +1268,7 @@ func (f *Frame) Query(selector string) api.ElementHandle { document, err := f.document() if err != nil { - k6.Panic(f.ctx, "error getting document: %w", err) + k6ext.Panic(f.ctx, "error getting document: %w", err) } value := document.Query(selector) if value != nil { @@ -1282,7 +1282,7 @@ func (f *Frame) QueryAll(selector string) []api.ElementHandle { document, err := f.document() if err != nil { - k6.Panic(f.ctx, "error getting document: %w", err) + k6ext.Panic(f.ctx, "error getting document: %w", err) } value := document.QueryAll(selector) if value != nil { @@ -1306,7 +1306,7 @@ func (f *Frame) Press(selector string, key string, opts goja.Value) { parsedOpts := NewFramePressOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1318,7 +1318,7 @@ func (f *Frame) Press(selector string, key string, opts goja.Value) { ) _, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1329,7 +1329,7 @@ func (f *Frame) SelectOption(selector string, values goja.Value, opts goja.Value parsedOpts := NewFrameSelectOptionOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1341,12 +1341,12 @@ func (f *Frame) SelectOption(selector string, values goja.Value, opts goja.Value ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } arrayHandle, ok := value.(api.JSHandle) if !ok { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } properties := arrayHandle.GetProperties() strArr := make([]string, 0, len(properties)) @@ -1366,7 +1366,7 @@ func (f *Frame) SetContent(html string, opts goja.Value) { parsedOpts := NewFrameSetContentOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "failed parsing options: %w", err) + k6ext.Panic(f.ctx, "failed parsing options: %w", err) } js := `(html) => { @@ -1384,14 +1384,14 @@ func (f *Frame) SetContent(html string, opts goja.Value) { } rt := f.vu.Runtime() if _, err := f.evaluate(f.ctx, utilityWorld, eopts, rt.ToValue(js), rt.ToValue(html)); err != nil { - k6.Panic(f.ctx, "error setting content: %w", err) + k6ext.Panic(f.ctx, "error setting content: %w", err) } applySlowMo(f.ctx) } func (f *Frame) SetInputFiles(selector string, files goja.Value, opts goja.Value) { - k6.Panic(f.ctx, "Frame.setInputFiles(selector, files, opts) has not been implemented yet") + k6ext.Panic(f.ctx, "Frame.setInputFiles(selector, files, opts) has not been implemented yet") // TODO: needs slowMo } @@ -1401,7 +1401,7 @@ func (f *Frame) Tap(selector string, opts goja.Value) { parsedOpts := NewFrameTapOptions(f.defaultTimeout()) err := parsedOpts.Parse(f.ctx, opts) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle, p *Position) (interface{}, error) { @@ -1412,7 +1412,7 @@ func (f *Frame) Tap(selector string, opts goja.Value) { ) _, err = callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1424,7 +1424,7 @@ func (f *Frame) TextContent(selector string, opts goja.Value) string { parsedOpts := NewFrameTextContentOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1435,7 +1435,7 @@ func (f *Frame) TextContent(selector string, opts goja.Value) string { ) value, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1446,7 +1446,7 @@ func (f *Frame) TextContent(selector string, opts goja.Value) string { val, ok := value.(goja.Value) if !ok { - k6.Panic(f.ctx, "unexpected textContent value type: %T", value) + k6ext.Panic(f.ctx, "unexpected textContent value type: %T", value) } return val.ToString().String() @@ -1464,7 +1464,7 @@ func (f *Frame) Type(selector string, text string, opts goja.Value) { parsedOpts := NewFrameTypeOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle) (interface{}, error) { @@ -1476,7 +1476,7 @@ func (f *Frame) Type(selector string, text string, opts goja.Value) { ) _, err := callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1488,7 +1488,7 @@ func (f *Frame) Uncheck(selector string, opts goja.Value) { parsedOpts := NewFrameUncheckOptions(f.defaultTimeout()) err := parsedOpts.Parse(f.ctx, opts) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } fn := func(apiCtx context.Context, handle *ElementHandle, p *Position) (interface{}, error) { @@ -1499,7 +1499,7 @@ func (f *Frame) Uncheck(selector string, opts goja.Value) { ) _, err = callApiWithTimeout(f.ctx, actFn, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } applySlowMo(f.ctx) @@ -1532,7 +1532,7 @@ func (f *Frame) WaitForFunction(fn goja.Value, opts goja.Value, jsArgs ...goja.V parsedOpts := NewFrameWaitForFunctionOptions(f.defaultTimeout()) err := parsedOpts.Parse(f.ctx, opts) if err != nil { - k6.Panic(f.ctx, "error parsing waitForFunction options: %w", err) + k6ext.Panic(f.ctx, "error parsing waitForFunction options: %w", err) } f.executionContextMu.RLock() @@ -1557,7 +1557,7 @@ func (f *Frame) WaitForFunction(fn goja.Value, opts goja.Value, jsArgs ...goja.V promise, err := f.waitForFunction(f.ctx, mainWorld, js, polling, parsedOpts.Timeout, args...) if err != nil { - k6.Panic(f.ctx, "%w", err) + k6ext.Panic(f.ctx, "%w", err) } return promise @@ -1571,13 +1571,13 @@ func (f *Frame) WaitForLoadState(state string, opts goja.Value) { parsedOpts := NewFrameWaitForLoadStateOptions(f.defaultTimeout()) err := parsedOpts.Parse(f.ctx, opts) if err != nil { - k6.Panic(f.ctx, "cannot parse waitForLoadState options: %v", err) + k6ext.Panic(f.ctx, "cannot parse waitForLoadState options: %v", err) } waitUntil := LifecycleEventLoad if state != "" { if err = waitUntil.UnmarshalText([]byte(state)); err != nil { - k6.Panic(f.ctx, "waitForLoadState error: %v", err) + k6ext.Panic(f.ctx, "waitForLoadState error: %v", err) } } @@ -1589,7 +1589,7 @@ func (f *Frame) WaitForLoadState(state string, opts goja.Value) { return data.(LifecycleEvent) == waitUntil }, parsedOpts.Timeout) if err != nil { - k6.Panic(f.ctx, "cannot waitForEvent: %v", err) + k6ext.Panic(f.ctx, "cannot waitForEvent: %v", err) } } @@ -1602,11 +1602,11 @@ func (f *Frame) WaitForNavigation(opts goja.Value) api.Response { func (f *Frame) WaitForSelector(selector string, opts goja.Value) api.ElementHandle { parsedOpts := NewFrameWaitForSelectorOptions(f.defaultTimeout()) if err := parsedOpts.Parse(f.ctx, opts); err != nil { - k6.Panic(f.ctx, "failed parsing options: %w", err) + k6ext.Panic(f.ctx, "failed parsing options: %w", err) } handle, err := f.waitForSelectorRetry(selector, parsedOpts, maxRetry) if err != nil { - k6.Panic(f.ctx, "error waiting for selector: %w", err) + k6ext.Panic(f.ctx, "error waiting for selector: %w", err) } return handle } diff --git a/common/frame_manager.go b/common/frame_manager.go index fbb750b03..28ae4e7de 100644 --- a/common/frame_manager.go +++ b/common/frame_manager.go @@ -30,7 +30,7 @@ import ( "time" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" k6common "go.k6.io/k6/js/common" @@ -89,7 +89,7 @@ func NewFrameManager( frames: make(map[cdp.FrameID]*Frame), inflightRequests: make(map[network.RequestID]bool), barriers: make([]*Barrier, 0), - vu: k6.GetVU(ctx), + vu: k6ext.GetVU(ctx), logger: l, id: atomic.AddInt64(&frameManagerID, 1), } @@ -708,7 +708,7 @@ func (m *FrameManager) WaitForFrameNavigation(frame *Frame, opts goja.Value) api parsedOpts := NewFrameWaitForNavigationOptions(time.Duration(m.timeoutSettings.timeout()) * time.Second) if err := parsedOpts.Parse(m.ctx, opts); err != nil { - k6.Panic(m.ctx, "cannot parse waitForNavigation options: %v", err) + k6ext.Panic(m.ctx, "cannot parse waitForNavigation options: %v", err) } ch, evCancelFn := createWaitForEventHandler(m.ctx, frame, []string{EventFrameNavigation}, @@ -726,7 +726,7 @@ func (m *FrameManager) WaitForFrameNavigation(frame *Frame, opts goja.Value) api m.ID(), frame.URL(), m.ctx.Err()) return nil case <-time.After(parsedOpts.Timeout): - k6.Panic(m.ctx, "waitForFrameNavigation timed out after %s", parsedOpts.Timeout) + k6ext.Panic(m.ctx, "waitForFrameNavigation timed out after %s", parsedOpts.Timeout) case data := <-ch: event = data.(*NavigationEvent) } @@ -747,7 +747,7 @@ func (m *FrameManager) WaitForFrameNavigation(frame *Frame, opts goja.Value) api return data.(LifecycleEvent) == parsedOpts.WaitUntil }, parsedOpts.Timeout) if err != nil { - k6.Panic(m.ctx, "waitForFrameNavigation cannot wait for event (EventFrameAddLifecycle): %v", err) + k6ext.Panic(m.ctx, "waitForFrameNavigation cannot wait for event (EventFrameAddLifecycle): %v", err) } } diff --git a/common/frame_options.go b/common/frame_options.go index cc134a69b..0042ad722 100644 --- a/common/frame_options.go +++ b/common/frame_options.go @@ -26,9 +26,8 @@ import ( "reflect" "time" - "github.com/grafana/xk6-browser/k6" - "github.com/dop251/goja" + "github.com/grafana/xk6-browser/k6ext" ) type FrameBaseOptions struct { @@ -168,7 +167,7 @@ func NewFrameBaseOptions(defaultTimeout time.Duration) *FrameBaseOptions { } func (o *FrameBaseOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -191,7 +190,7 @@ func NewFrameCheckOptions(defaultTimeout time.Duration) *FrameCheckOptions { } func (o *FrameCheckOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { return err } @@ -215,7 +214,7 @@ func NewFrameClickOptions(defaultTimeout time.Duration) *FrameClickOptions { } func (o *FrameClickOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleClickOptions.Parse(ctx, opts); err != nil { return err } @@ -239,7 +238,7 @@ func NewFrameDblClickOptions(defaultTimeout time.Duration) *FrameDblclickOptions } func (o *FrameDblclickOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleDblclickOptions.Parse(ctx, opts); err != nil { return err } @@ -263,7 +262,7 @@ func NewFrameFillOptions(defaultTimeout time.Duration) *FrameFillOptions { } func (o *FrameFillOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil { return err } @@ -288,7 +287,7 @@ func NewFrameGotoOptions(defaultReferer string, defaultTimeout time.Duration) *F } func (o *FrameGotoOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -316,7 +315,7 @@ func NewFrameHoverOptions(defaultTimeout time.Duration) *FrameHoverOptions { } func (o *FrameHoverOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleHoverOptions.Parse(ctx, opts); err != nil { return err } @@ -470,7 +469,7 @@ func NewFrameSelectOptionOptions(defaultTimeout time.Duration) *FrameSelectOptio } func (o *FrameSelectOptionOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil { return err } @@ -494,7 +493,7 @@ func NewFrameSetContentOptions(defaultTimeout time.Duration) *FrameSetContentOpt } func (o *FrameSetContentOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) @@ -523,7 +522,7 @@ func NewFrameTapOptions(defaultTimeout time.Duration) *FrameTapOptions { } func (o *FrameTapOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { return err } @@ -579,7 +578,7 @@ func NewFrameUncheckOptions(defaultTimeout time.Duration) *FrameUncheckOptions { } func (o *FrameUncheckOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { return err } @@ -605,7 +604,7 @@ func NewFrameWaitForFunctionOptions(defaultTimeout time.Duration) *FrameWaitForF // Parse JavaScript waitForFunction options. func (o *FrameWaitForFunctionOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) @@ -643,7 +642,7 @@ func NewFrameWaitForLoadStateOptions(defaultTimeout time.Duration) *FrameWaitFor } func (o *FrameWaitForLoadStateOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -665,7 +664,7 @@ func NewFrameWaitForNavigationOptions(defaultTimeout time.Duration) *FrameWaitFo } func (o *FrameWaitForNavigationOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -694,7 +693,7 @@ func NewFrameWaitForSelectorOptions(defaultTimeout time.Duration) *FrameWaitForS } func (o *FrameWaitForSelectorOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) diff --git a/common/frame_options_test.go b/common/frame_options_test.go index 141af1adf..19f884608 100644 --- a/common/frame_options_test.go +++ b/common/frame_options_test.go @@ -4,7 +4,7 @@ import ( "testing" "time" - "github.com/grafana/xk6-browser/k6/k6test" + "github.com/grafana/xk6-browser/k6ext/k6test" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/common/frame_session.go b/common/frame_session.go index 1514d2730..c7c4bf486 100644 --- a/common/frame_session.go +++ b/common/frame_session.go @@ -30,7 +30,7 @@ import ( "sync" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" k6modules "go.k6.io/k6/js/modules" @@ -65,7 +65,7 @@ type FrameSession struct { manager *FrameManager networkManager *NetworkManager - k6Metrics *k6.CustomMetrics + k6Metrics *k6ext.CustomMetrics targetID target.ID windowID browser.WindowID @@ -106,8 +106,8 @@ func NewFrameSession( isolatedWorlds: make(map[string]bool), eventCh: make(chan Event), childSessions: make(map[cdp.FrameID]*FrameSession), - vu: k6.GetVU(ctx), - k6Metrics: k6.GetCustomMetrics(ctx), + vu: k6ext.GetVU(ctx), + k6Metrics: k6ext.GetCustomMetrics(ctx), logger: l, serializer: l.ConsoleLogFormatterSerializer(), } @@ -553,7 +553,7 @@ func (fs *FrameSession) onExecutionContextCreated(event *cdpruntime.EventExecuti Type string `json:"type"` } if err := json.Unmarshal(auxData, &i); err != nil { - k6.Panic(fs.ctx, "unable to unmarshal JSON: %w", err) + k6ext.Panic(fs.ctx, "unable to unmarshal JSON: %w", err) } var world executionWorld frame := fs.manager.getFrameByID(i.FrameID) @@ -640,7 +640,7 @@ func (fs *FrameSession) onFrameNavigated(frame *cdp.Frame, initial bool) { err := fs.manager.frameNavigated(frame.ID, frame.ParentID, frame.LoaderID.String(), frame.Name, frame.URL+frame.URLFragment, initial) if err != nil { - k6.Panic(fs.ctx, "cannot handle frame navigation: %w", err) + k6ext.Panic(fs.ctx, "cannot handle frame navigation: %w", err) } } @@ -652,7 +652,7 @@ func (fs *FrameSession) onFrameRequestedNavigation(event *cdppage.EventFrameRequ if event.Disposition == "currentTab" { err := fs.manager.frameRequestedNavigation(event.FrameID, event.URL, "") if err != nil { - k6.Panic(fs.ctx, "cannot handle frame requested navigation: %w", err) + k6ext.Panic(fs.ctx, "cannot handle frame requested navigation: %w", err) } } } @@ -809,7 +809,7 @@ func (fs *FrameSession) onAttachedToTarget(event *target.EventAttachedToTarget) return // ignore } reason = "fatal" - k6.Panic(fs.ctx, "cannot attach %v: %w", ti.Type, err) + k6ext.Panic(fs.ctx, "cannot attach %v: %w", ti.Type, err) } } diff --git a/common/frame_test.go b/common/frame_test.go index efea982c2..8f10825c5 100644 --- a/common/frame_test.go +++ b/common/frame_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "github.com/grafana/xk6-browser/k6/k6test" + "github.com/grafana/xk6-browser/k6ext/k6test" "github.com/grafana/xk6-browser/log" "github.com/chromedp/cdproto/cdp" diff --git a/common/helpers.go b/common/helpers.go index 69776ac5e..0c567d47f 100644 --- a/common/helpers.go +++ b/common/helpers.go @@ -29,8 +29,7 @@ import ( cdpruntime "github.com/chromedp/cdproto/runtime" "github.com/dop251/goja" - - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" ) func convertBaseJSHandleTypes(ctx context.Context, execCtx *ExecutionContext, objHandle *BaseJSHandle) (*cdpruntime.CallArgument, error) { @@ -198,7 +197,7 @@ func waitForEvent(ctx context.Context, emitter EventEmitter, events []string, pr // panicOrSlowMo panics if err is not nil, otherwise applies slow motion. func panicOrSlowMo(ctx context.Context, err error) { if err != nil { - k6.Panic(ctx, "%w", err) + k6ext.Panic(ctx, "%w", err) } applySlowMo(ctx) } diff --git a/common/js_handle.go b/common/js_handle.go index 6afc65abd..3a6cced06 100644 --- a/common/js_handle.go +++ b/common/js_handle.go @@ -24,7 +24,7 @@ import ( "context" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" "github.com/chromedp/cdproto/cdp" @@ -91,7 +91,7 @@ func (h *BaseJSHandle) Dispose() { action := runtime.ReleaseObject(h.remoteObject.ObjectID) if err := action.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil { - k6.Panic(h.ctx, "unable to dispose element %T: %w", action, err) + k6ext.Panic(h.ctx, "unable to dispose element %T: %w", action, err) } } @@ -101,7 +101,7 @@ func (h *BaseJSHandle) Evaluate(pageFunc goja.Value, args ...goja.Value) interfa args = append([]goja.Value{rt.ToValue(h)}, args...) res, err := h.execCtx.Eval(h.ctx, pageFunc, args...) if err != nil { - k6.Panic(h.ctx, "%w", err) + k6ext.Panic(h.ctx, "%w", err) } return res } @@ -112,7 +112,7 @@ func (h *BaseJSHandle) EvaluateHandle(pageFunc goja.Value, args ...goja.Value) a args = append([]goja.Value{rt.ToValue(h)}, args...) res, err := h.execCtx.EvalHandle(h.ctx, pageFunc, args...) if err != nil { - k6.Panic(h.ctx, "%w", err) + k6ext.Panic(h.ctx, "%w", err) } return res } @@ -127,7 +127,7 @@ func (h *BaseJSHandle) GetProperties() map[string]api.JSHandle { action := runtime.GetProperties(h.remoteObject.ObjectID). WithOwnProperties(true) if result, _, _, _, err = action.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil { - k6.Panic(h.ctx, "unable to get properties for JS handle %T: %w", action, err) + k6ext.Panic(h.ctx, "unable to get properties for JS handle %T: %w", action, err) } props := make(map[string]api.JSHandle, len(result)) @@ -156,17 +156,17 @@ func (h *BaseJSHandle) JSONValue() goja.Value { WithAwaitPromise(true). WithObjectID(h.remoteObject.ObjectID) if result, _, err = action.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil { - k6.Panic(h.ctx, "unable to get properties for JS handle %T: %w", action, err) + k6ext.Panic(h.ctx, "unable to get properties for JS handle %T: %w", action, err) } res, err := valueFromRemoteObject(h.ctx, result) if err != nil { - k6.Panic(h.ctx, "unable to extract value from remote object: %w", err) + k6ext.Panic(h.ctx, "unable to extract value from remote object: %w", err) } return res } res, err := valueFromRemoteObject(h.ctx, h.remoteObject) if err != nil { - k6.Panic(h.ctx, "unable to extract value from remote object: %w", err) + k6ext.Panic(h.ctx, "unable to extract value from remote object: %w", err) } return res } diff --git a/common/keyboard.go b/common/keyboard.go index e35959791..29e8fccae 100644 --- a/common/keyboard.go +++ b/common/keyboard.go @@ -26,7 +26,7 @@ import ( "time" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/keyboardlayout" "github.com/chromedp/cdproto/cdp" @@ -69,14 +69,14 @@ func NewKeyboard(ctx context.Context, s session) *Keyboard { // Down sends a key down message to a session target. func (k *Keyboard) Down(key string) { if err := k.down(key); err != nil { - k6.Panic(k.ctx, "cannot send key down: %w", err) + k6ext.Panic(k.ctx, "cannot send key down: %w", err) } } // Up sends a key up message to a session target. func (k *Keyboard) Up(key string) { if err := k.up(key); err != nil { - k6.Panic(k.ctx, "cannot send key up: %w", err) + k6ext.Panic(k.ctx, "cannot send key up: %w", err) } } @@ -86,17 +86,17 @@ func (k *Keyboard) Up(key string) { func (k *Keyboard) Press(key string, opts goja.Value) { kbdOpts := NewKeyboardOptions() if err := kbdOpts.Parse(k.ctx, opts); err != nil { - k6.Panic(k.ctx, "cannot parse keyboard options: %w", err) + k6ext.Panic(k.ctx, "cannot parse keyboard options: %w", err) } if err := k.press(key, kbdOpts); err != nil { - k6.Panic(k.ctx, "cannot press key: %w", err) + k6ext.Panic(k.ctx, "cannot press key: %w", err) } } // InsertText inserts a text without dispatching key events. func (k *Keyboard) InsertText(text string) { if err := k.insertText(text); err != nil { - k6.Panic(k.ctx, "cannot insert text: %w", err) + k6ext.Panic(k.ctx, "cannot insert text: %w", err) } } @@ -108,10 +108,10 @@ func (k *Keyboard) InsertText(text string) { func (k *Keyboard) Type(text string, opts goja.Value) { kbdOpts := NewKeyboardOptions() if err := kbdOpts.Parse(k.ctx, opts); err != nil { - k6.Panic(k.ctx, "cannot parse keyboard options: %w", err) + k6ext.Panic(k.ctx, "cannot parse keyboard options: %w", err) } if err := k.typ(text, kbdOpts); err != nil { - k6.Panic(k.ctx, "cannot type text: %w", err) + k6ext.Panic(k.ctx, "cannot type text: %w", err) } } diff --git a/common/keyboard_options.go b/common/keyboard_options.go index e41f4f445..b086bb087 100644 --- a/common/keyboard_options.go +++ b/common/keyboard_options.go @@ -23,9 +23,8 @@ package common import ( "context" - "github.com/grafana/xk6-browser/k6" - "github.com/dop251/goja" + "github.com/grafana/xk6-browser/k6ext" ) type KeyboardOptions struct { @@ -39,7 +38,7 @@ func NewKeyboardOptions() *KeyboardOptions { } func (o *KeyboardOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { diff --git a/common/launch.go b/common/launch.go index 300bcbe72..fe3ce13ed 100644 --- a/common/launch.go +++ b/common/launch.go @@ -26,9 +26,8 @@ import ( "reflect" "time" - "github.com/grafana/xk6-browser/k6" - "github.com/dop251/goja" + "github.com/grafana/xk6-browser/k6ext" ) type ProxyOptions struct { @@ -71,7 +70,7 @@ func NewLaunchOptions() *LaunchOptions { // Parse parses launch options from a JS object. func (l *LaunchOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { diff --git a/common/launch_test.go b/common/launch_test.go index ff83796f0..3c46cfd13 100644 --- a/common/launch_test.go +++ b/common/launch_test.go @@ -3,7 +3,7 @@ package common import ( "testing" - "github.com/grafana/xk6-browser/k6/k6test" + "github.com/grafana/xk6-browser/k6ext/k6test" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/common/mouse.go b/common/mouse.go index b6f1c193c..dd96c1d5a 100644 --- a/common/mouse.go +++ b/common/mouse.go @@ -25,7 +25,7 @@ import ( "time" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/input" @@ -153,20 +153,20 @@ func (m *Mouse) up(x float64, y float64, opts *MouseDownUpOptions) error { func (m *Mouse) Click(x float64, y float64, opts goja.Value) { mouseOpts := NewMouseClickOptions() if err := mouseOpts.Parse(m.ctx, opts); err != nil { - k6.Panic(m.ctx, "failed parsing options: %w", err) + k6ext.Panic(m.ctx, "failed parsing options: %w", err) } if err := m.click(x, y, mouseOpts); err != nil { - k6.Panic(m.ctx, "unable to mouse click: %w", err) + k6ext.Panic(m.ctx, "unable to mouse click: %w", err) } } func (m *Mouse) DblClick(x float64, y float64, opts goja.Value) { mouseOpts := NewMouseDblClickOptions() if err := mouseOpts.Parse(m.ctx, opts); err != nil { - k6.Panic(m.ctx, "failed parsing options: %w", err) + k6ext.Panic(m.ctx, "failed parsing options: %w", err) } if err := m.dblClick(x, y, mouseOpts); err != nil { - k6.Panic(m.ctx, "unable to mouse double click: %w", err) + k6ext.Panic(m.ctx, "unable to mouse double click: %w", err) } } @@ -174,10 +174,10 @@ func (m *Mouse) DblClick(x float64, y float64, opts goja.Value) { func (m *Mouse) Down(x float64, y float64, opts goja.Value) { mouseOpts := NewMouseDownUpOptions() if err := mouseOpts.Parse(m.ctx, opts); err != nil { - k6.Panic(m.ctx, "failed parsing options: %w", err) + k6ext.Panic(m.ctx, "failed parsing options: %w", err) } if err := m.down(x, y, mouseOpts); err != nil { - k6.Panic(m.ctx, "unable to mouse down: %w", err) + k6ext.Panic(m.ctx, "unable to mouse down: %w", err) } } @@ -185,10 +185,10 @@ func (m *Mouse) Down(x float64, y float64, opts goja.Value) { func (m *Mouse) Move(x float64, y float64, opts goja.Value) { mouseOpts := NewMouseDownUpOptions() if err := mouseOpts.Parse(m.ctx, opts); err != nil { - k6.Panic(m.ctx, "failed parsing options: %w", err) + k6ext.Panic(m.ctx, "failed parsing options: %w", err) } if err := m.down(x, y, mouseOpts); err != nil { - k6.Panic(m.ctx, "unable to move mouse: %w", err) + k6ext.Panic(m.ctx, "unable to move mouse: %w", err) } } @@ -196,10 +196,10 @@ func (m *Mouse) Move(x float64, y float64, opts goja.Value) { func (m *Mouse) Up(x float64, y float64, opts goja.Value) { mouseOpts := NewMouseDownUpOptions() if err := mouseOpts.Parse(m.ctx, opts); err != nil { - k6.Panic(m.ctx, "failed parsing options: %w", err) + k6ext.Panic(m.ctx, "failed parsing options: %w", err) } if err := m.up(x, y, mouseOpts); err != nil { - k6.Panic(m.ctx, "unable to mouse up: %w", err) + k6ext.Panic(m.ctx, "unable to mouse up: %w", err) } } diff --git a/common/mouse_options.go b/common/mouse_options.go index d24eb3153..b762932c3 100644 --- a/common/mouse_options.go +++ b/common/mouse_options.go @@ -23,9 +23,8 @@ package common import ( "context" - "github.com/grafana/xk6-browser/k6" - "github.com/dop251/goja" + "github.com/grafana/xk6-browser/k6ext" ) type MouseClickOptions struct { @@ -57,7 +56,7 @@ func NewMouseClickOptions() *MouseClickOptions { } func (o *MouseClickOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -89,7 +88,7 @@ func NewMouseDblClickOptions() *MouseDblClickOptions { } func (o *MouseDblClickOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -118,7 +117,7 @@ func NewMouseDownUpOptions() *MouseDownUpOptions { } func (o *MouseDownUpOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -140,7 +139,7 @@ func NewMouseMoveOptions() *MouseMoveOptions { } func (o *MouseMoveOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { diff --git a/common/network_manager.go b/common/network_manager.go index 8d91d2955..f1d555496 100644 --- a/common/network_manager.go +++ b/common/network_manager.go @@ -29,9 +29,10 @@ import ( "sync" "time" - "github.com/grafana/xk6-browser/k6" "github.com/grafana/xk6-browser/log" + "github.com/grafana/xk6-browser/k6ext" + k6modules "go.k6.io/k6/js/modules" k6lib "go.k6.io/k6/lib" k6netext "go.k6.io/k6/lib/netext" @@ -80,7 +81,7 @@ type NetworkManager struct { func NewNetworkManager( ctx context.Context, s session, fm *FrameManager, parent *NetworkManager, ) (*NetworkManager, error) { - vu := k6.GetVU(ctx) + vu := k6ext.GetVU(ctx) state := vu.State() resolver, err := newResolver(state.Options.DNS) @@ -665,7 +666,7 @@ func (m *NetworkManager) Authenticate(credentials *Credentials) { m.userReqInterceptionEnabled = true } if err := m.updateProtocolRequestInterception(); err != nil { - k6.Panic(m.ctx, "error setting authentication credentials: %w", err) + k6ext.Panic(m.ctx, "error setting authentication credentials: %w", err) } } @@ -679,7 +680,7 @@ func (m *NetworkManager) ExtraHTTPHeaders() goja.Value { func (m *NetworkManager) SetExtraHTTPHeaders(headers network.Headers) { action := network.SetExtraHTTPHeaders(headers) if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil { - k6.Panic(m.ctx, "unable to set extra HTTP headers: %w", err) + k6ext.Panic(m.ctx, "unable to set extra HTTP headers: %w", err) } } @@ -692,7 +693,7 @@ func (m *NetworkManager) SetOfflineMode(offline bool) { action := network.EmulateNetworkConditions(m.offline, 0, -1, -1) if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil { - k6.Panic(m.ctx, "unable to set offline mode: %w", err) + k6ext.Panic(m.ctx, "unable to set offline mode: %w", err) } } @@ -700,7 +701,7 @@ func (m *NetworkManager) SetOfflineMode(offline bool) { func (m *NetworkManager) SetUserAgent(userAgent string) { action := emulation.SetUserAgentOverride(userAgent) if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil { - k6.Panic(m.ctx, "unable to set user agent override: %w", err) + k6ext.Panic(m.ctx, "unable to set user agent override: %w", err) } } @@ -708,6 +709,6 @@ func (m *NetworkManager) SetUserAgent(userAgent string) { func (m *NetworkManager) SetCacheEnabled(enabled bool) { m.userCacheDisabled = !enabled if err := m.updateProtocolCacheDisabled(); err != nil { - k6.Panic(m.ctx, "error toggling cache: %w", err) + k6ext.Panic(m.ctx, "error toggling cache: %w", err) } } diff --git a/common/network_manager_test.go b/common/network_manager_test.go index 7c9b2f7b4..6a0646564 100644 --- a/common/network_manager_test.go +++ b/common/network_manager_test.go @@ -6,7 +6,7 @@ import ( "net" "testing" - "github.com/grafana/xk6-browser/k6/k6test" + "github.com/grafana/xk6-browser/k6ext/k6test" "github.com/grafana/xk6-browser/log" k6lib "go.k6.io/k6/lib" diff --git a/common/page.go b/common/page.go index b842728e1..aba5e9170 100644 --- a/common/page.go +++ b/common/page.go @@ -29,7 +29,7 @@ import ( "time" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" k6modules "go.k6.io/k6/js/modules" @@ -121,7 +121,7 @@ func NewPage( frameSessions: make(map[cdp.FrameID]*FrameSession), workers: make(map[target.SessionID]*Worker), routes: make([]api.Route, 0), - vu: k6.GetVU(ctx), + vu: k6ext.GetVU(ctx), logger: logger, } @@ -367,15 +367,15 @@ func (p *Page) viewportSize() Size { // AddInitScript adds script to run in all new frames. func (p *Page) AddInitScript(script goja.Value, arg goja.Value) { - k6.Panic(p.ctx, "Page.addInitScript(script, arg) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.addInitScript(script, arg) has not been implemented yet") } func (p *Page) AddScriptTag(opts goja.Value) { - k6.Panic(p.ctx, "Page.addScriptTag(opts) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.addScriptTag(opts) has not been implemented yet") } func (p *Page) AddStyleTag(opts goja.Value) { - k6.Panic(p.ctx, "Page.addStyleTag(opts) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.addStyleTag(opts) has not been implemented yet") } // BringToFront activates the browser tab for this page. @@ -384,7 +384,7 @@ func (p *Page) BringToFront() { action := cdppage.BringToFront() if err := action.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil { - k6.Panic(p.ctx, "unable to bring page to front: %w", err) + k6ext.Panic(p.ctx, "unable to bring page to front: %w", err) } } @@ -435,7 +435,7 @@ func (p *Page) DispatchEvent(selector string, typ string, eventInit goja.Value, } func (p *Page) DragAndDrop(source string, target string, opts goja.Value) { - k6.Panic(p.ctx, "Page.DragAndDrop(source, target, opts) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.DragAndDrop(source, target, opts) has not been implemented yet") } func (p *Page) EmulateMedia(opts goja.Value) { @@ -443,7 +443,7 @@ func (p *Page) EmulateMedia(opts goja.Value) { parsedOpts := NewPageEmulateMediaOptions(p.mediaType, p.colorScheme, p.reducedMotion) if err := parsedOpts.Parse(p.ctx, opts); err != nil { - k6.Panic(p.ctx, "failed parsing options: %w", err) + k6ext.Panic(p.ctx, "failed parsing options: %w", err) } p.mediaType = parsedOpts.Media @@ -452,7 +452,7 @@ func (p *Page) EmulateMedia(opts goja.Value) { for _, fs := range p.frameSessions { if err := fs.updateEmulateMedia(false); err != nil { - k6.Panic(p.ctx, "error emulating media: %w", err) + k6ext.Panic(p.ctx, "error emulating media: %w", err) } } @@ -473,12 +473,12 @@ func (p *Page) EmulateVisionDeficiency(typ string) { } t, ok := validTypes[typ] if !ok { - k6.Panic(p.ctx, "unsupported vision deficiency: '%s'", typ) + k6ext.Panic(p.ctx, "unsupported vision deficiency: '%s'", typ) } action := emulation.SetEmulatedVisionDeficiency(t) if err := action.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil { - k6.Panic(p.ctx, "unable to set emulated vision deficiency '%s': %w", typ, err) + k6ext.Panic(p.ctx, "unable to set emulated vision deficiency '%s': %w", typ, err) } applySlowMo(p.ctx) @@ -498,11 +498,11 @@ func (p *Page) EvaluateHandle(pageFunc goja.Value, args ...goja.Value) api.JSHan } func (p *Page) ExposeBinding(name string, callback goja.Callable, opts goja.Value) { - k6.Panic(p.ctx, "Page.exposeBinding(name, callback) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.exposeBinding(name, callback) has not been implemented yet") } func (p *Page) ExposeFunction(name string, callback goja.Callable) { - k6.Panic(p.ctx, "Page.exposeFunction(name, callback) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.exposeFunction(name, callback) has not been implemented yet") } func (p *Page) Fill(selector string, value string, opts goja.Value) { @@ -518,7 +518,7 @@ func (p *Page) Focus(selector string, opts goja.Value) { } func (p *Page) Frame(frameSelector goja.Value) api.Frame { - k6.Panic(p.ctx, "Page.frame(frameSelector) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.frame(frameSelector) has not been implemented yet") return nil } @@ -535,12 +535,12 @@ func (p *Page) GetAttribute(selector string, name string, opts goja.Value) goja. } func (p *Page) GoBack(opts goja.Value) api.Response { - k6.Panic(p.ctx, "Page.goBack(opts) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.goBack(opts) has not been implemented yet") return nil } func (p *Page) GoForward(opts goja.Value) api.Response { - k6.Panic(p.ctx, "Page.goForward(opts) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.goForward(opts) has not been implemented yet") return nil } @@ -646,12 +646,12 @@ func (p *Page) Opener() api.Page { } func (p *Page) Pause() { - k6.Panic(p.ctx, "Page.pause() has not been implemented yet") + k6ext.Panic(p.ctx, "Page.pause() has not been implemented yet") } func (p *Page) Pdf(opts goja.Value) goja.ArrayBuffer { rt := p.vu.Runtime() - k6.Panic(p.ctx, "Page.pdf(opts) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.pdf(opts) has not been implemented yet") return rt.NewArrayBuffer([]byte{}) } @@ -679,7 +679,7 @@ func (p *Page) Reload(opts goja.Value) api.Response { parsedOpts := NewPageReloadOptions(LifecycleEventLoad, p.defaultTimeout()) if err := parsedOpts.Parse(p.ctx, opts); err != nil { - k6.Panic(p.ctx, "failed parsing options: %w", err) + k6ext.Panic(p.ctx, "failed parsing options: %w", err) } ch, evCancelFn := createWaitForEventHandler(p.ctx, p.frameManager.MainFrame(), []string{EventFrameNavigation}, func(data interface{}) bool { @@ -689,14 +689,14 @@ func (p *Page) Reload(opts goja.Value) api.Response { action := cdppage.Reload() if err := action.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil { - k6.Panic(p.ctx, "unable to reload page: %w", err) + k6ext.Panic(p.ctx, "unable to reload page: %w", err) } var event *NavigationEvent select { case <-p.ctx.Done(): case <-time.After(parsedOpts.Timeout): - k6.Panic(p.ctx, "%w", ErrTimedOut) + k6ext.Panic(p.ctx, "%w", ErrTimedOut) case data := <-ch: event = data.(*NavigationEvent) } @@ -719,19 +719,19 @@ func (p *Page) Reload(opts goja.Value) api.Response { } func (p *Page) Route(url goja.Value, handler goja.Callable) { - k6.Panic(p.ctx, "Page.route(url, handler) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.route(url, handler) has not been implemented yet") } // Screenshot will instruct Chrome to save a screenshot of the current page and save it to specified file. func (p *Page) Screenshot(opts goja.Value) goja.ArrayBuffer { parsedOpts := NewPageScreenshotOptions() if err := parsedOpts.Parse(p.ctx, opts); err != nil { - k6.Panic(p.ctx, "failed parsing screenshot options: %w", err) + k6ext.Panic(p.ctx, "failed parsing screenshot options: %w", err) } s := newScreenshotter(p.ctx) buf, err := s.screenshotPage(p, parsedOpts) if err != nil { - k6.Panic(p.ctx, "cannot capture screenshot: %w", err) + k6ext.Panic(p.ctx, "cannot capture screenshot: %w", err) } rt := p.vu.Runtime() return rt.NewArrayBuffer(*buf) @@ -772,7 +772,7 @@ func (p *Page) SetExtraHTTPHeaders(headers map[string]string) { } func (p *Page) SetInputFiles(selector string, files goja.Value, opts goja.Value) { - k6.Panic(p.ctx, "Page.textContent(selector, opts) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.textContent(selector, opts) has not been implemented yet") // TODO: needs slowMo } @@ -782,10 +782,10 @@ func (p *Page) SetViewportSize(viewportSize goja.Value) { s := &Size{} if err := s.Parse(p.ctx, viewportSize); err != nil { - k6.Panic(p.ctx, "error parsing viewport size: %w", err) + k6ext.Panic(p.ctx, "error parsing viewport size: %w", err) } if err := p.setViewportSize(s); err != nil { - k6.Panic(p.ctx, "error setting viewport size: %w", err) + k6ext.Panic(p.ctx, "error setting viewport size: %w", err) } applySlowMo(p.ctx) } @@ -823,7 +823,7 @@ func (p *Page) Uncheck(selector string, opts goja.Value) { } func (p *Page) Unroute(url goja.Value, handler goja.Callable) { - k6.Panic(p.ctx, "Page.unroute(url, handler) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.unroute(url, handler) has not been implemented yet") } // URL returns the location of the page. @@ -834,7 +834,7 @@ func (p *Page) URL() string { // Video returns information of recorded video. func (p *Page) Video() api.Video { - k6.Panic(p.ctx, "Page.video() has not been implemented yet") + k6ext.Panic(p.ctx, "Page.video() has not been implemented yet") return nil } @@ -851,7 +851,7 @@ func (p *Page) ViewportSize() map[string]float64 { // WaitForEvent waits for the specified event to trigger. func (p *Page) WaitForEvent(event string, optsOrPredicate goja.Value) interface{} { - k6.Panic(p.ctx, "Page.waitForEvent(event, optsOrPredicate) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.waitForEvent(event, optsOrPredicate) has not been implemented yet") return nil } @@ -877,12 +877,12 @@ func (p *Page) WaitForNavigation(opts goja.Value) api.Response { } func (p *Page) WaitForRequest(urlOrPredicate, opts goja.Value) api.Request { - k6.Panic(p.ctx, "Page.waitForRequest(urlOrPredicate, opts) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.waitForRequest(urlOrPredicate, opts) has not been implemented yet") return nil } func (p *Page) WaitForResponse(urlOrPredicate, opts goja.Value) api.Response { - k6.Panic(p.ctx, "Page.waitForResponse(urlOrPredicate, opts) has not been implemented yet") + k6ext.Panic(p.ctx, "Page.waitForResponse(urlOrPredicate, opts) has not been implemented yet") return nil } diff --git a/common/page_options.go b/common/page_options.go index 835ba2d13..e1fd483dd 100644 --- a/common/page_options.go +++ b/common/page_options.go @@ -26,10 +26,9 @@ import ( "strings" "time" - "github.com/grafana/xk6-browser/k6" - "github.com/chromedp/cdproto/page" "github.com/dop251/goja" + "github.com/grafana/xk6-browser/k6ext" ) type PageEmulateMediaOptions struct { @@ -61,7 +60,7 @@ func NewPageEmulateMediaOptions(defaultMedia MediaType, defaultColorScheme Color } func (o *PageEmulateMediaOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -86,7 +85,7 @@ func NewPageReloadOptions(defaultWaitUntil LifecycleEvent, defaultTimeout time.D } func (o *PageReloadOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { opts := opts.ToObject(rt) for _, k := range opts.Keys() { @@ -118,7 +117,7 @@ func NewPageScreenshotOptions() *PageScreenshotOptions { } func (o *PageScreenshotOptions) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) { formatSpecified := false opts := opts.ToObject(rt) diff --git a/common/remote_object.go b/common/remote_object.go index beb3506ad..da83cec8d 100644 --- a/common/remote_object.go +++ b/common/remote_object.go @@ -29,7 +29,7 @@ import ( "strconv" "strings" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" cdpruntime "github.com/chromedp/cdproto/runtime" "github.com/dop251/goja" @@ -150,7 +150,7 @@ func valueFromRemoteObject(ctx context.Context, robj *cdpruntime.RemoteObject) ( if val == "undefined" { return goja.Undefined(), err } - return k6.Runtime(ctx).ToValue(val), err + return k6ext.Runtime(ctx).ToValue(val), err } func handleParseRemoteObjectErr(ctx context.Context, err error, logger *logrus.Entry) { @@ -161,7 +161,7 @@ func handleParseRemoteObjectErr(ctx context.Context, err error, logger *logrus.E merr, ok := err.(*multierror.Error) if !ok { // If this panics it's a bug :) - k6.Panic(ctx, "unable to parse remote object value: %w", err) + k6ext.Panic(ctx, "unable to parse remote object value: %w", err) } for _, e := range merr.Errors { switch { @@ -171,7 +171,7 @@ func handleParseRemoteObjectErr(ctx context.Context, err error, logger *logrus.E logger.WithError(ope).Error() default: // If this panics it's a bug :) - k6.Panic(ctx, "unable to parse remote object value: %w", e) + k6ext.Panic(ctx, "unable to parse remote object value: %w", e) } } } diff --git a/common/remote_object_test.go b/common/remote_object_test.go index e97440131..a7b91994f 100644 --- a/common/remote_object_test.go +++ b/common/remote_object_test.go @@ -25,7 +25,7 @@ import ( "math" "testing" - "github.com/grafana/xk6-browser/k6/k6test" + "github.com/grafana/xk6-browser/k6ext/k6test" "github.com/chromedp/cdproto/runtime" "github.com/dop251/goja" diff --git a/common/request.go b/common/request.go index 3c4eadd87..2276456c8 100644 --- a/common/request.go +++ b/common/request.go @@ -28,7 +28,7 @@ import ( "time" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" k6modules "go.k6.io/k6/js/modules" @@ -98,7 +98,7 @@ func NewRequest( errorText: "", timestamp: event.Timestamp.Time(), wallTime: event.WallTime.Time(), - vu: k6.GetVU(ctx), + vu: k6ext.GetVU(ctx), } for n, v := range event.Request.Headers { switch v := v.(type) { @@ -154,7 +154,7 @@ func (r *Request) AllHeaders() map[string]string { } func (r *Request) Failure() goja.Value { - k6.Panic(r.ctx, "Request.failure() has not been implemented yet") + k6ext.Panic(r.ctx, "Request.failure() has not been implemented yet") return nil } @@ -215,17 +215,17 @@ func (r *Request) PostDataBuffer() goja.ArrayBuffer { // PostDataJSON returns the request post data as a JS object. func (r *Request) PostDataJSON() string { - k6.Panic(r.ctx, "Request.postDataJSON() has not been implemented yet") + k6ext.Panic(r.ctx, "Request.postDataJSON() has not been implemented yet") return "" } func (r *Request) RedirectedFrom() api.Request { - k6.Panic(r.ctx, "Request.redirectedFrom() has not been implemented yet") + k6ext.Panic(r.ctx, "Request.redirectedFrom() has not been implemented yet") return nil } func (r *Request) RedirectedTo() api.Request { - k6.Panic(r.ctx, "Request.redirectedTo() has not been implemented yet") + k6ext.Panic(r.ctx, "Request.redirectedTo() has not been implemented yet") return nil } diff --git a/common/request_test.go b/common/request_test.go index e3bdc001c..2ac384a93 100644 --- a/common/request_test.go +++ b/common/request_test.go @@ -25,7 +25,7 @@ import ( "time" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6/k6test" + "github.com/grafana/xk6-browser/k6ext/k6test" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/network" diff --git a/common/response.go b/common/response.go index 78662d846..e4df48e84 100644 --- a/common/response.go +++ b/common/response.go @@ -29,7 +29,7 @@ import ( "time" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" k6modules "go.k6.io/k6/js/modules" @@ -85,7 +85,7 @@ type Response struct { // NewHTTPResponse creates a new HTTP response. func NewHTTPResponse(ctx context.Context, req *Request, resp *network.Response, timestamp *cdp.MonotonicTime) *Response { - vu := k6.GetVU(ctx) + vu := k6ext.GetVU(ctx) state := vu.State() r := Response{ ctx: ctx, @@ -184,10 +184,10 @@ func (r *Response) AllHeaders() map[string]string { // Body returns the response body as a binary buffer. func (r *Response) Body() goja.ArrayBuffer { if r.status >= 300 && r.status <= 399 { - k6.Panic(r.ctx, "Response body is unavailable for redirect responses") + k6ext.Panic(r.ctx, "Response body is unavailable for redirect responses") } if err := r.fetchBody(); err != nil { - k6.Panic(r.ctx, "error getting response body: %w", err) + k6ext.Panic(r.ctx, "error getting response body: %w", err) } r.bodyMu.RLock() defer r.bodyMu.RUnlock() @@ -215,7 +215,7 @@ func (r *Response) bodySize() int64 { // Finished waits for response to finish, return error if request failed. func (r *Response) Finished() bool { // TODO: should return nil|Error - k6.Panic(r.ctx, "Response.finished() has not been implemented yet") + k6ext.Panic(r.ctx, "Response.finished() has not been implemented yet") return false } @@ -277,14 +277,14 @@ func (r *Response) HeadersArray() []api.HTTPHeader { func (r *Response) JSON() goja.Value { if r.cachedJSON == nil { if err := r.fetchBody(); err != nil { - k6.Panic(r.ctx, "error getting response body: %w", err) + k6ext.Panic(r.ctx, "error getting response body: %w", err) } var v interface{} r.bodyMu.RLock() defer r.bodyMu.RUnlock() if err := json.Unmarshal(r.body, &v); err != nil { - k6.Panic(r.ctx, "error unmarshalling response body to JSON: %w", err) + k6ext.Panic(r.ctx, "error unmarshalling response body to JSON: %w", err) } r.cachedJSON = v } @@ -336,7 +336,7 @@ func (r *Response) StatusText() string { // Text returns the response body as a string. func (r *Response) Text() string { if err := r.fetchBody(); err != nil { - k6.Panic(r.ctx, "error getting response body as text: %w", err) + k6ext.Panic(r.ctx, "error getting response body as text: %w", err) } r.bodyMu.RLock() defer r.bodyMu.RUnlock() diff --git a/common/touchscreen.go b/common/touchscreen.go index 1b898699b..0fa083051 100644 --- a/common/touchscreen.go +++ b/common/touchscreen.go @@ -24,7 +24,7 @@ import ( "context" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/input" @@ -69,6 +69,6 @@ func (t *Touchscreen) tap(x float64, y float64) error { // Tap dispatches a tap start and tap end event. func (t *Touchscreen) Tap(x float64, y float64) { if err := t.tap(x, y); err != nil { - k6.Panic(t.ctx, "unable to tap: %w", err) + k6ext.Panic(t.ctx, "unable to tap: %w", err) } } diff --git a/common/types.go b/common/types.go index dda752b10..1a5bb5d71 100644 --- a/common/types.go +++ b/common/types.go @@ -30,7 +30,7 @@ import ( "strings" "github.com/grafana/xk6-browser/api" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" "github.com/dop251/goja" ) @@ -159,7 +159,7 @@ func NewGeolocation() *Geolocation { } func (g *Geolocation) Parse(ctx context.Context, opts goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) longitude := 0.0 latitude := 0.0 accuracy := 0.0 @@ -459,7 +459,7 @@ type Screen struct { } func (s *Screen) Parse(ctx context.Context, screen goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if screen != nil && !goja.IsUndefined(screen) && !goja.IsNull(screen) { screen := screen.ToObject(rt) for _, k := range screen.Keys() { @@ -493,7 +493,7 @@ func (s Size) enclosingIntSize() *Size { } func (s *Size) Parse(ctx context.Context, viewport goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if viewport != nil && !goja.IsUndefined(viewport) && !goja.IsNull(viewport) { viewport := viewport.ToObject(rt) for _, k := range viewport.Keys() { @@ -516,7 +516,7 @@ type Viewport struct { // Parse viewport details from a given goja viewport value. func (v *Viewport) Parse(ctx context.Context, viewport goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if viewport != nil && !goja.IsUndefined(viewport) && !goja.IsNull(viewport) { viewport := viewport.ToObject(rt) for _, k := range viewport.Keys() { @@ -561,7 +561,7 @@ func NewCredentials() *Credentials { } func (c *Credentials) Parse(ctx context.Context, credentials goja.Value) error { - rt := k6.Runtime(ctx) + rt := k6ext.Runtime(ctx) if credentials != nil && !goja.IsUndefined(credentials) && !goja.IsNull(credentials) { credentials := credentials.ToObject(rt) for _, k := range credentials.Keys() { diff --git a/k6/context.go b/k6ext/context.go similarity index 99% rename from k6/context.go rename to k6ext/context.go index 57150ac2a..8138898c6 100644 --- a/k6/context.go +++ b/k6ext/context.go @@ -1,4 +1,4 @@ -package k6 +package k6ext import ( "context" diff --git a/k6/k6test/vu.go b/k6ext/k6test/vu.go similarity index 95% rename from k6/k6test/vu.go rename to k6ext/k6test/vu.go index b1749786d..adb7ba7b4 100644 --- a/k6/k6test/vu.go +++ b/k6ext/k6test/vu.go @@ -4,7 +4,7 @@ import ( "context" "testing" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" k6common "go.k6.io/k6/js/common" k6eventloop "go.k6.io/k6/js/eventloop" @@ -66,7 +66,7 @@ func NewVU(tb testing.TB) *VU { StateField: state, }, } - ctx := k6.WithVU(context.Background(), vu) + ctx := k6ext.WithVU(context.Background(), vu) vu.CtxField = ctx loop := k6eventloop.New(vu) diff --git a/k6/metrics.go b/k6ext/metrics.go similarity index 98% rename from k6/metrics.go rename to k6ext/metrics.go index 169fc15f4..28240d094 100644 --- a/k6/metrics.go +++ b/k6ext/metrics.go @@ -1,4 +1,4 @@ -package k6 +package k6ext import k6metrics "go.k6.io/k6/metrics" diff --git a/k6/panic.go b/k6ext/panic.go similarity index 98% rename from k6/panic.go rename to k6ext/panic.go index 105c95ace..541b4828a 100644 --- a/k6/panic.go +++ b/k6ext/panic.go @@ -1,4 +1,4 @@ -package k6 +package k6ext import ( "context" diff --git a/main.go b/main.go index b2bbde0a6..1c3cad5ee 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ import ( "github.com/grafana/xk6-browser/api" "github.com/grafana/xk6-browser/chromium" "github.com/grafana/xk6-browser/common" - "github.com/grafana/xk6-browser/k6" + "github.com/grafana/xk6-browser/k6ext" k6common "go.k6.io/k6/js/common" k6modules "go.k6.io/k6/js/modules" @@ -44,7 +44,7 @@ type ( // JSModule is the entrypoint into the browser JS module. JSModule struct { vu k6modules.VU - k6Metrics *k6.CustomMetrics + k6Metrics *k6ext.CustomMetrics Devices map[string]common.Device Version string } @@ -68,7 +68,7 @@ func New() *RootModule { // NewModuleInstance implements the k6modules.Module interface to return // a new instance for each VU. func (*RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance { - k6m := k6.RegisterCustomMetrics(vu.InitEnv().Registry) + k6m := k6ext.RegisterCustomMetrics(vu.InitEnv().Registry) return &ModuleInstance{ mod: &JSModule{ vu: vu, @@ -96,8 +96,8 @@ func (m *JSModule) Launch(browserName string, opts goja.Value) api.Browser { <-ctx.Done() }()*/ - ctx := k6.WithVU(m.vu.Context(), m.vu) - ctx = k6.WithCustomMetrics(ctx, m.k6Metrics) + ctx := k6ext.WithVU(m.vu.Context(), m.vu) + ctx = k6ext.WithCustomMetrics(ctx, m.k6Metrics) if browserName == "chromium" { bt := chromium.NewBrowserType(ctx) diff --git a/tests/test_browser.go b/tests/test_browser.go index 136c57211..495d8e344 100644 --- a/tests/test_browser.go +++ b/tests/test_browser.go @@ -29,8 +29,8 @@ import ( "github.com/grafana/xk6-browser/api" "github.com/grafana/xk6-browser/chromium" - "github.com/grafana/xk6-browser/k6" - "github.com/grafana/xk6-browser/k6/k6test" + "github.com/grafana/xk6-browser/k6ext" + "github.com/grafana/xk6-browser/k6ext/k6test" k6http "go.k6.io/k6/js/modules/k6/http" k6httpmultibin "go.k6.io/k6/lib/testutils/httpmultibin" @@ -96,13 +96,13 @@ func newTestBrowser(tb testing.TB, opts ...interface{}) *testBrowser { vu.CtxField = dummyCtx } else { // Attach the mock VU to the passed context - ctx = k6.WithVU(ctx, vu) + ctx = k6ext.WithVU(ctx, vu) vu.CtxField = ctx } registry := k6metrics.NewRegistry() - k6m := k6.RegisterCustomMetrics(registry) - vu.CtxField = k6.WithCustomMetrics(vu.Context(), k6m) + k6m := k6ext.RegisterCustomMetrics(registry) + vu.CtxField = k6ext.WithCustomMetrics(vu.Context(), k6m) var ( state = vu.StateField