Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to type safe page.on event names #1477

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion browser/page_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion browser/sync_page_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 12 additions & 13 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()

Expand Down
Loading