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

Async JSHandle #1366

Merged
merged 7 commits into from
Jun 5, 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
69 changes: 41 additions & 28 deletions browser/js_handle_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,56 @@ import (
"github.com/dop251/goja"

"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6ext"
)

// mapJSHandle to the JS module.
func mapJSHandle(vu moduleVU, jsh common.JSHandleAPI) mapping {
rt := vu.Runtime()
return mapping{
"asElement": func() *goja.Object {
m := mapElementHandle(vu, jsh.AsElement())
return rt.ToValue(m).ToObject(rt)
"asElement": func() mapping {
return mapElementHandle(vu, jsh.AsElement())
},
"dispose": jsh.Dispose,
"evaluate": func(pageFunc goja.Value, gargs ...goja.Value) (any, error) {
args := make([]any, 0, len(gargs))
for _, a := range gargs {
args = append(args, exportArg(a))
}
return jsh.Evaluate(pageFunc.String(), args...)
"dispose": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, jsh.Dispose() //nolint:wrapcheck
})
},
"evaluateHandle": func(pageFunc goja.Value, gargs ...goja.Value) (mapping, error) {
h, err := jsh.EvaluateHandle(pageFunc.String(), exportArgs(gargs)...)
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapJSHandle(vu, h), nil
"evaluate": func(pageFunc goja.Value, gargs ...goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
args := make([]any, 0, len(gargs))
for _, a := range gargs {
args = append(args, exportArg(a))
}
return jsh.Evaluate(pageFunc.String(), args...) //nolint:wrapcheck
})
},
"getProperties": func() (mapping, error) {
props, err := jsh.GetProperties()
if err != nil {
return nil, err //nolint:wrapcheck
}
"evaluateHandle": func(pageFunc goja.Value, gargs ...goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
h, err := jsh.EvaluateHandle(pageFunc.String(), exportArgs(gargs)...)
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapJSHandle(vu, h), nil
})
},
"getProperties": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
props, err := jsh.GetProperties()
if err != nil {
return nil, err //nolint:wrapcheck
}

dst := make(map[string]any)
for k, v := range props {
dst[k] = mapJSHandle(vu, v)
}
return dst, nil
dst := make(map[string]any)
for k, v := range props {
dst[k] = mapJSHandle(vu, v)
}
return dst, nil
})
},
"jsonValue": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return jsh.JSONValue() //nolint:wrapcheck
})
},
"jsonValue": jsh.JSONValue,
}
}
8 changes: 5 additions & 3 deletions examples/pageon.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ export default async function() {
try {
await page.goto('https://test.k6.io/');

page.on('console', msg => {
page.on('console', async(msg) => {
const jsonValue1 = await msg.args()[0].jsonValue();
const jsonValue2 = await msg.args()[1].jsonValue();
check(msg, {
'assertConsoleMessageType': msg => msg.type() == 'log',
'assertConsoleMessageText': msg => msg.text() == 'this is a console.log message 42',
'assertConsoleMessageArgs0': msg => msg.args()[0].jsonValue() == 'this is a console.log message',
'assertConsoleMessageArgs1': msg => msg.args()[1].jsonValue() == 42,
'assertConsoleMessageArgs0': msg => jsonValue1 == 'this is a console.log message',
'assertConsoleMessageArgs1': msg => jsonValue2 == 42,
});
});

Expand Down
Loading