Skip to content

Commit

Permalink
support FromNode with nodes which aren't frames
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdan committed Jul 23, 2020
1 parent 9d8af7a commit 446edaa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
15 changes: 11 additions & 4 deletions nav_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,23 @@ func TestQueryIframe(t *testing.T) {
ctx, cancel := testAllocate(t, "iframe.html")
defer cancel()

var iframes []*cdp.Node
var iframes, forms []*cdp.Node
if err := Run(ctx, Nodes(`iframe`, &iframes, ByQuery)); err != nil {
t.Fatal(err)
}
iframe := iframes[0]
if err := Run(ctx, Nodes(`#form`, &forms, ByQuery, FromNode(iframe))); err != nil {
t.Fatal(err)
}
form := forms[0]

var gotFoo string
if err := Run(ctx,
WaitVisible(`#form`, ByQuery, FromNode(iframes[0])),
Text("#foo", &gotFoo, ByQuery, FromNode(iframes[0])),
WaitVisible(`#form`, ByQuery, FromNode(iframe)),
Text("#foo", &gotFoo, ByQuery, FromNode(form)),

Click("#btn2", ByQuery, FromNode(iframes[0])),
Click("#btn1", ByQuery, FromNode(iframe)),
Click("#btn2", ByQuery, FromNode(form)),
); err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 5 additions & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ func (s *Selector) Do(ctx context.Context) error {
continue
}
} else {
execCtx = t.execContexts[fromNode.FrameID]
frameID := t.enclosingFrame(fromNode)
execCtx = t.execContexts[frameID]
t.frameMu.RUnlock()

// TODO: we probably want to use the nested frame
Expand Down Expand Up @@ -288,6 +289,9 @@ type QueryOption = func(*Selector)
// FromNode is an element query action option where a query will be run. That
// is, the query will only look at the node's element sub-tree. By default, or
// when passed nil, the document's root element will be used.
//
// Note that, at present, BySearch and ByJSPath do not support FromNode; this
// option is mainly useful for ByQuery selectors.
func FromNode(node *cdp.Node) QueryOption {
return func(s *Selector) { s.fromNode = node }
}
Expand Down

0 comments on commit 446edaa

Please sign in to comment.