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

Commit 5738545

Browse files
committed
Refactor away goja from dispatch event
This is a necessary fix to remove passing Goja values to ExecutionContext.
1 parent 323e7c2 commit 5738545

File tree

7 files changed

+101
-72
lines changed

7 files changed

+101
-72
lines changed

browser/mapping.go

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,35 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {
6262
return nil, lo.Click(popts) //nolint:wrapcheck
6363
}), nil
6464
},
65-
"dblclick": lo.Dblclick,
66-
"check": lo.Check,
67-
"uncheck": lo.Uncheck,
68-
"isChecked": lo.IsChecked,
69-
"isEditable": lo.IsEditable,
70-
"isEnabled": lo.IsEnabled,
71-
"isDisabled": lo.IsDisabled,
72-
"isVisible": lo.IsVisible,
73-
"isHidden": lo.IsHidden,
74-
"fill": lo.Fill,
75-
"focus": lo.Focus,
76-
"getAttribute": lo.GetAttribute,
77-
"innerHTML": lo.InnerHTML,
78-
"innerText": lo.InnerText,
79-
"textContent": lo.TextContent,
80-
"inputValue": lo.InputValue,
81-
"selectOption": lo.SelectOption,
82-
"press": lo.Press,
83-
"type": lo.Type,
84-
"hover": lo.Hover,
85-
"tap": lo.Tap,
86-
"dispatchEvent": lo.DispatchEvent,
87-
"waitFor": lo.WaitFor,
65+
"dblclick": lo.Dblclick,
66+
"check": lo.Check,
67+
"uncheck": lo.Uncheck,
68+
"isChecked": lo.IsChecked,
69+
"isEditable": lo.IsEditable,
70+
"isEnabled": lo.IsEnabled,
71+
"isDisabled": lo.IsDisabled,
72+
"isVisible": lo.IsVisible,
73+
"isHidden": lo.IsHidden,
74+
"fill": lo.Fill,
75+
"focus": lo.Focus,
76+
"getAttribute": lo.GetAttribute,
77+
"innerHTML": lo.InnerHTML,
78+
"innerText": lo.InnerText,
79+
"textContent": lo.TextContent,
80+
"inputValue": lo.InputValue,
81+
"selectOption": lo.SelectOption,
82+
"press": lo.Press,
83+
"type": lo.Type,
84+
"hover": lo.Hover,
85+
"tap": lo.Tap,
86+
"dispatchEvent": func(typ string, eventInit, opts goja.Value) error {
87+
popts := common.NewFrameDispatchEventOptions(lo.DefaultTimeout())
88+
if err := popts.Parse(vu.Context(), opts); err != nil {
89+
return fmt.Errorf("parsing locator dispatch event options: %w", err)
90+
}
91+
return lo.DispatchEvent(typ, eventInit.Export(), popts) //nolint:wrapcheck
92+
},
93+
"waitFor": lo.WaitFor,
8894
}
8995
}
9096

