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 Sobek out of browserContext.addinitscript #1228

Merged
merged 4 commits into from
Mar 4, 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
31 changes: 29 additions & 2 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package browser
import (
"context"
"fmt"
"reflect"
"time"

"github.com/dop251/goja"
Expand Down Expand Up @@ -811,8 +812,34 @@ func mapWorker(vu moduleVU, w *common.Worker) mapping {
func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolint:funlen
rt := vu.Runtime()
return mapping{
"addCookies": bc.AddCookies,
"addInitScript": bc.AddInitScript,
"addCookies": bc.AddCookies,
"addInitScript": func(script goja.Value) error {
if !gojaValueExists(script) {
return nil
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
}

source := ""
switch script.ExportType() {
case reflect.TypeOf(string("")):
source = script.String()
case reflect.TypeOf(goja.Object{}):
opts := script.ToObject(rt)
for _, k := range opts.Keys() {
if k == "content" {
source = opts.Get(k).String()
}
}
default:
_, isCallable := goja.AssertFunction(script)
if !isCallable {
source = fmt.Sprintf("(%s);", script.ToString().String())
} else {
source = fmt.Sprintf("(%s)(...args);", script.ToString().String())
}
}

return bc.AddInitScript(source) //nolint:wrapcheck
},
"browser": bc.Browser,
"clearCookies": bc.ClearCookies,
"clearPermissions": bc.ClearPermissions,
Expand Down
43 changes: 6 additions & 37 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/url"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -109,57 +108,27 @@ func NewBrowserContext(
}
}

rt := b.vu.Runtime()
k6Obj := rt.ToValue(js.K6ObjectScript)
wv := rt.ToValue(js.WebVitalIIFEScript)
wvi := rt.ToValue(js.WebVitalInitScript)

if err := b.AddInitScript(k6Obj, nil); err != nil {
if err := b.AddInitScript(js.K6ObjectScript); err != nil {
return nil, fmt.Errorf("adding k6 object to new browser context: %w", err)
}
if err := b.AddInitScript(wv, nil); err != nil {
if err := b.AddInitScript(js.WebVitalIIFEScript); err != nil {
return nil, fmt.Errorf("adding web vital script to new browser context: %w", err)
}
if err := b.AddInitScript(wvi, nil); err != nil {
if err := b.AddInitScript(js.WebVitalInitScript); err != nil {
return nil, fmt.Errorf("adding web vital init script to new browser context: %w", err)
}

return &b, nil
}

// AddInitScript adds a script that will be initialized on all new pages.
func (b *BrowserContext) AddInitScript(script goja.Value, arg goja.Value) error {
func (b *BrowserContext) AddInitScript(script string) error {
b.logger.Debugf("BrowserContext:AddInitScript", "bctxid:%v", b.id)

rt := b.vu.Runtime()

source := ""
if gojaValueExists(script) {
switch script.ExportType() {
case reflect.TypeOf(string("")):
source = script.String()
case reflect.TypeOf(goja.Object{}):
opts := script.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "content":
source = opts.Get(k).String()
}
}
default:
_, isCallable := goja.AssertFunction(script)
if !isCallable {
source = fmt.Sprintf("(%s);", script.ToString().String())
} else {
source = fmt.Sprintf("(%s)(...args);", script.ToString().String())
}
}
}

b.evaluateOnNewDocumentSources = append(b.evaluateOnNewDocumentSources, source)
b.evaluateOnNewDocumentSources = append(b.evaluateOnNewDocumentSources, script)

for _, p := range b.browser.getPages() {
if err := p.evaluateOnNewDocument(source); err != nil {
if err := p.evaluateOnNewDocument(script); err != nil {
return fmt.Errorf("adding init script to browser context: %w", err)
}
}
Expand Down
Loading