This repository has been archived by the owner on Jan 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmodule.go
76 lines (61 loc) · 1.81 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Package browser provides an entry point to the browser extension.
package browser
import (
"errors"
"os"
"github.com/dop251/goja"
"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6ext"
k6common "go.k6.io/k6/js/common"
k6modules "go.k6.io/k6/js/modules"
)
const version = "0.7.0"
type (
// RootModule is the global module instance that will create module
// instances for each VU.
RootModule struct{}
// JSModule exposes the properties available to the JS script.
JSModule struct {
Chromium *goja.Object
Devices map[string]common.Device
Version string
}
// ModuleInstance represents an instance of the JS module.
ModuleInstance struct {
mod *JSModule
}
)
var (
_ k6modules.Module = &RootModule{}
_ k6modules.Instance = &ModuleInstance{}
)
// New returns a pointer to a new RootModule instance.
func New() *RootModule {
return &RootModule{}
}
// NewModuleInstance implements the k6modules.Module interface to return
// a new instance for each VU.
func (*RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
if _, ok := os.LookupEnv("K6_BROWSER_DISABLE_RUN"); ok {
msg := "Disable run flag enabled, browser test run aborted. Please contact support."
if m, ok := os.LookupEnv("K6_BROWSER_DISABLE_RUN_MSG"); ok {
msg = m
}
k6common.Throw(vu.Runtime(), errors.New(msg))
}
// promises and inner objects need the VU object to be
// able to use k6-core specific functionality.
ctx := k6ext.WithVU(vu.Context(), vu)
return &ModuleInstance{
mod: &JSModule{
Chromium: mapBrowserToGoja(ctx, vu),
Devices: common.GetDevices(),
Version: version,
},
}
}
// Exports returns the exports of the JS module so that it can be used in test
// scripts.
func (mi *ModuleInstance) Exports() k6modules.Exports {
return k6modules.Exports{Default: mi.mod}
}