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

Commit 54b570e

Browse files
authored
Merge pull request #649 from grafana/add/641-naked-browser-launch-opts
Add/641 naked browser launch opts
2 parents 79b7f08 + dac6e6e commit 54b570e

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

chromium/browser_type.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ func (b *BrowserType) Launch(opts goja.Value) api.Browser {
131131
if b.logger, err = makeLogger(ctx); err != nil {
132132
k6ext.Panic(ctx, "setting up logger: %w", err)
133133
}
134-
135134
launchOpts := common.NewLaunchOptions(k6ext.OnCloud())
136-
if err := launchOpts.Parse(ctx, opts, b.logger); err != nil {
135+
if err := launchOpts.Parse(ctx, b.logger, opts); err != nil {
137136
k6ext.Panic(ctx, "parsing launch options: %w", err)
138137
}
139138
ctx = common.WithLaunchOptions(ctx, launchOpts)

common/browser_options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package common
22

33
import (
44
"context"
5-
"errors"
65
"time"
76

87
"github.com/dop251/goja"
@@ -55,9 +54,10 @@ func NewLaunchOptions(onCloud bool) *LaunchOptions {
5554
}
5655

5756
// Parse parses launch options from a JS object.
58-
func (l *LaunchOptions) Parse(ctx context.Context, opts goja.Value, logger *log.Logger) error { //nolint:cyclop
57+
func (l *LaunchOptions) Parse(ctx context.Context, logger *log.Logger, opts goja.Value) error { //nolint:cyclop
58+
// when opts is nil, we just return the default options without error.
5959
if !gojaValueExists(opts) {
60-
return errors.New("LaunchOptions does not exist in the runtime")
60+
return nil
6161
}
6262
var (
6363
rt = k6ext.Runtime(ctx)

common/browser_options_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import (
1414
func TestBrowserLaunchOptionsParse(t *testing.T) {
1515
t.Parallel()
1616

17+
defaultOptions := &LaunchOptions{
18+
Env: make(map[string]string),
19+
Headless: true,
20+
LogCategoryFilter: ".*",
21+
Timeout: DefaultTimeout,
22+
}
23+
1724
for name, tt := range map[string]struct {
1825
opts map[string]any
1926
assert func(testing.TB, *LaunchOptions)
@@ -24,12 +31,14 @@ func TestBrowserLaunchOptionsParse(t *testing.T) {
2431
opts: map[string]any{},
2532
assert: func(tb testing.TB, lo *LaunchOptions) {
2633
tb.Helper()
27-
assert.Equal(t, &LaunchOptions{
28-
Env: make(map[string]string),
29-
Headless: true,
30-
LogCategoryFilter: ".*",
31-
Timeout: DefaultTimeout,
32-
}, lo)
34+
assert.Equal(t, defaultOptions, lo)
35+
},
36+
},
37+
"defaults_nil": { // providing nil option returns default options
38+
opts: nil,
39+
assert: func(tb testing.TB, lo *LaunchOptions) {
40+
tb.Helper()
41+
assert.Equal(t, defaultOptions, lo)
3342
},
3443
},
3544
"defaults_on_cloud": {
@@ -253,7 +262,7 @@ func TestBrowserLaunchOptionsParse(t *testing.T) {
253262
vu = k6test.NewVU(t)
254263
lo = NewLaunchOptions(tt.onCloud)
255264
)
256-
err := lo.Parse(vu.Context(), vu.ToGojaValue(tt.opts), log.NewNullLogger())
265+
err := lo.Parse(vu.Context(), log.NewNullLogger(), vu.ToGojaValue(tt.opts))
257266
if tt.err != "" {
258267
require.ErrorContains(t, err, tt.err)
259268
} else {

examples/launch_naked.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import exec from 'k6/execution';
2+
import { chromium } from 'k6/x/browser';
3+
4+
export const options = {}
5+
6+
export default function() {
7+
try {
8+
const browser = chromium.launch();
9+
browser.close();
10+
} catch (e) {
11+
// The test should not fail when launching the browser without
12+
// options. Try catch is used to report the error to the shell.
13+
exec.test.abort(e);
14+
}
15+
}

0 commit comments

Comments
 (0)