Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 65d53fe

Browse files
authored
Merge pull request #667 from grafana/add/662-importable-browser
xk6-browser should be importable as a k6 module
2 parents fbcd5a4 + 4a01306 commit 65d53fe

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

main.go renamed to browser/module.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1+
// Package browser provides an entry point to the browser extension.
12
package browser
23

34
import (
4-
"log"
5-
"net/http"
6-
_ "net/http/pprof" // nolint:gosec
7-
"os"
8-
95
"github.com/grafana/xk6-browser/api"
106
"github.com/grafana/xk6-browser/chromium"
117
"github.com/grafana/xk6-browser/common"
@@ -38,16 +34,6 @@ var (
3834
_ k6modules.Instance = &ModuleInstance{}
3935
)
4036

41-
func init() {
42-
if _, ok := os.LookupEnv("K6_BROWSER_PPROF"); ok {
43-
go func() {
44-
address := "localhost:6060"
45-
log.Println("Starting http debug server", address)
46-
log.Println(http.ListenAndServe(address, nil))
47-
}()
48-
}
49-
}
50-
5137
// New returns a pointer to a new RootModule instance.
5238
func New() *RootModule {
5339
return &RootModule{}
@@ -70,7 +56,3 @@ func (*RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
7056
func (mi *ModuleInstance) Exports() k6modules.Exports {
7157
return k6modules.Exports{Default: mi.mod}
7258
}
73-
74-
func init() {
75-
k6modules.Register("k6/x/browser", New())
76-
}

browser/module_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package browser
2+
3+
import (
4+
"testing"
5+
6+
"github.com/dop251/goja"
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/grafana/xk6-browser/chromium"
10+
11+
k6common "go.k6.io/k6/js/common"
12+
k6modulestest "go.k6.io/k6/js/modulestest"
13+
k6metrics "go.k6.io/k6/metrics"
14+
)
15+
16+
// TestModuleNew tests registering the module.
17+
// It doesn't test the module's remaining functionality as it is
18+
// already tested in the tests/ integration tests.
19+
func TestModuleNew(t *testing.T) {
20+
t.Parallel()
21+
22+
vu := &k6modulestest.VU{
23+
RuntimeField: goja.New(),
24+
InitEnvField: &k6common.InitEnvironment{
25+
Registry: k6metrics.NewRegistry(),
26+
},
27+
}
28+
m, ok := New().NewModuleInstance(vu).(*ModuleInstance)
29+
require.True(t, ok, "NewModuleInstance should return a ModuleInstance")
30+
require.NotNil(t, m.mod, "Module should be set")
31+
require.IsType(t, m.mod.Chromium, &chromium.BrowserType{})
32+
require.NotNil(t, m.mod.Devices, "Devices should be set")
33+
require.Equal(t, m.mod.Version, version, "Incorrect version")
34+
}

register.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Package browser provides an entry point to the browser extension.
2+
package browser
3+
4+
import (
5+
"log"
6+
"net/http"
7+
_ "net/http/pprof" //nolint:gosec
8+
"os"
9+
10+
"github.com/grafana/xk6-browser/browser"
11+
12+
k6modules "go.k6.io/k6/js/modules"
13+
)
14+
15+
func init() {
16+
if _, ok := os.LookupEnv("K6_BROWSER_PPROF"); ok {
17+
go func() {
18+
address := "localhost:6060"
19+
log.Println("Starting http debug server", address)
20+
log.Println(http.ListenAndServe(address, nil)) //nolint:gosec
21+
// no linted because we don't need to set timeouts for the debug server.
22+
}()
23+
}
24+
}
25+
26+
func init() {
27+
k6modules.Register("k6/x/browser", browser.New())
28+
}

0 commit comments

Comments
 (0)