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

Commit cbac709

Browse files
author
Ivan Mirić
committed
Validate FrameWaitForNavigation WaitUntil using UnmarshalText
1 parent 1d32ee4 commit cbac709

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

common/frame_options.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,8 @@ func (o *FrameWaitForNavigationOptions) Parse(ctx context.Context, opts goja.Val
678678
o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
679679
case "waitUntil":
680680
lifeCycle := opts.Get(k).String()
681-
if l, ok := lifecycleEventToID[lifeCycle]; ok {
682-
o.WaitUntil = l
683-
} else {
684-
return fmt.Errorf("%q is not a valid lifecycle", lifeCycle)
681+
if err := o.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil {
682+
return fmt.Errorf("error parsing waitForNavigation options: %w", err)
685683
}
686684
}
687685
}

common/frame_options_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package common
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestFrameWaitForNavigationOptionsParse(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+
"url": "https://example.com/",
20+
"timeout": "1000",
21+
"waitUntil": "networkidle",
22+
})
23+
navOpts := NewFrameWaitForNavigationOptions(0)
24+
err := navOpts.Parse(mockVU.CtxField, opts)
25+
require.NoError(t, err)
26+
27+
assert.Equal(t, "https://example.com/", navOpts.URL)
28+
assert.Equal(t, time.Second, navOpts.Timeout)
29+
assert.Equal(t, LifecycleEventNetworkIdle, navOpts.WaitUntil)
30+
})
31+
32+
t.Run("err/invalid_waitUntil", func(t *testing.T) {
33+
t.Parallel()
34+
35+
mockVU := newMockVU(t)
36+
opts := mockVU.RuntimeField.ToValue(map[string]interface{}{
37+
"waitUntil": "none",
38+
})
39+
navOpts := NewFrameWaitForNavigationOptions(0)
40+
err := navOpts.Parse(mockVU.CtxField, opts)
41+
42+
assert.EqualError(t, err,
43+
`error parsing waitForNavigation options: `+
44+
`invalid lifecycle event: "none"; must be one of: `+
45+
`load, domcontentloaded, networkidle`)
46+
})
47+
}

0 commit comments

Comments
 (0)