Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Add error handling to FrameElement
Browse files Browse the repository at this point in the history
Updates: #804
Related: #688
  • Loading branch information
inancgumus committed Mar 14, 2023
1 parent dac28f8 commit 62ca4d1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion api/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Frame interface {
EvaluateHandle(pageFunc goja.Value, args ...goja.Value) (JSHandle, error)
Fill(selector string, value string, opts goja.Value)
Focus(selector string, opts goja.Value)
FrameElement() ElementHandle
FrameElement() (ElementHandle, error)
GetAttribute(selector string, name string, opts goja.Value) goja.Value
Goto(url string, opts goja.Value) (Response, error)
Hover(selector string, opts goja.Value)
Expand Down
9 changes: 6 additions & 3 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,12 @@ func mapFrame(vu moduleVU, f api.Frame) mapping {
},
"fill": f.Fill,
"focus": f.Focus,
"frameElement": func() *goja.Object {
eh := mapElementHandle(vu, f.FrameElement())
return rt.ToValue(eh).ToObject(rt)
"frameElement": func() (mapping, error) {
fe, err := f.FrameElement()
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapElementHandle(vu, fe), nil
},
"getAttribute": f.GetAttribute,
"goto": func(url string, opts goja.Value) *goja.Promise {
Expand Down
15 changes: 10 additions & 5 deletions common/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,23 @@ func (h *ElementHandle) boundingBox() (*Rect, error) {
y := math.Min(quad[1], math.Min(quad[3], math.Min(quad[5], quad[7])))
width := math.Max(quad[0], math.Max(quad[2], math.Max(quad[4], quad[6]))) - x
height := math.Max(quad[1], math.Max(quad[3], math.Max(quad[5], quad[7]))) - y
position := h.frame.position()

position, err := h.frame.position()
if err != nil {
return nil, err
}

return &Rect{X: x + position.X, Y: y + position.Y, Width: width, Height: height}, nil
}

func (h *ElementHandle) checkHitTargetAt(apiCtx context.Context, point Position) (bool, error) {
frame := h.ownerFrame(apiCtx)
if frame != nil && frame.parentFrame != nil {
var (
el = h.frame.FrameElement()
element, ok = el.(*ElementHandle)
)
el, err := h.frame.FrameElement()
if err != nil {
return false, err
}
element, ok := el.(*ElementHandle)
if !ok {
return false, fmt.Errorf("unexpected type %T", el)
}
Expand Down
22 changes: 14 additions & 8 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,17 +358,22 @@ func (f *Frame) onLoadingStopped() {
// website never stops performing network requests.
}

func (f *Frame) position() *Position {
func (f *Frame) position() (*Position, error) {
frame := f.manager.getFrameByID(cdp.FrameID(f.page.targetID))
if frame == nil {
return nil
return nil, fmt.Errorf("could not find frame with id %s", f.page.targetID)
}
if frame == f.page.frameManager.MainFrame() {
return &Position{X: 0, Y: 0}
return &Position{X: 0, Y: 0}, nil
}
element, err := frame.FrameElement()
if err != nil {
return nil, err
}
element := frame.FrameElement()

box := element.BoundingBox()
return &Position{X: box.X, Y: box.Y}

return &Position{X: box.X, Y: box.Y}, nil
}

func (f *Frame) removeChildFrame(child *Frame) {
Expand Down Expand Up @@ -826,14 +831,15 @@ func (f *Frame) focus(selector string, opts *FrameBaseOptions) error {
return nil
}

func (f *Frame) FrameElement() api.ElementHandle {
// FrameElement returns the element handle for the frame.
func (f *Frame) FrameElement() (api.ElementHandle, error) {
f.log.Debugf("Frame:FrameElement", "fid:%s furl:%q", f.ID(), f.URL())

element, err := f.page.getFrameElement(f)
if err != nil {
k6ext.Panic(f.ctx, "getting frame element: %w", err)
return nil, fmt.Errorf("getting frame element: %w", err)
}
return element
return element, nil
}

// GetAttribute of the first element found that matches the selector.
Expand Down

0 comments on commit 62ca4d1

Please sign in to comment.