Skip to content

Commit

Permalink
Update JSHandle.Evaluate to return err
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Jun 4, 2024
1 parent f6dbee4 commit 3523f93
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion browser/js_handle_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions common/js_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"

"github.com/grafana/xk6-browser/k6ext"
"github.com/grafana/xk6-browser/log"

"github.com/chromedp/cdproto/cdp"
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 15 additions & 3 deletions common/screenshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -302,6 +313,7 @@ func (s *screenshotter) screenshotElement(h *ElementHandle, opts *ElementHandleS
return nil, fmt.Errorf("restoring viewport: %w", err)
}
}

return buf, nil
}

Expand Down

0 comments on commit 3523f93

Please sign in to comment.