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

Add core to inject k6 object into window #1007

Merged
merged 6 commits into from
Aug 31, 2023
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
4 changes: 4 additions & 0 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ 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 {
return nil, fmt.Errorf("adding k6 object to new browser context: %w", err)
}
if err := b.AddInitScript(wv, nil); err != nil {
return nil, fmt.Errorf("adding web vital script to new browser context: %w", err)
}
Expand Down
6 changes: 5 additions & 1 deletion common/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@ func TestNewBrowserContext(t *testing.T) {

webVitalIIFEScriptFound := false
webVitalInitScriptFound := false
k6ObjScriptFound := false
for _, script := range bc.evaluateOnNewDocumentSources {
switch script {
case js.WebVitalIIFEScript:
webVitalIIFEScriptFound = true
case js.WebVitalInitScript:
webVitalInitScriptFound = true
case js.K6ObjectScript:
k6ObjScriptFound = true
default:
assert.Fail(t, "script is neither WebVitalIIFEScript nor WebVitalInitScript")
assert.Fail(t, "script is neither WebVitalIIFEScript, WebVitalInitScript, nor k6ObjScript")
}
}

assert.True(t, webVitalIIFEScriptFound, "WebVitalIIFEScript was not initialized in the context")
assert.True(t, webVitalInitScriptFound, "WebVitalInitScript was not initialized in the context")
assert.True(t, k6ObjScriptFound, "k6ObjScript was not initialized in the context")
})
}
7 changes: 7 additions & 0 deletions common/js/web_vital.go → common/js/embedded_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ var WebVitalIIFEScript string
//
//go:embed web_vital_init.js
var WebVitalInitScript string

// K6ObjectScript is used to propagate
// information to other libraries about
// the current user session.
//
//go:embed k6_object.js
var K6ObjectScript string
1 change: 1 addition & 0 deletions common/js/k6_object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.k6 = {};
15 changes: 15 additions & 0 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,18 @@ func TestBrowserContextCookies(t *testing.T) {
})
}
}

func TestK6Object(t *testing.T) {
b := newTestBrowser(t, withFileServer())
p := b.NewPage(nil)

url := b.staticURL("empty.html")
r, err := p.Goto(url, nil)
require.NoError(t, err)
require.NotNil(t, r)

k6Obj := p.Evaluate(b.toGojaValue(`() => window.k6`))
k6ObjGoja := b.toGojaValue(k6Obj)

assert.False(t, k6ObjGoja.Equals(goja.Null()))
}
Loading