Skip to content

Commit

Permalink
Update to Goja fork called Sobek
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Jun 7, 2024
1 parent 021fdd4 commit 06643f3
Show file tree
Hide file tree
Showing 68 changed files with 878 additions and 901 deletions.
36 changes: 18 additions & 18 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 @@ -15,30 +15,30 @@ import (
func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolint:funlen,gocognit,cyclop
rt := vu.Runtime()
return mapping{
"addCookies": func(cookies []*common.Cookie) *goja.Promise {
"addCookies": func(cookies []*common.Cookie) *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.AddCookies(cookies) //nolint:wrapcheck
})
},
"addInitScript": func(script goja.Value) *goja.Promise {
"addInitScript": func(script sobek.Value) *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
if !gojaValueExists(script) {
if !sobekValueExists(script) {
return nil, nil
}

source := ""
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 @@ -53,27 +53,27 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
// the browser is grabbed from VU.
return mapBrowser(vu)
},
"clearCookies": func() *goja.Promise {
"clearCookies": func() *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.ClearCookies() //nolint:wrapcheck
})
},
"clearPermissions": func() *goja.Promise {
"clearPermissions": func() *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.ClearPermissions() //nolint:wrapcheck
})
},
"close": func() *goja.Promise {
"close": func() *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.Close() //nolint:wrapcheck
})
},
"cookies": func(urls ...string) *goja.Promise {
"cookies": func(urls ...string) *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return bc.Cookies(urls...) //nolint:wrapcheck
})
},
"grantPermissions": func(permissions []string, opts goja.Value) *goja.Promise {
"grantPermissions": func(permissions []string, opts sobek.Value) *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
popts := common.NewGrantPermissionsOptions()
popts.Parse(vu.Context(), opts)
Expand All @@ -83,22 +83,22 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
},
"setDefaultNavigationTimeout": bc.SetDefaultNavigationTimeout,
"setDefaultTimeout": bc.SetDefaultTimeout,
"setGeolocation": func(geolocation goja.Value) *goja.Promise {
"setGeolocation": func(geolocation sobek.Value) *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.SetGeolocation(geolocation) //nolint:wrapcheck
})
},
"setHTTPCredentials": func(httpCredentials goja.Value) *goja.Promise {
"setHTTPCredentials": func(httpCredentials sobek.Value) *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.SetHTTPCredentials(httpCredentials) //nolint:staticcheck,wrapcheck
})
},
"setOffline": func(offline bool) *goja.Promise {
"setOffline": func(offline bool) *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.SetOffline(offline) //nolint:wrapcheck
})
},
"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 @@ -120,7 +120,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 @@ -145,7 +145,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 All @@ -160,7 +160,7 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin

return rt.ToValue(mpages).ToObject(rt)
},
"newPage": func() *goja.Promise {
"newPage": func() *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
page, err := bc.NewPage()
if err != nil {
Expand Down
8 changes: 4 additions & 4 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"
"github.com/grafana/xk6-browser/k6ext"
Expand All @@ -19,7 +19,7 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop
}
return mapBrowserContext(vu, b.Context()), nil
},
"closeContext": func() *goja.Promise {
"closeContext": func() *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
b, err := vu.browser()
if err != nil {
Expand All @@ -35,7 +35,7 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop
}
return b.IsConnected(), nil
},
"newContext": func(opts goja.Value) (*goja.Promise, error) {
"newContext": func(opts sobek.Value) (*sobek.Promise, error) {
return k6ext.Promise(vu.Context(), func() (any, error) {
b, err := vu.browser()
if err != nil {
Expand Down Expand Up @@ -69,7 +69,7 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop
}
return b.Version(), nil
},
"newPage": func(opts goja.Value) *goja.Promise {
"newPage": func(opts sobek.Value) *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
b, err := vu.browser()
if err != nil {
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
Loading

0 comments on commit 06643f3

Please sign in to comment.