From c584676762aec003ceb319faf1bc0dc1ffb45b78 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Fri, 3 May 2024 15:53:31 +0100 Subject: [PATCH] Fix NPD when using goto on a second page This fixes an issue when working with a second tab (i.e. opening a new page in the same existing browserContext), and navigating to a url. In fact this issue would arise as soon as any API is used from the new page instance. This was occurring due to the unmapped browserContext when getting it from the browser. --- browser/browser_mapping.go | 4 ++-- tests/browser_context_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/browser/browser_mapping.go b/browser/browser_mapping.go index d7e63a629..f2cfc7640 100644 --- a/browser/browser_mapping.go +++ b/browser/browser_mapping.go @@ -12,12 +12,12 @@ import ( func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop rt := vu.Runtime() return mapping{ - "context": func() (*common.BrowserContext, error) { + "context": func() (mapping, error) { b, err := vu.browser() if err != nil { return nil, err } - return b.Context(), nil + return mapBrowserContext(vu, b.Context()), nil }, "closeContext": func() error { b, err := vu.browser() diff --git a/tests/browser_context_test.go b/tests/browser_context_test.go index 238b1cd26..bd60b9396 100644 --- a/tests/browser_context_test.go +++ b/tests/browser_context_test.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "net/http" + "net/http/httptest" + "os" "testing" "time" @@ -677,6 +679,39 @@ func TestK6Object(t *testing.T) { } } +// This test ensures that when opening a new tab, this it is possible to navigate +// to the url. If the mapping layer is not setup correctly we can end up with a +// NPD. +func TestNewTab(t *testing.T) { + t.Parallel() + + // Start a server that will return static html files. + mux := http.NewServeMux() + s := httptest.NewServer(mux) + t.Cleanup(s.Close) + + const ( + slash = string(os.PathSeparator) + path = slash + testBrowserStaticDir + slash + ) + fs := http.FileServer(http.Dir(testBrowserStaticDir)) + mux.Handle(path, http.StripPrefix(path, fs)) + + // Start the iteration + _, rt, _, cleanUp := startIteration(t, env.ConstLookup(env.K6TestRunID, "12345")) + defer cleanUp() + + // Run the test script + _, err := rt.RunString(fmt.Sprintf(` + const p = browser.newPage() + p.goto("%s/%s/ping.html") + + const p2 = browser.context().newPage() + p2.goto("%s/%s/ping.html") + `, s.URL, testBrowserStaticDir, s.URL, testBrowserStaticDir)) + require.NoError(t, err) +} + func TestBrowserContextTimeout(t *testing.T) { t.Parallel()