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

Commit 707c3b9

Browse files
author
Ivan Mirić
committed
Validate FrameGoto WaitUntil using UnmarshalText
1 parent cbac709 commit 707c3b9

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

common/frame_options.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,8 @@ func (o *FrameGotoOptions) Parse(ctx context.Context, opts goja.Value) error {
297297
o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
298298
case "waitUntil":
299299
lifeCycle := opts.Get(k).String()
300-
if l, ok := lifecycleEventToID[lifeCycle]; ok {
301-
o.WaitUntil = l
302-
} else {
303-
return fmt.Errorf("%q is not a valid lifecycle", lifeCycle)
300+
if err := o.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil {
301+
return fmt.Errorf("error parsing goto options: %w", err)
304302
}
305303
}
306304
}

common/frame_options_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,43 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11+
func TestFrameGotoOptionsParse(t *testing.T) {
12+
t.Parallel()
13+
14+
t.Run("ok", func(t *testing.T) {
15+
t.Parallel()
16+
17+
mockVU := newMockVU(t)
18+
opts := mockVU.RuntimeField.ToValue(map[string]interface{}{
19+
"timeout": "1000",
20+
"waitUntil": "networkidle",
21+
})
22+
gotoOpts := NewFrameGotoOptions("https://example.com/", 0)
23+
err := gotoOpts.Parse(mockVU.CtxField, opts)
24+
require.NoError(t, err)
25+
26+
assert.Equal(t, "https://example.com/", gotoOpts.Referer)
27+
assert.Equal(t, time.Second, gotoOpts.Timeout)
28+
assert.Equal(t, LifecycleEventNetworkIdle, gotoOpts.WaitUntil)
29+
})
30+
31+
t.Run("err/invalid_waitUntil", func(t *testing.T) {
32+
t.Parallel()
33+
34+
mockVU := newMockVU(t)
35+
opts := mockVU.RuntimeField.ToValue(map[string]interface{}{
36+
"waitUntil": "none",
37+
})
38+
navOpts := NewFrameGotoOptions("", 0)
39+
err := navOpts.Parse(mockVU.CtxField, opts)
40+
41+
assert.EqualError(t, err,
42+
`error parsing goto options: `+
43+
`invalid lifecycle event: "none"; must be one of: `+
44+
`load, domcontentloaded, networkidle`)
45+
})
46+
}
47+
1148
func TestFrameWaitForNavigationOptionsParse(t *testing.T) {
1249
t.Parallel()
1350

0 commit comments

Comments
 (0)