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

Commit a9b05be

Browse files
author
Ivan Mirić
committed
Export Chromium BrowserType implementation directly
Closes #485
1 parent c6039c1 commit a9b05be

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

main.go

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"github.com/grafana/xk6-browser/k6ext"
1313

1414
k6modules "go.k6.io/k6/js/modules"
15-
16-
"github.com/dop251/goja"
1715
)
1816

1917
const version = "v0.4.0"
@@ -23,12 +21,11 @@ type (
2321
// instances for each VU.
2422
RootModule struct{}
2523

26-
// JSModule is the entrypoint into the browser JS module.
24+
// JSModule exposes the properties available to the JS script.
2725
JSModule struct {
28-
vu k6modules.VU
29-
k6Metrics *k6ext.CustomMetrics
30-
Devices map[string]common.Device
31-
Version string
26+
Chromium api.BrowserType
27+
Devices map[string]common.Device
28+
Version string
3229
}
3330

3431
// ModuleInstance represents an instance of the JS module.
@@ -61,34 +58,22 @@ func New() *RootModule {
6158
// a new instance for each VU.
6259
func (*RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
6360
k6m := k6ext.RegisterCustomMetrics(vu.InitEnv().Registry)
61+
ctx := k6ext.WithVU(vu.Context(), vu)
62+
ctx = k6ext.WithCustomMetrics(ctx, k6m)
63+
6464
return &ModuleInstance{
6565
mod: &JSModule{
66-
vu: vu,
67-
k6Metrics: k6m,
68-
Devices: common.GetDevices(),
69-
Version: version,
66+
Chromium: chromium.NewBrowserType(ctx),
67+
Devices: common.GetDevices(),
68+
Version: version,
7069
},
7170
}
7271
}
7372

7473
// Exports returns the exports of the JS module so that it can be used in test
7574
// scripts.
7675
func (mi *ModuleInstance) Exports() k6modules.Exports {
77-
return k6modules.Exports{
78-
Named: map[string]interface{}{
79-
"chromium": mi.mod,
80-
"devices": mi.mod.Devices,
81-
},
82-
}
83-
}
84-
85-
// Launch Chromium with given options.
86-
func (m *JSModule) Launch(opts goja.Value) api.Browser {
87-
ctx := k6ext.WithVU(m.vu.Context(), m.vu)
88-
ctx = k6ext.WithCustomMetrics(ctx, m.k6Metrics)
89-
90-
bt := chromium.NewBrowserType(ctx)
91-
return bt.Launch(opts)
76+
return k6modules.Exports{Default: mi.mod}
9277
}
9378

9479
func init() {

0 commit comments

Comments
 (0)