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 ownerFrame
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 8d2f71c commit 5587bd7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ElementHandle interface {
IsEnabled() bool
IsHidden() bool
IsVisible() bool
OwnerFrame() Frame
OwnerFrame() (Frame, error)
Press(key string, opts goja.Value)
Query(selector string) (ElementHandle, error)
QueryAll(selector string) ([]ElementHandle, error)
Expand Down
10 changes: 6 additions & 4 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,12 @@ func mapElementHandle(vu moduleVU, eh api.ElementHandle) mapping {
"isEnabled": eh.IsEnabled,
"isHidden": eh.IsHidden,
"isVisible": eh.IsVisible,
"ownerFrame": func() *goja.Object {
f := eh.OwnerFrame()
mf := mapFrame(vu, f)
return rt.ToValue(mf).ToObject(rt)
"ownerFrame": func() (mapping, error) {
f, err := eh.OwnerFrame()
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapFrame(vu, f), nil
},
"press": eh.Press,
"screenshot": eh.Screenshot,
Expand Down
21 changes: 12 additions & 9 deletions common/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ func (h *ElementHandle) IsVisible() bool {
}

// OwnerFrame returns the frame containing this element.
func (h *ElementHandle) OwnerFrame() api.Frame {
func (h *ElementHandle) OwnerFrame() (api.Frame, error) {
fn := `
(node, injected) => {
return injected.getDocumentElement(node);
Expand All @@ -982,28 +982,31 @@ func (h *ElementHandle) OwnerFrame() api.Frame {
}
res, err := h.evalWithScript(h.ctx, opts, fn)
if err != nil {
k6ext.Panic(h.ctx, "getting document element: %w", err)
err = fmt.Errorf("getting document element: %w", err)
}
if res == nil {
return nil
if res == nil || err != nil {
return nil, err
}

documentHandle := res.(*ElementHandle)
documentHandle, ok := res.(*ElementHandle)
if !ok {
return nil, fmt.Errorf("unexpected result type while getting document element: %T", res)
}
defer documentHandle.Dispose()
if documentHandle.remoteObject.ObjectID == "" {
return nil
return nil, err
}

var node *cdp.Node
action := dom.DescribeNode().WithObjectID(documentHandle.remoteObject.ObjectID)
if node, err = action.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil {
k6ext.Panic(h.ctx, "getting node in frame: %w", err)
return nil, fmt.Errorf("getting node in frame: %w", err)
}
if node == nil || node.FrameID == "" {
return nil
return nil, fmt.Errorf("no frame found for node: %w", err)
}

return h.frame.manager.getFrameByID(node.FrameID)
return h.frame.manager.getFrameByID(node.FrameID), nil
}

func (h *ElementHandle) Press(key string, opts goja.Value) {
Expand Down

0 comments on commit 5587bd7

Please sign in to comment.