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

Commit 62fa217

Browse files
author
Ivan Mirić
committed
Validate FrameSetContent WaitUntil using UnmarshalText
1 parent 707c3b9 commit 62fa217

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

common/frame_options.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,8 @@ func (o *FrameSetContentOptions) Parse(ctx context.Context, opts goja.Value) err
502502
o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
503503
case "waitUntil":
504504
lifeCycle := opts.Get(k).String()
505-
if l, ok := lifecycleEventToID[lifeCycle]; ok {
506-
o.WaitUntil = l
507-
} else {
508-
return fmt.Errorf("%q is not a valid lifecycle", lifeCycle)
505+
if err := o.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil {
506+
return fmt.Errorf("error parsing setContent options: %w", err)
509507
}
510508
}
511509
}

common/frame_options_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,41 @@ func TestFrameGotoOptionsParse(t *testing.T) {
4545
})
4646
}
4747

48+
func TestFrameSetContentOptionsParse(t *testing.T) {
49+
t.Parallel()
50+
51+
t.Run("ok", func(t *testing.T) {
52+
t.Parallel()
53+
54+
mockVU := newMockVU(t)
55+
opts := mockVU.RuntimeField.ToValue(map[string]interface{}{
56+
"waitUntil": "networkidle",
57+
})
58+
scOpts := NewFrameSetContentOptions(30 * time.Second)
59+
err := scOpts.Parse(mockVU.CtxField, opts)
60+
require.NoError(t, err)
61+
62+
assert.Equal(t, 30*time.Second, scOpts.Timeout)
63+
assert.Equal(t, LifecycleEventNetworkIdle, scOpts.WaitUntil)
64+
})
65+
66+
t.Run("err/invalid_waitUntil", func(t *testing.T) {
67+
t.Parallel()
68+
69+
mockVU := newMockVU(t)
70+
opts := mockVU.RuntimeField.ToValue(map[string]interface{}{
71+
"waitUntil": "none",
72+
})
73+
navOpts := NewFrameSetContentOptions(0)
74+
err := navOpts.Parse(mockVU.CtxField, opts)
75+
76+
assert.EqualError(t, err,
77+
`error parsing setContent options: `+
78+
`invalid lifecycle event: "none"; must be one of: `+
79+
`load, domcontentloaded, networkidle`)
80+
})
81+
}
82+
4883
func TestFrameWaitForNavigationOptionsParse(t *testing.T) {
4984
t.Parallel()
5085

0 commit comments

Comments
 (0)