From 6652248acaf78335086a136d40ce26b02f4c4916 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Tue, 5 Mar 2024 12:46:12 +0000 Subject: [PATCH] Add testRunId to the k6 object 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. --- browser/mapping.go | 10 +++--- tests/browser_context_test.go | 57 +++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/browser/mapping.go b/browser/mapping.go index d344f8398..b8b8b1944 100644 --- a/browser/mapping.go +++ b/browser/mapping.go @@ -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 } @@ -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 } @@ -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) } diff --git a/tests/browser_context_test.go b/tests/browser_context_test.go index 39399bd77..808a10a65 100644 --- a/tests/browser_context_test.go +++ b/tests/browser_context_test.go @@ -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) { @@ -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) {