Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor query #1107

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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, common.StrictModeOff)
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, common.StrictModeOff)
if err != nil {
return nil, err //nolint:wrapcheck
}
Expand Down
4 changes: 4 additions & 0 deletions common/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ const (
// Life-cycle consts

LifeCycleNetworkIdleTimeout time.Duration = 500 * time.Millisecond

// API default consts.

StrictModeOff = false
)
8 changes: 4 additions & 4 deletions common/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,21 +1023,21 @@ 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)
}
fn := `
(node, injected, selector) => {
return injected.querySelector(selector, node || document, false);
(node, injected, selector, strict) => {
return injected.querySelector(selector, strict, node || document);
}
`
opts := evalOptions{
forceCallable: true,
returnByValue: false,
}
result, err := h.evalWithScript(h.ctx, opts, fn, parsedSelector)
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/js/injected_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ class InjectedScript {
);
}

querySelector(selector, root, strict) {
querySelector(selector, strict, root) {
if (!root["querySelector"]) {
return "error:notqueryablenode";
}
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, StrictModeOff)
}

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