@@ -23,10 +23,15 @@ package tests
23
23
import (
24
24
"testing"
25
25
26
+ "github.com/grafana/xk6-browser/common"
26
27
"github.com/stretchr/testify/assert"
28
+ "github.com/stretchr/testify/require"
29
+ k6metrics "go.k6.io/k6/lib/metrics"
30
+ k6stats "go.k6.io/k6/stats"
27
31
)
28
32
29
33
func TestDataURLSkipRequest (t * testing.T ) {
34
+ t .Parallel ()
30
35
tb := newTestBrowser (t )
31
36
p := tb .NewPage (nil )
32
37
@@ -36,3 +41,63 @@ func TestDataURLSkipRequest(t *testing.T) {
36
41
37
42
assert .True (t , lc .contains ("skipped request handling of data URL" ))
38
43
}
44
+
45
+ func TestMetricsEmission (t * testing.T ) {
46
+ t .Parallel ()
47
+ tb := newTestBrowser (t , withHTTPServer ())
48
+
49
+ url := tb .URL ("/get" )
50
+ browserTags := map [string ]string {
51
+ "group" : "" ,
52
+ "url" : "about:blank" ,
53
+ }
54
+ httpTags := map [string ]string {
55
+ "method" : "GET" ,
56
+ "url" : url ,
57
+ "status" : "200" ,
58
+ "group" : "" ,
59
+ "proto" : "http/1.1" ,
60
+ "from_cache" : "false" ,
61
+ "from_prefetch_cache" : "false" ,
62
+ "from_service_worker" : "false" ,
63
+ }
64
+ expMetricTags := map [string ]map [string ]string {
65
+ common .BrowserDOMContentLoaded .Name : browserTags ,
66
+ common .BrowserLoaded .Name : browserTags ,
67
+ k6metrics .DataSentName : map [string ]string {
68
+ "group" : "" ,
69
+ "method" : "GET" ,
70
+ "url" : url ,
71
+ },
72
+ k6metrics .HTTPReqsName : httpTags ,
73
+ k6metrics .HTTPReqDurationName : httpTags ,
74
+ k6metrics .DataReceivedName : httpTags ,
75
+ k6metrics .HTTPReqConnectingName : httpTags ,
76
+ k6metrics .HTTPReqTLSHandshakingName : httpTags ,
77
+ k6metrics .HTTPReqSendingName : httpTags ,
78
+ k6metrics .HTTPReqReceivingName : httpTags ,
79
+ }
80
+
81
+ p := tb .NewPage (nil )
82
+ resp := p .Goto (url , nil )
83
+ require .NotNil (t , resp )
84
+
85
+ // Wait for all metrics to be emitted
86
+ p .WaitForLoadState ("networkidle" , nil )
87
+
88
+ bufSamples := k6stats .GetBufferedSamples (tb .samples )
89
+
90
+ var reqsCount int
91
+ cb := func (sample k6stats.Sample ) {
92
+ switch sample .Metric .Name {
93
+ case k6metrics .HTTPReqsName :
94
+ reqsCount += int (sample .Value )
95
+ case k6metrics .DataSentName , k6metrics .DataReceivedName :
96
+ assert .Greaterf (t , int (sample .Value ), 0 ,
97
+ "metric %s" , sample .Metric .Name )
98
+ }
99
+ }
100
+
101
+ assertMetricsEmitted (t , bufSamples , expMetricTags , cb )
102
+ assert .Equal (t , 1 , reqsCount )
103
+ }
0 commit comments