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

Unify page.on event mappings #1473

Merged
merged 8 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ func TestMappings(t *testing.T) {
"mapMetricEvent": {
apiInterface: (*metricEventAPI)(nil),
mapp: func() mapping {
m, _ := mapMetricEvent(moduleVU{VU: vu}, &common.MetricEvent{})
return m
return mapMetricEvent(moduleVU{VU: vu}, &common.MetricEvent{})
},
},
"mapTouchscreen": {
Expand Down
18 changes: 2 additions & 16 deletions browser/metric_event_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,9 @@ import (
)

// mapMetricEvent to the JS module.
func mapMetricEvent(vu moduleVU, cm *common.MetricEvent) (mapping, error) {
func mapMetricEvent(vu moduleVU, cm *common.MetricEvent) mapping {
rt := vu.VU.Runtime()

// We're setting up the function in the Sobek context that will be reused
// for this VU.
_, err := rt.RunString(`
function _k6BrowserCheckRegEx(pattern, url) {
let r = pattern;
if (typeof pattern === 'string') {
r = new RegExp(pattern);
}
return r.test(url);
}`)
if err != nil {
return nil, fmt.Errorf("evaluating regex function: %w", err)
}

return mapping{
"tag": func(urls common.URLTagPatterns) error {
callback := func(pattern, url string) (bool, error) {
Expand All @@ -39,5 +25,5 @@ func mapMetricEvent(vu moduleVU, cm *common.MetricEvent) (mapping, error) {

return cm.Tag(callback, urls)
},
}, nil
}
}
28 changes: 22 additions & 6 deletions browser/page_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,11 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop
return errors.New("incorrect metric message")
}

mapping, err := mapMetricEvent(vu, m)
if err != nil {
return fmt.Errorf("mapping the metric: %w", err)
}

if _, err = handler(sobek.Undefined(), vu.VU.Runtime().ToValue(mapping)); err != nil {
mapping := mapMetricEvent(vu, m)
if _, err := handler(sobek.Undefined(), vu.VU.Runtime().ToValue(mapping)); err != nil {
return fmt.Errorf("executing page.on('metric') handler: %w", err)
}

return nil
})
<-c
Expand All @@ -259,6 +256,25 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop
return fmt.Errorf("unknown page event: %q", event)
}

if event == common.EventPageMetricCalled {
// Register a custom regex function for the metric event
// that will be used to check URLs against the patterns.
// This is needed because we want to use the JavaScript regex
// to comply with what users expect when using the `tag` method.
_, err := rt.RunString(`
function _k6BrowserCheckRegEx(pattern, url) {
let r = pattern;
if (typeof pattern === 'string') {
r = new RegExp(pattern);
}
return r.test(url);
}
`)
if err != nil {
return fmt.Errorf("evaluating regex function: %w", err)
}
}

inancgumus marked this conversation as resolved.
Show resolved Hide resolved
return p.On(event, runInTaskQueue) //nolint:wrapcheck
},
"opener": func() *sobek.Promise {
Expand Down
Loading