Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Change to using sobek instead of goja #1372

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 8 additions & 8 deletions browser/browser_context_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"reflect"

"github.com/dop251/goja"
"github.com/grafana/sobek"

"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6error"
Expand All @@ -16,7 +16,7 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
rt := vu.Runtime()
return mapping{
"addCookies": bc.AddCookies,
"addInitScript": func(script goja.Value) error {
"addInitScript": func(script sobek.Value) error {
if !gojaValueExists(script) {
return nil
}
Expand All @@ -25,15 +25,15 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
switch script.ExportType() {
case reflect.TypeOf(string("")):
source = script.String()
case reflect.TypeOf(goja.Object{}):
case reflect.TypeOf(sobek.Object{}):
opts := script.ToObject(rt)
for _, k := range opts.Keys() {
if k == "content" {
source = opts.Get(k).String()
}
}
default:
_, isCallable := goja.AssertFunction(script)
_, isCallable := sobek.AssertFunction(script)
if !isCallable {
source = fmt.Sprintf("(%s);", script.ToString().String())
} else {
Expand All @@ -48,7 +48,7 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
"clearPermissions": bc.ClearPermissions,
"close": bc.Close,
"cookies": bc.Cookies,
"grantPermissions": func(permissions []string, opts goja.Value) error {
"grantPermissions": func(permissions []string, opts sobek.Value) error {
pOpts := common.NewGrantPermissionsOptions()
pOpts.Parse(vu.Context(), opts)

Expand All @@ -59,7 +59,7 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
"setGeolocation": bc.SetGeolocation,
"setHTTPCredentials": bc.SetHTTPCredentials, //nolint:staticcheck
"setOffline": bc.SetOffline,
"waitForEvent": func(event string, optsOrPredicate goja.Value) (*goja.Promise, error) {
"waitForEvent": func(event string, optsOrPredicate sobek.Value) (*sobek.Promise, error) {
ctx := vu.Context()
popts := common.NewWaitForEventOptions(
bc.Timeout(),
Expand All @@ -81,7 +81,7 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
// before returning the result to the caller.
c := make(chan bool)
tq.Queue(func() error {
var resp goja.Value
var resp sobek.Value
resp, err = popts.PredicateFn(vu.Runtime().ToValue(p))
rtn = resp.ToBoolean()
close(c)
Expand All @@ -106,7 +106,7 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
return mapPage(vu, p), nil
}), nil
},
"pages": func() *goja.Object {
"pages": func() *sobek.Object {
var (
mpages []mapping
pages = bc.Pages()
Expand Down
6 changes: 3 additions & 3 deletions browser/browser_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package browser
import (
"fmt"

"github.com/dop251/goja"
"github.com/grafana/sobek"

"github.com/grafana/xk6-browser/common"
)
Expand Down Expand Up @@ -33,7 +33,7 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop
}
return b.IsConnected(), nil
},
"newContext": func(opts goja.Value) (*goja.Object, error) {
"newContext": func(opts sobek.Value) (*sobek.Object, error) {
b, err := vu.browser()
if err != nil {
return nil, err
Expand Down Expand Up @@ -65,7 +65,7 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop
}
return b.Version(), nil
},
"newPage": func(opts goja.Value) (mapping, error) {
"newPage": func(opts sobek.Value) (mapping, error) {
b, err := vu.browser()
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions browser/console_message_mapping.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package browser

import (
"github.com/dop251/goja"
"github.com/grafana/sobek"

"github.com/grafana/xk6-browser/common"
)
Expand All @@ -10,7 +10,7 @@ import (
func mapConsoleMessage(vu moduleVU, cm *common.ConsoleMessage) mapping {
rt := vu.Runtime()
return mapping{
"args": func() *goja.Object {
"args": func() *sobek.Object {
var (
margs []mapping
args = cm.Args
Expand All @@ -24,14 +24,14 @@ func mapConsoleMessage(vu moduleVU, cm *common.ConsoleMessage) mapping {
},
// page(), text() and type() are defined as
// functions in order to match Playwright's API
"page": func() *goja.Object {
"page": func() *sobek.Object {
mp := mapPage(vu, cm.Page)
return rt.ToValue(mp).ToObject(rt)
},
"text": func() *goja.Object {
"text": func() *sobek.Object {
return rt.ToValue(cm.Text).ToObject(rt)
},
"type": func() *goja.Object {
"type": func() *sobek.Object {
return rt.ToValue(cm.Type).ToObject(rt)
},
}
Expand Down
12 changes: 6 additions & 6 deletions browser/element_handle_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package browser
import (
"fmt"

"github.com/dop251/goja"
"github.com/grafana/sobek"

"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6ext"
Expand All @@ -17,7 +17,7 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint:
maps := mapping{
"boundingBox": eh.BoundingBox,
"check": eh.Check,
"click": func(opts goja.Value) (*goja.Promise, error) {
"click": func(opts sobek.Value) (*sobek.Promise, error) {
ctx := vu.Context()

popts := common.NewElementHandleClickOptions(eh.Timeout())
Expand All @@ -38,7 +38,7 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint:
return mapFrame(vu, f), nil
},
"dblclick": eh.Dblclick,
"dispatchEvent": func(typ string, eventInit goja.Value) error {
"dispatchEvent": func(typ string, eventInit sobek.Value) error {
return eh.DispatchEvent(typ, exportArg(eventInit)) //nolint:wrapcheck
},
"fill": eh.Fill,
Expand All @@ -62,7 +62,7 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint:
return mapFrame(vu, f), nil
},
"press": eh.Press,
"screenshot": func(opts goja.Value) (*goja.ArrayBuffer, error) {
"screenshot": func(opts sobek.Value) (*sobek.ArrayBuffer, error) {
ctx := vu.Context()

popts := common.NewElementHandleScreenshotOptions(eh.Timeout())
Expand All @@ -83,7 +83,7 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint:
"selectOption": eh.SelectOption,
"selectText": eh.SelectText,
"setInputFiles": eh.SetInputFiles,
"tap": func(opts goja.Value) (*goja.Promise, error) {
"tap": func(opts sobek.Value) (*sobek.Promise, error) {
popts := common.NewElementHandleTapOptions(eh.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
return nil, fmt.Errorf("parsing element tap options: %w", err)
Expand All @@ -96,7 +96,7 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint:
"type": eh.Type,
"uncheck": eh.Uncheck,
"waitForElementState": eh.WaitForElementState,
"waitForSelector": func(selector string, opts goja.Value) (mapping, error) {
"waitForSelector": func(selector string, opts sobek.Value) (mapping, error) {
eh, err := eh.WaitForSelector(selector, opts)
if err != nil {
return nil, err //nolint:wrapcheck
Expand Down
28 changes: 14 additions & 14 deletions browser/frame_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package browser
import (
"fmt"

"github.com/dop251/goja"
"github.com/grafana/sobek"

"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6ext"
Expand All @@ -16,7 +16,7 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop
rt := vu.Runtime()
maps := mapping{
"check": f.Check,
"childFrames": func() *goja.Object {
"childFrames": func() *sobek.Object {
var (
mcfs []mapping
cfs = f.ChildFrames()
Expand All @@ -26,7 +26,7 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop
}
return rt.ToValue(mcfs).ToObject(rt)
},
"click": func(selector string, opts goja.Value) (*goja.Promise, error) {
"click": func(selector string, opts sobek.Value) (*sobek.Promise, error) {
popts, err := parseFrameClickOptions(vu.Context(), opts, f.Timeout())
if err != nil {
return nil, err
Expand All @@ -39,17 +39,17 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop
},
"content": f.Content,
"dblclick": f.Dblclick,
"dispatchEvent": func(selector, typ string, eventInit, opts goja.Value) error {
"dispatchEvent": func(selector, typ string, eventInit, opts sobek.Value) error {
popts := common.NewFrameDispatchEventOptions(f.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
return fmt.Errorf("parsing frame dispatch event options: %w", err)
}
return f.DispatchEvent(selector, typ, exportArg(eventInit), popts) //nolint:wrapcheck
},
"evaluate": func(pageFunction goja.Value, gargs ...goja.Value) any {
"evaluate": func(pageFunction sobek.Value, gargs ...sobek.Value) any {
return f.Evaluate(pageFunction.String(), exportArgs(gargs)...)
},
"evaluateHandle": func(pageFunction goja.Value, gargs ...goja.Value) (mapping, error) {
"evaluateHandle": func(pageFunction sobek.Value, gargs ...sobek.Value) (mapping, error) {
jsh, err := f.EvaluateHandle(pageFunction.String(), exportArgs(gargs)...)
if err != nil {
return nil, err //nolint:wrapcheck
Expand All @@ -66,7 +66,7 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop
return mapElementHandle(vu, fe), nil
},
"getAttribute": f.GetAttribute,
"goto": func(url string, opts goja.Value) (*goja.Promise, error) {
"goto": func(url string, opts sobek.Value) (*sobek.Promise, error) {
gopts := common.NewFrameGotoOptions(
f.Referrer(),
f.NavigationTimeout(),
Expand Down Expand Up @@ -94,24 +94,24 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop
"isEnabled": f.IsEnabled,
"isHidden": f.IsHidden,
"isVisible": f.IsVisible,
"locator": func(selector string, opts goja.Value) *goja.Object {
"locator": func(selector string, opts sobek.Value) *sobek.Object {
ml := mapLocator(vu, f.Locator(selector, opts))
return rt.ToValue(ml).ToObject(rt)
},
"name": f.Name,
"page": func() *goja.Object {
"page": func() *sobek.Object {
mp := mapPage(vu, f.Page())
return rt.ToValue(mp).ToObject(rt)
},
"parentFrame": func() *goja.Object {
"parentFrame": func() *sobek.Object {
mf := mapFrame(vu, f.ParentFrame())
return rt.ToValue(mf).ToObject(rt)
},
"press": f.Press,
"selectOption": f.SelectOption,
"setContent": f.SetContent,
"setInputFiles": f.SetInputFiles,
"tap": func(selector string, opts goja.Value) (*goja.Promise, error) {
"tap": func(selector string, opts sobek.Value) (*sobek.Promise, error) {
popts := common.NewFrameTapOptions(f.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
return nil, fmt.Errorf("parsing frame tap options: %w", err)
Expand All @@ -125,7 +125,7 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop
"type": f.Type,
"uncheck": f.Uncheck,
"url": f.URL,
"waitForFunction": func(pageFunc, opts goja.Value, args ...goja.Value) (*goja.Promise, error) {
"waitForFunction": func(pageFunc, opts sobek.Value, args ...sobek.Value) (*sobek.Promise, error) {
js, popts, pargs, err := parseWaitForFunctionArgs(
vu.Context(), f.Timeout(), pageFunc, opts, args...,
)
Expand All @@ -138,7 +138,7 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop
}), nil
},
"waitForLoadState": f.WaitForLoadState,
"waitForNavigation": func(opts goja.Value) (*goja.Promise, error) {
"waitForNavigation": func(opts sobek.Value) (*sobek.Promise, error) {
popts := common.NewFrameWaitForNavigationOptions(f.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
return nil, fmt.Errorf("parsing frame wait for navigation options: %w", err)
Expand All @@ -152,7 +152,7 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop
return mapResponse(vu, resp), nil
}), nil
},
"waitForSelector": func(selector string, opts goja.Value) (mapping, error) {
"waitForSelector": func(selector string, opts sobek.Value) (mapping, error) {
eh, err := f.WaitForSelector(selector, opts)
if err != nil {
return nil, err //nolint:wrapcheck
Expand Down
10 changes: 5 additions & 5 deletions browser/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"errors"

"github.com/dop251/goja"
"github.com/grafana/sobek"

"github.com/grafana/xk6-browser/k6error"
"github.com/grafana/xk6-browser/k6ext"
Expand All @@ -18,15 +18,15 @@ func panicIfFatalError(ctx context.Context, err error) {

// exportArg exports the value and returns it.
// It returns nil if the value is undefined or null.
func exportArg(gv goja.Value) any {
func exportArg(gv sobek.Value) any {
if !gojaValueExists(gv) {
return nil
}
return gv.Export()
}

// exportArgs returns a slice of exported Goja values.
func exportArgs(gargs []goja.Value) []any {
func exportArgs(gargs []sobek.Value) []any {
args := make([]any, 0, len(gargs))
for _, garg := range gargs {
// leaves a nil garg in the array since users might want to
Expand All @@ -38,6 +38,6 @@ func exportArgs(gargs []goja.Value) []any {

// gojaValueExists returns true if a given value is not nil and exists
// (defined and not null) in the goja runtime.
func gojaValueExists(v goja.Value) bool {
return v != nil && !goja.IsUndefined(v) && !goja.IsNull(v)
func gojaValueExists(v sobek.Value) bool {
return v != nil && !sobek.IsUndefined(v) && !sobek.IsNull(v)
}
8 changes: 4 additions & 4 deletions browser/js_handle_mapping.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package browser

import (
"github.com/dop251/goja"
"github.com/grafana/sobek"

"github.com/grafana/xk6-browser/common"
)
Expand All @@ -10,19 +10,19 @@ import (
func mapJSHandle(vu moduleVU, jsh common.JSHandleAPI) mapping {
rt := vu.Runtime()
return mapping{
"asElement": func() *goja.Object {
"asElement": func() *sobek.Object {
m := mapElementHandle(vu, jsh.AsElement())
return rt.ToValue(m).ToObject(rt)
},
"dispose": jsh.Dispose,
"evaluate": func(pageFunc goja.Value, gargs ...goja.Value) any {
"evaluate": func(pageFunc sobek.Value, gargs ...sobek.Value) any {
args := make([]any, 0, len(gargs))
for _, a := range gargs {
args = append(args, exportArg(a))
}
return jsh.Evaluate(pageFunc.String(), args...)
},
"evaluateHandle": func(pageFunc goja.Value, gargs ...goja.Value) (mapping, error) {
"evaluateHandle": func(pageFunc sobek.Value, gargs ...sobek.Value) (mapping, error) {
h, err := jsh.EvaluateHandle(pageFunc.String(), exportArgs(gargs)...)
if err != nil {
return nil, err //nolint:wrapcheck
Expand Down
Loading
Loading