Skip to content

Commit

Permalink
Add testRunId to the k6 object
Browse files Browse the repository at this point in the history
We can now add the testRunId to the k6 object which is injected into
the page, allowing external apps to work with the k6 object, while also
allowing them to link back to the test run id.
  • Loading branch information
ankur22 committed Mar 5, 2024
1 parent 92b0d0d commit 6652248
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
10 changes: 6 additions & 4 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen
return nil, err //nolint:wrapcheck
}

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

Expand Down Expand Up @@ -1039,7 +1039,7 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen
return nil, err //nolint:wrapcheck
}

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

Expand All @@ -1048,12 +1048,14 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen
}
}

func initBrowserContext(bctx *common.BrowserContext) error {
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("window.k6 = {}"); err != nil {
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)
}

Expand Down
57 changes: 47 additions & 10 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,17 +628,53 @@ func TestBrowserContextClearCookies(t *testing.T) {
func TestK6Object(t *testing.T) {
t.Parallel()

_, rt, _, cleanUp := startIteration(t)
defer cleanUp()
tests := []struct {
name string
testRunID string
want string
}{
{
name: "empty_testRunId",
want: `{"testRunId":""}`,
},
{
name: "with_testRunId",
testRunID: "123456",
want: `{"testRunId":"123456"}`,
},
}

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, "{}", got.String())
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 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

0 comments on commit 6652248

Please sign in to comment.