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

Commit 1016629

Browse files
committed
Add importable browser module
Moves the module registration logic to a new pkg called browser.
1 parent fbcd5a4 commit 1016629

File tree

2 files changed

+64
-54
lines changed

2 files changed

+64
-54
lines changed

browser/module.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Package browser provides an entry point to the browser extension.
2+
package browser
3+
4+
import (
5+
"github.com/grafana/xk6-browser/api"
6+
"github.com/grafana/xk6-browser/chromium"
7+
"github.com/grafana/xk6-browser/common"
8+
9+
k6modules "go.k6.io/k6/js/modules"
10+
)
11+
12+
const version = "0.6.0"
13+
14+
type (
15+
// RootModule is the global module instance that will create module
16+
// instances for each VU.
17+
RootModule struct{}
18+
19+
// JSModule exposes the properties available to the JS script.
20+
JSModule struct {
21+
Chromium api.BrowserType
22+
Devices map[string]common.Device
23+
Version string
24+
}
25+
26+
// ModuleInstance represents an instance of the JS module.
27+
ModuleInstance struct {
28+
mod *JSModule
29+
}
30+
)
31+
32+
var (
33+
_ k6modules.Module = &RootModule{}
34+
_ k6modules.Instance = &ModuleInstance{}
35+
)
36+
37+
// New returns a pointer to a new RootModule instance.
38+
func New() *RootModule {
39+
return &RootModule{}
40+
}
41+
42+
// NewModuleInstance implements the k6modules.Module interface to return
43+
// a new instance for each VU.
44+
func (*RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
45+
return &ModuleInstance{
46+
mod: &JSModule{
47+
Chromium: chromium.NewBrowserType(vu),
48+
Devices: common.GetDevices(),
49+
Version: version,
50+
},
51+
}
52+
}
53+
54+
// Exports returns the exports of the JS module so that it can be used in test
55+
// scripts.
56+
func (mi *ModuleInstance) Exports() k6modules.Exports {
57+
return k6modules.Exports{Default: mi.mod}
58+
}

main.go

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,28 @@
1+
// Package browser provides an entry point to the browser extension.
12
package browser
23

34
import (
45
"log"
56
"net/http"
6-
_ "net/http/pprof" // nolint:gosec
7+
_ "net/http/pprof" //nolint:gosec
78
"os"
89

9-
"github.com/grafana/xk6-browser/api"
10-
"github.com/grafana/xk6-browser/chromium"
11-
"github.com/grafana/xk6-browser/common"
10+
"github.com/grafana/xk6-browser/browser"
1211

1312
k6modules "go.k6.io/k6/js/modules"
1413
)
1514

16-
const version = "0.6.0"
17-
18-
type (
19-
// RootModule is the global module instance that will create module
20-
// instances for each VU.
21-
RootModule struct{}
22-
23-
// JSModule exposes the properties available to the JS script.
24-
JSModule struct {
25-
Chromium api.BrowserType
26-
Devices map[string]common.Device
27-
Version string
28-
}
29-
30-
// ModuleInstance represents an instance of the JS module.
31-
ModuleInstance struct {
32-
mod *JSModule
33-
}
34-
)
35-
36-
var (
37-
_ k6modules.Module = &RootModule{}
38-
_ k6modules.Instance = &ModuleInstance{}
39-
)
40-
4115
func init() {
4216
if _, ok := os.LookupEnv("K6_BROWSER_PPROF"); ok {
4317
go func() {
4418
address := "localhost:6060"
4519
log.Println("Starting http debug server", address)
46-
log.Println(http.ListenAndServe(address, nil))
20+
log.Println(http.ListenAndServe(address, nil)) //nolint:gosec
21+
// no linted because we don't need to set timeouts for the debug server.
4722
}()
4823
}
4924
}
5025

51-
// New returns a pointer to a new RootModule instance.
52-
func New() *RootModule {
53-
return &RootModule{}
54-
}
55-
56-
// NewModuleInstance implements the k6modules.Module interface to return
57-
// a new instance for each VU.
58-
func (*RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
59-
return &ModuleInstance{
60-
mod: &JSModule{
61-
Chromium: chromium.NewBrowserType(vu),
62-
Devices: common.GetDevices(),
63-
Version: version,
64-
},
65-
}
66-
}
67-
68-
// Exports returns the exports of the JS module so that it can be used in test
69-
// scripts.
70-
func (mi *ModuleInstance) Exports() k6modules.Exports {
71-
return k6modules.Exports{Default: mi.mod}
72-
}
73-
7426
func init() {
75-
k6modules.Register("k6/x/browser", New())
27+
k6modules.Register("k6/x/browser", browser.New())
7628
}

0 commit comments

Comments
 (0)