Skip to content

Commit

Permalink
Add BrowserType.Connect test
Browse files Browse the repository at this point in the history
Also adds a new method in common.Browser API so we can get the
underlying browser process WS URL. This is required in order to reuse
the current TestBrowser implementation to launch a new browser and
connecto to it through BrowserType.Connect.
  • Loading branch information
ka3de committed Feb 28, 2023
1 parent 09a53b1 commit 7ee3d92
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,8 @@ func (b *Browser) Version() string {
}
return product[i+1:]
}

// WsURL returns the Websocket URL that the browser is listening on for CDP clients.
func (b *Browser) WsURL() string {
return b.browserProc.WsURL()
}
20 changes: 20 additions & 0 deletions tests/browser_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tests

import (
"testing"

"github.com/grafana/xk6-browser/chromium"
"github.com/grafana/xk6-browser/k6ext/k6test"
)

func TestBrowserTypeConnect(t *testing.T) {
// Start a test browser so we can get its WS URL
// and use it to connect through BrowserType.Connect.
tb := newTestBrowser(t)
vu := k6test.NewVU(t)
bt := chromium.NewBrowserType(vu)
vu.MoveToVUContext()

b := bt.Connect(tb.WsURL(), nil)
b.NewPage(nil)
}
17 changes: 16 additions & 1 deletion tests/test_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/chromium"
"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6ext"
"github.com/grafana/xk6-browser/k6ext/k6test"

Expand All @@ -32,7 +33,8 @@ type testBrowser struct {
vu *k6test.VU
logCache *logCache

pid int // the browser process ID
pid int // the browser process ID
wsURL string

api.Browser
}
Expand Down Expand Up @@ -110,7 +112,12 @@ func newTestBrowser(tb testing.TB, opts ...any) *testBrowser {
state.TLSConfig = testServer.TLSClientConfig
state.Transport = testServer.HTTPTransport
}

b, pid := bt.Launch(rt.ToValue(launchOpts))
cb, ok := b.(*common.Browser)
if !ok {
panic(fmt.Errorf("testBrowser: unexpected browser %T", b))
}

tb.Cleanup(func() {
select {
Expand All @@ -130,6 +137,7 @@ func newTestBrowser(tb testing.TB, opts ...any) *testBrowser {
logCache: lc,
Browser: b,
pid: pid,
wsURL: cb.WsURL(),
}
if enableFileServer {
tbr = tbr.withFileServer()
Expand Down Expand Up @@ -187,6 +195,13 @@ func (b *testBrowser) staticURL(path string) string {
return b.URL("/" + testBrowserStaticDir + "/" + path)
}

// WsURL returns the Websocket URL that the test browser is listening on for CDP clients.
func (b *testBrowser) WsURL() string {
b.t.Helper()

return b.wsURL
}

// attachFrame attaches the frame to the page and returns it.
func (b *testBrowser) attachFrame(page api.Page, frameID string, url string) api.Frame {
b.t.Helper()
Expand Down

0 comments on commit 7ee3d92

Please sign in to comment.