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 JSONValue to use parseConsoleRemoteObject #1190

Merged
merged 7 commits into from
Jan 29, 2024
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
6 changes: 5 additions & 1 deletion common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,11 @@ func (f *Frame) selectOption(selector string, values goja.Value, opts *FrameSele
}
vals := make([]string, 0, len(optHandles))
for _, oh := range optHandles {
vals = append(vals, oh.JSONValue())
val, err := oh.JSONValue()
if err != nil {
return nil, fmt.Errorf("reading value: %w", err)
}
vals = append(vals, val)
if err := oh.dispose(); err != nil {
return nil, fmt.Errorf("optionHandle.dispose: %w", err)
}
Expand Down
2 changes: 2 additions & 0 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ func (fs *FrameSession) onConsoleAPICalled(event *cdpruntime.EventConsoleAPICall
case "error":
l.Error()
default:
// this is where debug & other console.* apis will default to (such as
// console.table).
l.Debug()
}
}
Expand Down
34 changes: 10 additions & 24 deletions common/js_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"context"
"encoding/json"
"fmt"

"github.com/grafana/xk6-browser/k6ext"
Expand All @@ -25,7 +24,7 @@ type JSHandleAPI interface {
EvaluateHandle(pageFunc string, args ...any) (JSHandleAPI, error)
GetProperties() (map[string]JSHandleAPI, error)
GetProperty(propertyName string) JSHandleAPI
JSONValue() string
JSONValue() (string, error)
ObjectID() cdpruntime.RemoteObjectID
}

Expand Down Expand Up @@ -163,38 +162,25 @@ func (h *BaseJSHandle) GetProperty(_ string) JSHandleAPI {
}

// JSONValue returns a JSON version of this JS handle.
func (h *BaseJSHandle) JSONValue() string { // TODO: return error
convertToString := func(ro *cdpruntime.RemoteObject) string {
res, err := valueFromRemoteObject(h.ctx, ro)
if err != nil {
k6ext.Panic(h.ctx, "extracting value from remote object: %w", err)
}
switch v := res.(type) {
case string:
return v
default:
buf, err := json.Marshal(v)
if err != nil {
k6ext.Panic(h.ctx, "marshaling value: %w", err)
}
return string(buf)
}
}
func (h *BaseJSHandle) JSONValue() (string, error) {
remoteObject := h.remoteObject
if remoteObject.ObjectID != "" {
var result *runtime.RemoteObject
var err error
action := runtime.CallFunctionOn("function() { return this; }").
WithReturnByValue(true).
WithAwaitPromise(true).
WithObjectID(h.remoteObject.ObjectID)
if result, _, err = action.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil {
k6ext.Panic(h.ctx, "getting properties for JS handle: %w", err)
if remoteObject, _, err = action.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil {
return "", fmt.Errorf("retrieving json value: %w", err)
}
remoteObject = result
}

return convertToString(remoteObject)
res, err := parseConsoleRemoteObject(h.logger, remoteObject)
if err != nil {
return "", fmt.Errorf("extracting json value (remote object id: %v): %w", remoteObject.ObjectID, err)
}

return res, nil
}

// ObjectID returns the remote object ID.
Expand Down
3 changes: 2 additions & 1 deletion tests/js_handle_get_properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestJSHandleGetProperties(t *testing.T) {
props, err := handle.GetProperties()
require.NoError(t, err, "expected no error when getting properties")

value := props["prop1"].JSONValue()
value, err := props["prop1"].JSONValue()
assert.NoError(t, err, "expected no error when getting JSONValue")
assert.Equal(t, value, "one", `expected property value of "one", got %q`, value)
}
28 changes: 21 additions & 7 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "log", cm.Type)
assert.Equal(t, "this is a log message", cm.Text)
assert.Equal(t, "this is a log message", cm.Args[0].JSONValue())
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "this is a log message", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
},
},
Expand All @@ -863,7 +865,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "debug", cm.Type)
assert.Equal(t, "this is a debug message", cm.Text)
assert.Equal(t, "this is a debug message", cm.Args[0].JSONValue())
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "this is a debug message", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
},
},
Expand All @@ -874,7 +878,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "info", cm.Type)
assert.Equal(t, "this is an info message", cm.Text)
assert.Equal(t, "this is an info message", cm.Args[0].JSONValue())
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "this is an info message", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
},
},
Expand All @@ -885,7 +891,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "error", cm.Type)
assert.Equal(t, "this is an error message", cm.Text)
assert.Equal(t, "this is an error message", cm.Args[0].JSONValue())
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "this is an error message", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
},
},
Expand All @@ -896,7 +904,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "warning", cm.Type)
assert.Equal(t, "this is a warning message", cm.Text)
assert.Equal(t, "this is a warning message", cm.Args[0].JSONValue())
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "this is a warning message", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
},
},
Expand Down Expand Up @@ -927,7 +937,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "table", cm.Type)
assert.Equal(t, "Array(2)", cm.Text)
assert.Equal(t, `[["Grafana","k6"],["Grafana","Mimir"]]`, cm.Args[0].JSONValue())
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, `[["Grafana","k6"],["Grafana","Mimir"]]`, val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
},
},
Expand All @@ -938,7 +950,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "trace", cm.Type)
assert.Equal(t, "trace example", cm.Text)
assert.Equal(t, "trace example", cm.Args[0].JSONValue())
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "trace example", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
},
},
Expand Down
Loading