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

Fix error on page.query when no matches with given selector #1101

Merged
merged 3 commits into from
Nov 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping {
if err != nil {
return nil, err //nolint:wrapcheck
}
// ElementHandle can be null when the selector does not match any elements.
// We do not want to map nil elementHandles since the expectation is a
// null result in the test script for this case.
if eh == nil {
ka3de marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil //nolint:nilnil
}
ehm := mapElementHandle(vu, eh)
return ehm, nil
}
Expand Down Expand Up @@ -396,6 +402,12 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping {
if err != nil {
return nil, err //nolint:wrapcheck
}
// ElementHandle can be null when the selector does not match any elements.
// We do not want to map nil elementHandles since the expectation is a
// null result in the test script for this case.
if eh == nil {
return nil, nil //nolint:nilnil
}
ehm := mapElementHandle(vu, eh)
return ehm, nil
}
Expand Down Expand Up @@ -594,6 +606,12 @@ func mapPage(vu moduleVU, p *common.Page) mapping {
if err != nil {
return nil, err //nolint:wrapcheck
}
// ElementHandle can be null when the selector does not match any elements.
// We do not want to map nil elementHandles since the expectation is a
// null result in the test script for this case.
if eh == nil {
return nil, nil //nolint:nilnil
}
ehm := mapElementHandle(vu, eh)
return ehm, nil
}
Expand Down
2 changes: 1 addition & 1 deletion common/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ func (h *ElementHandle) Query(selector string) (*ElementHandle, error) {
return nil, fmt.Errorf("querying selector %q: %w", selector, err)
}
if result == nil {
return nil, fmt.Errorf("querying selector %q", selector)
return nil, nil //nolint:nilnil
}
handle, ok := result.(JSHandleAPI)
if !ok {
Expand Down
12 changes: 12 additions & 0 deletions tests/element_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,15 @@ func TestElementHandlePress(t *testing.T) {

require.Equal(t, "AbC", el.InputValue(nil))
}

func TestElementHandleQuery(t *testing.T) {
t.Parallel()

p := newTestBrowser(t).NewPage(nil)
p.SetContent(`<div id="foo">hello</div>`, nil)

element, err := p.Query("bar")

require.NoError(t, err)
require.Nil(t, element)
}
Loading