diff --git a/browser/js_handle_mapping.go b/browser/js_handle_mapping.go index a5a11389d..691cabdb4 100644 --- a/browser/js_handle_mapping.go +++ b/browser/js_handle_mapping.go @@ -15,7 +15,7 @@ func mapJSHandle(vu moduleVU, jsh common.JSHandleAPI) mapping { return rt.ToValue(m).ToObject(rt) }, "dispose": jsh.Dispose, - "evaluate": func(pageFunc goja.Value, gargs ...goja.Value) any { + "evaluate": func(pageFunc goja.Value, gargs ...goja.Value) (any, error) { args := make([]any, 0, len(gargs)) for _, a := range gargs { args = append(args, exportArg(a)) diff --git a/common/js_handle.go b/common/js_handle.go index 106af1f13..3f17b79ac 100644 --- a/common/js_handle.go +++ b/common/js_handle.go @@ -6,7 +6,6 @@ import ( "fmt" "strings" - "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" "github.com/chromedp/cdproto/cdp" @@ -22,7 +21,7 @@ import ( type JSHandleAPI interface { AsElement() *ElementHandle Dispose() error - Evaluate(pageFunc string, args ...any) any + Evaluate(pageFunc string, args ...any) (any, error) EvaluateHandle(pageFunc string, args ...any) (JSHandleAPI, error) GetProperties() (map[string]JSHandleAPI, error) JSONValue() (string, error) @@ -124,12 +123,13 @@ func (h *BaseJSHandle) dispose() error { } // Evaluate will evaluate provided page function within an execution context. -func (h *BaseJSHandle) Evaluate(pageFunc string, args ...any) any { +func (h *BaseJSHandle) Evaluate(pageFunc string, args ...any) (any, error) { res, err := h.execCtx.Eval(h.ctx, pageFunc, args...) if err != nil { - k6ext.Panic(h.ctx, "%w", err) + return nil, fmt.Errorf("evaluating element: %w", err) } - return res + + return res, nil } // EvaluateHandle will evaluate provided page function within an execution context. diff --git a/common/screenshotter.go b/common/screenshotter.go index d25b79a3b..4b4002146 100644 --- a/common/screenshotter.go +++ b/common/screenshotter.go @@ -282,18 +282,29 @@ func (s *screenshotter) screenshotElement(h *ElementHandle, opts *ElementHandleS } } - documentRect := bbox - scrollOffset := h.Evaluate(`() => { return {x: window.scrollX, y: window.scrollY};}`) + scrollOffset, err := h.Evaluate(`() => { return {x: window.scrollX, y: window.scrollY};}`) + if err != nil { + return nil, fmt.Errorf("evaluating scroll offset: %w", err) + } var returnVal Position if err := convert(scrollOffset, &returnVal); err != nil { return nil, fmt.Errorf("unpacking scroll offset: %w", err) } + documentRect := bbox documentRect.X += returnVal.X documentRect.Y += returnVal.Y - buf, err := s.screenshot(h.frame.page.session, documentRect.enclosingIntRect(), nil, format, opts.OmitBackground, opts.Quality, opts.Path) + buf, err := s.screenshot( + h.frame.page.session, + documentRect.enclosingIntRect(), + nil, // viewportRect + format, + opts.OmitBackground, + opts.Quality, + opts.Path, + ) if err != nil { return nil, err } @@ -302,6 +313,7 @@ func (s *screenshotter) screenshotElement(h *ElementHandle, opts *ElementHandleS return nil, fmt.Errorf("restoring viewport: %w", err) } } + return buf, nil }