Skip to content

Commit

Permalink
Update elementhandle.Query with strict param
Browse files Browse the repository at this point in the history
This will allow us to pass in the strict flag so that when necessary
we can enable strict mode. This change will be useful when it comes to
working with APIs which need to query for a single unique element only
and otherwise fail, usually with the locator API.
  • Loading branch information
ankur22 committed Nov 22, 2023
1 parent 07dd77e commit 8f949c6
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping {
},
}
maps["$"] = func(selector string) (mapping, error) {
eh, err := eh.Query(selector)
eh, err := eh.Query(selector, false)
if err != nil {
return nil, err //nolint:wrapcheck
}
Expand Down Expand Up @@ -398,7 +398,7 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping {
"waitForTimeout": f.WaitForTimeout,
}
maps["$"] = func(selector string) (mapping, error) {
eh, err := f.Query(selector)
eh, err := f.Query(selector, false)
if err != nil {
return nil, err //nolint:wrapcheck
}
Expand Down
4 changes: 2 additions & 2 deletions common/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ func (h *ElementHandle) Press(key string, opts goja.Value) {

// Query runs "element.querySelector" within the page. If no element matches the selector,
// the return value resolves to "null".
func (h *ElementHandle) Query(selector string) (*ElementHandle, error) {
func (h *ElementHandle) Query(selector string, strict bool) (*ElementHandle, error) {
parsedSelector, err := NewSelector(selector)
if err != nil {
k6ext.Panic(h.ctx, "parsing selector %q: %w", selector, err)
Expand All @@ -1037,7 +1037,7 @@ func (h *ElementHandle) Query(selector string) (*ElementHandle, error) {
forceCallable: true,
returnByValue: false,
}
result, err := h.evalWithScript(h.ctx, opts, fn, parsedSelector, false)
result, err := h.evalWithScript(h.ctx, opts, fn, parsedSelector, strict)
if err != nil {
return nil, fmt.Errorf("querying selector %q: %w", selector, err)
}
Expand Down
4 changes: 2 additions & 2 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -1355,14 +1355,14 @@ func (f *Frame) Name() string {

// Query runs a selector query against the document tree, returning the first matching element or
// "null" if no match is found.
func (f *Frame) Query(selector string) (*ElementHandle, error) {
func (f *Frame) Query(selector string, strict bool) (*ElementHandle, error) {
f.log.Debugf("Frame:Query", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)

document, err := f.document()
if err != nil {
k6ext.Panic(f.ctx, "getting document: %w", err)
}
return document.Query(selector)
return document.Query(selector, strict)
}

// QueryAll runs a selector query against the document tree, returning all matching elements.
Expand Down
2 changes: 1 addition & 1 deletion common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ func (p *Page) Press(selector string, key string, opts goja.Value) {
func (p *Page) Query(selector string) (*ElementHandle, error) {
p.logger.Debugf("Page:Query", "sid:%v selector:%s", p.sessionID(), selector)

return p.frameManager.MainFrame().Query(selector)
return p.frameManager.MainFrame().Query(selector, false)
}

// QueryAll returns all elements matching the specified selector.
Expand Down

0 comments on commit 8f949c6

Please sign in to comment.