@@ -249,21 +255,23 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping {
249255
}
250256
return mapFrame(vu, f), nil
251257
},
252-
"dblclick": eh.Dblclick,
253-
"dispatchEvent": eh.DispatchEvent,
254-
"fill": eh.Fill,
255-
"focus": eh.Focus,
256-
"getAttribute": eh.GetAttribute,
257-
"hover": eh.Hover,
258-
"innerHTML": eh.InnerHTML,
259-
"innerText": eh.InnerText,
260-
"inputValue": eh.InputValue,
261-
"isChecked": eh.IsChecked,
262-
"isDisabled": eh.IsDisabled,
263-
"isEditable": eh.IsEditable,
264-
"isEnabled": eh.IsEnabled,
265-
"isHidden": eh.IsHidden,
266-
"isVisible": eh.IsVisible,
258+
"dblclick": eh.Dblclick,
259+
"dispatchEvent": func(typ string, eventInit goja.Value) error {
260+
return eh.DispatchEvent(typ, eventInit.Export()) //nolint:wrapcheck
261+
},
262+
"fill": eh.Fill,
263+
"focus": eh.Focus,
264+
"getAttribute": eh.GetAttribute,
265+
"hover": eh.Hover,
266+
"innerHTML": eh.InnerHTML,
267+
"innerText": eh.InnerText,
268+
"inputValue": eh.InputValue,
269+
"isChecked": eh.IsChecked,
270+
"isDisabled": eh.IsDisabled,
271+
"isEditable": eh.IsEditable,
272+
"isEnabled": eh.IsEnabled,
273+
"isHidden": eh.IsHidden,
274+
"isVisible": eh.IsVisible,
267275
"ownerFrame": func() (mapping, error) {
268276
f, err := eh.OwnerFrame()
269277
if err != nil {
@@ -355,9 +363,15 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping {
355363
return nil, err //nolint:wrapcheck
356364
}), nil
357365
},
358-
"content": f.Content,
359-
"dblclick": f.Dblclick,
360-
"dispatchEvent": f.DispatchEvent,
366+
"content": f.Content,
367+
"dblclick": f.Dblclick,
368+
"dispatchEvent": func(selector, typ string, eventInit, opts goja.Value) error {
369+
popts := common.NewFrameDispatchEventOptions(f.Timeout())
370+
if err := popts.Parse(vu.Context(), opts); err != nil {
371+
return fmt.Errorf("parsing frame dispatch event options: %w", err)
372+
}
373+
return f.DispatchEvent(selector, typ, eventInit.Export(), popts) //nolint:wrapcheck
374+
},
361375
"evaluate": func(pageFunction goja.Value, gargs ...goja.Value) any {
362376
args := make([]any, 0, len(gargs))
363377
for _, a := range gargs {
@@ -518,10 +532,16 @@ func mapPage(vu moduleVU, p *common.Page) mapping {
518532

519533
return p.Close(opts) //nolint:wrapcheck
520534
},
521-
"content": p.Content,
522-
"context": p.Context,
523-
"dblclick": p.Dblclick,
524-
"dispatchEvent": p.DispatchEvent,
535+
"content": p.Content,
536+
"context": p.Context,
537+
"dblclick": p.Dblclick,
538+
"dispatchEvent": func(selector, typ string, eventInit, opts goja.Value) error {
539+
popts := common.NewFrameDispatchEventOptions(p.Timeout())
540+
if err := popts.Parse(vu.Context(), opts); err != nil {
541+
return fmt.Errorf("parsing page dispatch event options: %w", err)
542+
}
543+
return p.DispatchEvent(selector, typ, eventInit.Export(), popts) //nolint:wrapcheck
544+
},
525545
"dragAndDrop": p.DragAndDrop,
526546
"emulateMedia": p.EmulateMedia,
527547
"emulateVisionDeficiency": p.EmulateVisionDeficiency,

common/element_handle.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (h *ElementHandle) defaultTimeout() time.Duration {
233233
return h.frame.manager.timeoutSettings.timeout()
234234
}
235235

236-
func (h *ElementHandle) dispatchEvent(_ context.Context, typ string, eventInit goja.Value) (any, error) {
236+
func (h *ElementHandle) dispatchEvent(_ context.Context, typ string, eventInit any) (any, error) {
237237
fn := `
238238
(node, injected, type, eventInit) => {
239239
injected.dispatchEvent(node, type, eventInit);
@@ -774,17 +774,21 @@ func (h *ElementHandle) Dblclick(opts goja.Value) {
774774
applySlowMo(h.ctx)
775775
}
776776

777-
func (h *ElementHandle) DispatchEvent(typ string, eventInit goja.Value) {
777+
// DispatchEvent dispatches a DOM event to the element.
778+
func (h *ElementHandle) DispatchEvent(typ string, eventInit any) error {
778779
fn := func(apiCtx context.Context, handle *ElementHandle) (any, error) {
779780
return handle.dispatchEvent(apiCtx, typ, eventInit)
780781
}
781782
opts := NewElementHandleBaseOptions(h.defaultTimeout())
782783
actFn := h.newAction([]string{}, fn, opts.Force, opts.NoWaitAfter, opts.Timeout)
783784
_, err := call(h.ctx, actFn, opts.Timeout)
784785
if err != nil {
785-
k6ext.Panic(h.ctx, "dispatching element event: %w", err)
786+
return fmt.Errorf("dispatching element event %q: %w", typ, err)
786787
}
788+
787789
applySlowMo(h.ctx)
790+
791+
return nil
788792
}
789793

790794
func (h *ElementHandle) Fill(value string, opts goja.Value) {

common/execution_context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ func (e *ExecutionContext) Eval(apiCtx context.Context, js string, args ...any)
303303
for _, a := range args {
304304
evalArgs = append(evalArgs, a)
305305
}
306+
306307
return e.eval(apiCtx, opts, js, evalArgs...)
307308
}
308309

common/frame.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -719,22 +719,20 @@ func (f *Frame) dblclick(selector string, opts *FrameDblclickOptions) error {
719719
}
720720

721721
// DispatchEvent dispatches an event for the first element matching the selector.
722-
func (f *Frame) DispatchEvent(selector, typ string, eventInit, opts goja.Value) {
722+
func (f *Frame) DispatchEvent(selector, typ string, eventInit any, opts *FrameDispatchEventOptions) error {
723723
f.log.Debugf("Frame:DispatchEvent", "fid:%s furl:%q sel:%q typ:%q", f.ID(), f.URL(), selector, typ)
724724

725-
popts := NewFrameDispatchEventOptions(f.defaultTimeout())
726-
if err := popts.Parse(f.ctx, opts); err != nil {
727-
k6ext.Panic(f.ctx, "parsing dispatch event options: %w", err)
728-
}
729-
if err := f.dispatchEvent(selector, typ, eventInit, popts); err != nil {
730-
k6ext.Panic(f.ctx, "dispatching event %q to %q: %w", typ, selector, err)
725+
if err := f.dispatchEvent(selector, typ, eventInit, opts); err != nil {
726+
return fmt.Errorf("dispatching event %q to %q: %w", typ, selector, err)
731727
}
732728
applySlowMo(f.ctx)
729+
730+
return nil
733731
}
734732

735733
// dispatchEvent is like DispatchEvent but takes parsed options and neither throws
736734
// an error, or applies slow motion.
737-
func (f *Frame) dispatchEvent(selector, typ string, eventInit goja.Value, opts *FrameDispatchEventOptions) error {
735+
func (f *Frame) dispatchEvent(selector, typ string, eventInit any, opts *FrameDispatchEventOptions) error {
738736
dispatchEvent := func(apiCtx context.Context, handle *ElementHandle) (any, error) {
739737
return handle.dispatchEvent(apiCtx, typ, eventInit)
740738
}

common/locator.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ func (l *Locator) tap(opts *FrameTapOptions) error {
585585

586586
// DispatchEvent dispatches an event for the element matching the
587587
// locator's selector with strict mode on.
588-
func (l *Locator) DispatchEvent(typ string, eventInit, opts goja.Value) {
588+
func (l *Locator) DispatchEvent(typ string, eventInit any, opts *FrameDispatchEventOptions) error {
589589
l.log.Debugf(
590590
"Locator:DispatchEvent", "fid:%s furl:%q sel:%q typ:%q eventInit:%+v opts:%+v",
591591
l.frame.ID(), l.frame.URL(), l.selector, typ, eventInit, opts,
@@ -594,18 +594,14 @@ func (l *Locator) DispatchEvent(typ string, eventInit, opts goja.Value) {
594594
var err error
595595
defer func() { panicOrSlowMo(l.ctx, err) }()
596596

597-
popts := NewFrameDispatchEventOptions(l.frame.defaultTimeout())
598-
if err = popts.Parse(l.ctx, opts); err != nil {
599-
err = fmt.Errorf("parsing dispatch event options: %w", err)
600-
return
601-
}
602-
if err = l.dispatchEvent(typ, eventInit, popts); err != nil {
603-
err = fmt.Errorf("dispatching event %q to %q: %w", typ, l.selector, err)
604-
return
597+
if err = l.dispatchEvent(typ, eventInit, opts); err != nil {
598+
return fmt.Errorf("dispatching locator event %q to %q: %w", typ, l.selector, err)
605599
}
600+
601+
return nil
606602
}
607603

608-
func (l *Locator) dispatchEvent(typ string, eventInit goja.Value, opts *FrameDispatchEventOptions) error {
604+
func (l *Locator) dispatchEvent(typ string, eventInit any, opts *FrameDispatchEventOptions) error {
609605
opts.Strict = true
610606
return l.frame.dispatchEvent(l.selector, typ, eventInit, opts)
611607
}
@@ -627,3 +623,9 @@ func (l *Locator) waitFor(opts *FrameWaitForSelectorOptions) error {
627623
opts.Strict = true
628624
return l.frame.waitFor(l.selector, opts)
629625
}
626+
627+
// DefaultTimeout returns the default timeout for the locator.
628+
// This is an internal API and should not be used by users.
629+
func (l *Locator) DefaultTimeout() time.Duration {
630+
return l.frame.defaultTimeout()
631+
}

common/page.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,10 +716,11 @@ func (p *Page) Dblclick(selector string, opts goja.Value) {
716716
p.MainFrame().Dblclick(selector, opts)
717717
}
718718

719-
func (p *Page) DispatchEvent(selector string, typ string, eventInit goja.Value, opts goja.Value) {
719+
// DispatchEvent dispatches an event on the page to the element that matches the provided selector.
720+
func (p *Page) DispatchEvent(selector string, typ string, eventInit any, opts *FrameDispatchEventOptions) error {
720721
p.logger.Debugf("Page:DispatchEvent", "sid:%v selector:%s", p.sessionID(), selector)
721722

722-
p.MainFrame().DispatchEvent(selector, typ, eventInit, opts)
723+
return p.MainFrame().DispatchEvent(selector, typ, eventInit, opts)
723724
}
724725

725726
// DragAndDrop is not implemented.

tests/locator_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type jsFrameWaitForSelectorOpts struct {
2626

2727
func TestLocator(t *testing.T) {
2828
t.Parallel()
29-
t.Skip("TODO: fix goja escape")
3029

3130
tests := []struct {
3231
name string
@@ -91,7 +90,11 @@ func TestLocator(t *testing.T) {
9190
return asBool(t, v)
9291
}
9392
require.False(t, result(), "should not be clicked first")
94-
p.Locator("#link", nil).DispatchEvent("click", tb.toGojaValue("mouseevent"), nil)
93+
opts := &common.FrameDispatchEventOptions{
94+
FrameBaseOptions: &common.FrameBaseOptions{},
95+
}
96+
err := p.Locator("#link", nil).DispatchEvent("click", "mouseevent", opts)
97+
require.NoError(t, err)
9598
require.True(t, result(), "cannot not dispatch event")
9699
},
97100
},
@@ -280,7 +283,7 @@ func TestLocator(t *testing.T) {
280283
},
281284
{
282285
"DispatchEvent", func(l *common.Locator, tb *testBrowser) {
283-
l.DispatchEvent("click", tb.toGojaValue("mouseevent"), timeout(tb))
286+
l.DispatchEvent("click", "mouseevent", common.NewFrameDispatchEventOptions(100*time.Millisecond))
284287
},
285288
},
286289
{
@@ -334,7 +337,7 @@ func TestLocator(t *testing.T) {
334337
tb := newTestBrowser(t)
335338
p := tb.NewPage(nil)
336339
p.SetContent("<html></html>", nil)
337-
assert.Panics(t, func() { tt.do(p.Locator("NOTEXIST", nil), tb) })
340+
assert.Panics(t, func() { tt.do(t, p.Locator("NOTEXIST", nil), tb) })
338341
})
339342
}
340343

0 commit comments

Comments
 (0)