Skip to content

Commit

Permalink
Fix NPD when using goto on a second page
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ankur22 committed May 7, 2024
1 parent f99418b commit 991c699
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions browser/browser_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
35 changes: 35 additions & 0 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 991c699

Please sign in to comment.