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 k6 object and add testRunID #1235

Merged
merged 4 commits into from
Mar 7, 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
24 changes: 24 additions & 0 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,11 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen
if err != nil {
return nil, err //nolint:wrapcheck
}

if err := initBrowserContext(bctx, vu.testRunID); err != nil {
return nil, err
}

m := mapBrowserContext(vu, bctx)
return rt.ToValue(m).ToObject(rt), nil
},
Expand All @@ -1033,7 +1038,26 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen
if err != nil {
return nil, err //nolint:wrapcheck
}

if err := initBrowserContext(b.Context(), vu.testRunID); err != nil {
return nil, err
}

return mapPage(vu, page), nil
},
}
}

func initBrowserContext(bctx *common.BrowserContext, testRunID string) error {
// Setting a k6 object which will contain k6 specific metadata
// on the current test run. This allows external applications
// (such as Grafana Faro) to identify that the session is a k6
// automated one and not one driven by a real person.
if err := bctx.AddInitScript(
fmt.Sprintf(`window.k6 = { testRunId: %q }`, testRunID),
); err != nil {
return fmt.Errorf("adding k6 object to new browser context: %w", err)
}

return nil
}
3 changes: 0 additions & 3 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ func NewBrowserContext(
}
}

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(js.WebVitalIIFEScript); err != nil {
return nil, fmt.Errorf("adding web vital script to new browser context: %w", err)
}
Expand Down
6 changes: 1 addition & 5 deletions common/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,19 @@ 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, WebVitalInitScript, nor k6ObjScript")
assert.Fail(t, "script is neither WebVitalIIFEScript, nor WebVitalInitScript")
}
}

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")
})
}

Expand Down
7 changes: 0 additions & 7 deletions common/js/embedded_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,3 @@ 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: 0 additions & 1 deletion common/js/k6_object.js

This file was deleted.

60 changes: 46 additions & 14 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"

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

func TestBrowserContextAddCookies(t *testing.T) {
Expand Down Expand Up @@ -627,22 +628,53 @@ func TestBrowserContextClearCookies(t *testing.T) {
func TestK6Object(t *testing.T) {
t.Parallel()

b := newTestBrowser(t, withFileServer())
p := b.NewPage(nil)

url := b.staticURL("empty.html")
opts := &common.FrameGotoOptions{
Timeout: common.DefaultTimeout,
tests := []struct {
name string
testRunID string
want string
}{
{
name: "empty_testRunId",
want: `{"testRunId":""}`,
},
{
name: "with_testRunId",
testRunID: "123456",
want: `{"testRunId":"123456"}`,
},
}
r, err := p.Goto(
url,
opts,
)
require.NoError(t, err)
require.NotNil(t, r)

k6Obj := p.Evaluate(`() => window.k6`)
assert.NotNil(t, k6Obj)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

_, rt, _, cleanUp := startIteration(t, env.ConstLookup(env.K6TestRunID, tt.testRunID))
defer cleanUp()

// First test with browser.newPage
got, err := rt.RunString(`
const p = browser.newPage()
p.goto("about:blank")
const o = p.evaluate(() => window.k6)
JSON.stringify(o)
`)
require.NoError(t, err)
assert.Equal(t, tt.want, got.String())

// Now test with browser.newContext
got, err = rt.RunString(`
browser.closeContext()
const c = browser.newContext()
const p2 = c.newPage()
p2.goto("about:blank")
const o2 = p2.evaluate(() => window.k6)
JSON.stringify(o2)
`)
require.NoError(t, err)
assert.Equal(t, tt.want, got.String())
})
}
}

func TestBrowserContextTimeout(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,10 +822,11 @@ func TestPageWaitForFunction(t *testing.T) {
// (more importantly) set browser as the mapped browser instance which will
// force all tests that work with this to go through the mapping layer.
// This returns a cleanup function which should be deferred.
func startIteration(t *testing.T) (*k6test.VU, *goja.Runtime, *[]string, func()) {
// The opts are passed to k6test.NewVU as is without any modification.
func startIteration(t *testing.T, opts ...any) (*k6test.VU, *goja.Runtime, *[]string, func()) {
t.Helper()

vu := k6test.NewVU(t)
vu := k6test.NewVU(t, opts...)
rt := vu.Runtime()

mod := browser.New().NewModuleInstance(vu)
Expand Down
Loading