Skip to content
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
7 changes: 4 additions & 3 deletions internal/js/modules/k6/browser/common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,9 @@ func (b *Browser) onDetachedFromTarget(ev *target.EventDetachedFromTarget) {
}

func (b *Browser) newPageInContext(id cdp.BrowserContextID) (*Page, error) {
if b.context == nil || b.context.id != id {
return nil, fmt.Errorf("missing browser context %s, current context is %s", id, b.context.id)
bc := b.getDefaultBrowserContextOrMatchedID(id)
if bc.id != id {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure the default browser context can't be nil? Should we handle this case to avoid future panics?

Copy link
Contributor Author

@inancgumus inancgumus Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default one is always non-nil, as we always set it when connecting to Chromium. It's the always-available browser context.

return nil, fmt.Errorf("missing browser context %s, current context is %s", id, bc.id)
}

ctx, cancel := context.WithTimeout(b.vuCtx, b.browserOpts.Timeout)
Expand All @@ -466,7 +467,7 @@ func (b *Browser) newPageInContext(id cdp.BrowserContextID) (*Page, error) {

waitForPage, removeEventHandler := createWaitForEventHandler(
ctx,
b.context, // browser context will emit the following event:
bc, // browser context will emit the following event:
[]string{EventBrowserContextPage},
func(e any) bool {
tid := <-targetID
Expand Down
58 changes: 33 additions & 25 deletions internal/js/modules/k6/browser/common/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,33 @@ import (
func TestBrowserNewPageInContext(t *testing.T) {
t.Parallel()

const (
// default IDs to be used in tests.
browserContextID cdp.BrowserContextID = "42"
targetID target.ID = "84"
)

type testCase struct {
b *Browser
bc *BrowserContext
}

newTestCase := func(id cdp.BrowserContextID) *testCase {
ctx, cancel := context.WithCancel(context.Background())
logger := log.NewNullLogger()
b := newBrowser(context.Background(), ctx, cancel, nil, NewLocalBrowserOptions(), logger)
b := newBrowser(context.Background(), ctx, cancel, nil, NewLocalBrowserOptions(), log.NewNullLogger())

// set a new browser context in the browser with `id`, so that newPageInContext can find it.
var err error
vu := k6test.NewVU(t)
ctx = k6ext.WithVU(ctx, vu)
b.context, err = NewBrowserContext(ctx, b, id, nil, nil)
b.context, err = NewBrowserContext(k6ext.WithVU(ctx, k6test.NewVU(t)), b, id, nil, nil)
b.defaultContext = b.context // always happens when a new browser is connected
require.NoError(t, err)
return &testCase{
b: b,
bc: b.context,
}
}

const (
// default IDs to be used in tests.
browserContextID cdp.BrowserContextID = "42"
targetID target.ID = "84"
)

t.Run("happy_path", func(t *testing.T) {
t.Parallel()

// newPageInContext will look for this browser context.
tc := newTestCase(browserContextID)
tc := &testCase{b: b, bc: b.context}

// newPageInContext will return this page by searching it by its targetID in the wait event handler.
tc.b.pages[targetID] = &Page{targetID: targetID}

tc.b.conn = fakeConn{
execute: func(
ctx context.Context, method string, params, res any,
) error {
execute: func(ctx context.Context, method string, params, res any) error {
require.Equal(t, target.CommandCreateTarget, method)
require.IsType(t, params, &target.CreateTargetParams{})
tp, _ := params.(*target.CreateTargetParams)
Expand All @@ -79,6 +67,14 @@ func TestBrowserNewPageInContext(t *testing.T) {
return nil
},
}
return tc
}

t.Run("happy_path", func(t *testing.T) {
t.Parallel()

// newPageInContext will look for this browser context.
tc := newTestCase(browserContextID)

page, err := tc.b.newPageInContext(browserContextID)
require.NoError(t, err)
Expand All @@ -101,6 +97,18 @@ func TestBrowserNewPageInContext(t *testing.T) {
"should have returned the missing browser context ID in the error message")
})

t.Run("uses_default_browser_context", func(t *testing.T) {
t.Parallel()

tc := newTestCase(browserContextID)
tc.b.context = nil // should use default context if there is no current context

require.NotPanics(t, func() {
_, err := tc.b.newPageInContext(browserContextID)
require.NoError(t, err)
})
})

// should return the error returned from the executor.
t.Run("error_in_create_target_action", func(t *testing.T) {
t.Parallel()
Expand Down
Loading