diff --git a/browser/mapping_test.go b/browser/mapping_test.go index 45836adf9..cd25d16d6 100644 --- a/browser/mapping_test.go +++ b/browser/mapping_test.go @@ -332,7 +332,7 @@ type pageAPI interface { IsVisible(selector string, opts sobek.Value) (bool, error) Locator(selector string, opts sobek.Value) *common.Locator MainFrame() *common.Frame - On(event string, handler func(common.PageOnEvent) error) error + On(event common.PageOnEventName, handler func(common.PageOnEvent) error) error Opener() pageAPI Press(selector string, key string, opts sobek.Value) error Query(selector string) (*common.ElementHandle, error) diff --git a/browser/page_mapping.go b/browser/page_mapping.go index 67e95fe01..a174ad15e 100644 --- a/browser/page_mapping.go +++ b/browser/page_mapping.go @@ -203,7 +203,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return rt.ToValue(mf).ToObject(rt) }, "mouse": mapMouse(vu, p.GetMouse()), - "on": func(event string, handler sobek.Callable) error { + "on": func(event common.PageOnEventName, handler sobek.Callable) error { tq := vu.taskQueueRegistry.get(vu.Context(), p.TargetID()) var runInTaskQueue func(common.PageOnEvent) diff --git a/browser/sync_page_mapping.go b/browser/sync_page_mapping.go index 0a737ba64..c9bf1ec43 100644 --- a/browser/sync_page_mapping.go +++ b/browser/sync_page_mapping.go @@ -113,7 +113,7 @@ func syncMapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return rt.ToValue(mf).ToObject(rt) }, "mouse": rt.ToValue(p.GetMouse()).ToObject(rt), - "on": func(event string, handler sobek.Callable) error { + "on": func(event common.PageOnEventName, handler sobek.Callable) error { tq := vu.taskQueueRegistry.get(vu.Context(), p.TargetID()) mapMsgAndHandleEvent := func(m *common.ConsoleMessage) error { diff --git a/common/page.go b/common/page.go index c69df3f04..87935ddd5 100644 --- a/common/page.go +++ b/common/page.go @@ -32,11 +32,17 @@ import ( // BlankPage represents a blank page. const BlankPage = "about:blank" +// PageOnEventName represents the name of the page.on event. +type PageOnEventName string + +const webVitalBinding = "k6browserSendWebVitalMetric" + const ( - webVitalBinding = "k6browserSendWebVitalMetric" + // EventPageConsoleAPICalled represents the page.on('console') event. + EventPageConsoleAPICalled PageOnEventName = "console" - EventPageConsoleAPICalled = "console" - EventPageMetricCalled = "metric" + // EventPageMetricCalled represents the page.on('metric') event. + EventPageMetricCalled PageOnEventName = "metric" ) // MediaType represents the type of media to emulate. @@ -238,7 +244,7 @@ type Page struct { backgroundPage bool eventCh chan Event - eventHandlers map[string][]func(PageOnEvent) + eventHandlers map[PageOnEventName][]func(PageOnEvent) eventHandlersMu sync.RWMutex mainFrameSession *FrameSession @@ -277,7 +283,7 @@ func NewPage( Keyboard: NewKeyboard(ctx, s), jsEnabled: true, eventCh: make(chan Event), - eventHandlers: make(map[string][]func(PageOnEvent)), + eventHandlers: make(map[PageOnEventName][]func(PageOnEvent)), frameSessions: make(map[cdp.FrameID]*FrameSession), workers: make(map[target.SessionID]*Worker), vu: k6ext.GetVU(ctx), @@ -1106,14 +1112,7 @@ type PageOnEvent struct { // On subscribes to a page event for which the given handler will be executed // passing in the ConsoleMessage associated with the event. // The only accepted event value is 'console'. -func (p *Page) On(event string, handler func(PageOnEvent)) error { - switch event { - case EventPageConsoleAPICalled: - case EventPageMetricCalled: - default: - return fmt.Errorf("unknown page event: %q", event) - } - +func (p *Page) On(event PageOnEventName, handler func(PageOnEvent)) error { p.eventHandlersMu.Lock() defer p.eventHandlersMu.Unlock()