From 2733ce26dc5691fb63e11c9803c642111b853192 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Wed, 15 Nov 2023 15:12:32 +0000 Subject: [PATCH 1/3] Fix error on nil result on page.query When page.query (page.$ in the js test script) is used, if no element matches the selector then it used to return an error. The expected behaviour is that it returns nil in this case. --- common/element_handle.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/element_handle.go b/common/element_handle.go index 5c5becc84..062e47125 100644 --- a/common/element_handle.go +++ b/common/element_handle.go @@ -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 { From c97deb656f4c8bd99dd86aa9c02a05d01d013972 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Wed, 15 Nov 2023 15:28:57 +0000 Subject: [PATCH 2/3] Update mapping to avoid mapping nil elementHandle If the result of query is nil, we shouldn't map this to an elementHandle, since the expectation on a nil result in query is null in the ks test script. If we map the nil result, the result is no longer null in the js test script. --- browser/mapping.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/browser/mapping.go b/browser/mapping.go index 08672e480..02d235aea 100644 --- a/browser/mapping.go +++ b/browser/mapping.go @@ -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 { + return nil, nil //nolint:nilnil + } ehm := mapElementHandle(vu, eh) return ehm, nil } @@ -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 } @@ -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 } From f4708eeffd87ce4e70969314ed6520435c58051f Mon Sep 17 00:00:00 2001 From: ankur22 Date: Wed, 15 Nov 2023 15:14:24 +0000 Subject: [PATCH 3/3] Add a integration test for page.query --- tests/element_handle_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/element_handle_test.go b/tests/element_handle_test.go index 4dd91dd29..5a8e790b6 100644 --- a/tests/element_handle_test.go +++ b/tests/element_handle_test.go @@ -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(`
hello
`, nil) + + element, err := p.Query("bar") + + require.NoError(t, err) + require.Nil(t, element) +}