Skip to content

Commit

Permalink
Improve null detection in parseRemoteObjectValue
Browse files Browse the repository at this point in the history
Previously, we can't detect a null value:

        v, _ := page.GetAttribute("#el", "missing", nil)
        assert.Nil(t, nil, v) // v is not nil

Now, `v` will be `nil` instead of "null" as a string. This enables us to
detect `nil` in methods like GetAttribute, TextNode, etc. to tell the
user that what they're looking for does not exist instead of returning
"null" or an empty string.
  • Loading branch information
inancgumus committed Jun 5, 2024
1 parent d328f6a commit d6e3ca8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions common/remote_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/grafana/xk6-browser/log"

"github.com/chromedp/cdproto/runtime"
cdpruntime "github.com/chromedp/cdproto/runtime"
)

Expand Down Expand Up @@ -105,8 +106,8 @@ func parseRemoteObjectValue(
if val == "Object" {
return val, nil
}
if st == "null" {
return "null", nil
if st == runtime.SubtypeNull {
return nil, nil //nolint:nilnil
}
case cdpruntime.TypeUndefined:
return "undefined", nil
Expand Down
16 changes: 15 additions & 1 deletion common/remote_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ func TestValueFromRemoteObject(t *testing.T) {
require.Nil(t, arg)
})

t.Run("null", func(t *testing.T) {
t.Parallel()

vu := k6test.NewVU(t)
remoteObject := &runtime.RemoteObject{
Type: cdpruntime.TypeObject,
Subtype: cdpruntime.SubtypeNull,
}

arg, err := valueFromRemoteObject(vu.Context(), remoteObject)
require.NoError(t, err)
require.Nil(t, arg)
})

t.Run("primitive types", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -236,7 +250,7 @@ func TestParseRemoteObject(t *testing.T) {
name: "null",
subtype: "null",
value: nil,
expected: "null",
expected: nil,
},
}

Expand Down
2 changes: 1 addition & 1 deletion tests/remote_obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestEvalRemoteObjectParse(t *testing.T) {
name: "empty", eval: "", want: "",
},
{
name: "null", eval: "null", want: "null",
name: "null", eval: "null", want: nil,
},
{
name: "undefined", eval: "undefined", want: nil,
Expand Down

0 comments on commit d6e3ca8

Please sign in to comment.