1
1
package browser
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
6
7
"github.com/grafana/sobek"
7
8
8
9
"go.k6.io/k6/internal/js/modules/k6/browser/common"
9
10
"go.k6.io/k6/internal/js/modules/k6/browser/k6ext"
11
+ k6common "go.k6.io/k6/js/common"
10
12
)
11
13
12
14
// mapLocator API to the JS module.
@@ -57,6 +59,10 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {
57
59
return nil , lo .Click (popts ) //nolint:wrapcheck
58
60
}), nil
59
61
},
62
+ "contentFrame" : func () * sobek.Object {
63
+ ml := mapFrameLocator (vu , lo .ContentFrame ())
64
+ return rt .ToValue (ml ).ToObject (rt )
65
+ },
60
66
"count" : func () * sobek.Promise {
61
67
return k6ext .Promise (vu .Context (), func () (any , error ) {
62
68
return lo .Count () //nolint:wrapcheck
@@ -117,6 +123,11 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {
117
123
return nil , lo .Fill (value , opts ) //nolint:wrapcheck
118
124
})
119
125
},
126
+ "filter" : func (opts sobek.Value ) mapping {
127
+ return mapLocator (vu , lo .Filter (& common.LocatorFilterOptions {
128
+ LocatorOptions : parseLocatorOptions (rt , opts ),
129
+ }))
130
+ },
120
131
"first" : func () * sobek.Object {
121
132
ml := mapLocator (vu , lo .First ())
122
133
return rt .ToValue (ml ).ToObject (rt )
@@ -138,6 +149,69 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {
138
149
return s , nil
139
150
})
140
151
},
152
+ "getByAltText" : func (alt sobek.Value , opts sobek.Value ) (* sobek.Object , error ) {
153
+ if k6common .IsNullish (alt ) {
154
+ return nil , errors .New ("missing required argument 'altText'" )
155
+ }
156
+ palt , popts := parseGetByBaseOptions (vu .Context (), alt , false , opts )
157
+
158
+ ml := mapLocator (vu , lo .GetByAltText (palt , popts ))
159
+ return rt .ToValue (ml ).ToObject (rt ), nil
160
+ },
161
+ "getByLabel" : func (label sobek.Value , opts sobek.Value ) (* sobek.Object , error ) {
162
+ if k6common .IsNullish (label ) {
163
+ return nil , errors .New ("missing required argument 'label'" )
164
+ }
165
+ plabel , popts := parseGetByBaseOptions (vu .Context (), label , true , opts )
166
+
167
+ ml := mapLocator (vu , lo .GetByLabel (plabel , popts ))
168
+ return rt .ToValue (ml ).ToObject (rt ), nil
169
+ },
170
+ "getByPlaceholder" : func (placeholder sobek.Value , opts sobek.Value ) (* sobek.Object , error ) {
171
+ if k6common .IsNullish (placeholder ) {
172
+ return nil , errors .New ("missing required argument 'placeholder'" )
173
+ }
174
+ pplaceholder , popts := parseGetByBaseOptions (vu .Context (), placeholder , false , opts )
175
+
176
+ ml := mapLocator (vu , lo .GetByPlaceholder (pplaceholder , popts ))
177
+ return rt .ToValue (ml ).ToObject (rt ), nil
178
+ },
179
+ "getByRole" : func (role sobek.Value , opts sobek.Value ) (* sobek.Object , error ) {
180
+ if k6common .IsNullish (role ) {
181
+ return nil , errors .New ("missing required argument 'role'" )
182
+ }
183
+ popts := parseGetByRoleOptions (vu .Context (), opts )
184
+
185
+ ml := mapLocator (vu , lo .GetByRole (role .String (), popts ))
186
+ return rt .ToValue (ml ).ToObject (rt ), nil
187
+ },
188
+ "getByTestId" : func (testID sobek.Value ) (* sobek.Object , error ) {
189
+ if k6common .IsNullish (testID ) {
190
+ return nil , errors .New ("missing required argument 'testId'" )
191
+ }
192
+ ptestID := parseStringOrRegex (testID , false )
193
+
194
+ ml := mapLocator (vu , lo .GetByTestID (ptestID ))
195
+ return rt .ToValue (ml ).ToObject (rt ), nil
196
+ },
197
+ "getByText" : func (text sobek.Value , opts sobek.Value ) (* sobek.Object , error ) {
198
+ if k6common .IsNullish (text ) {
199
+ return nil , errors .New ("missing required argument 'text'" )
200
+ }
201
+ ptext , popts := parseGetByBaseOptions (vu .Context (), text , true , opts )
202
+
203
+ ml := mapLocator (vu , lo .GetByText (ptext , popts ))
204
+ return rt .ToValue (ml ).ToObject (rt ), nil
205
+ },
206
+ "getByTitle" : func (title sobek.Value , opts sobek.Value ) (* sobek.Object , error ) {
207
+ if k6common .IsNullish (title ) {
208
+ return nil , errors .New ("missing required argument 'title'" )
209
+ }
210
+ ptitle , popts := parseGetByBaseOptions (vu .Context (), title , false , opts )
211
+
212
+ ml := mapLocator (vu , lo .GetByTitle (ptitle , popts ))
213
+ return rt .ToValue (ml ).ToObject (rt ), nil
214
+ },
141
215
"locator" : func (selector string ) * sobek.Object {
142
216
ml := mapLocator (vu , lo .Locator (selector ))
143
217
return rt .ToValue (ml ).ToObject (rt )
@@ -222,3 +296,23 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {
222
296
},
223
297
}
224
298
}
299
+
300
+ func parseLocatorOptions (rt * sobek.Runtime , opts sobek.Value ) * common.LocatorOptions {
301
+ if k6common .IsNullish (opts ) {
302
+ return nil
303
+ }
304
+
305
+ var popts common.LocatorOptions
306
+
307
+ obj := opts .ToObject (rt )
308
+ for _ , k := range obj .Keys () {
309
+ switch k {
310
+ case "hasText" :
311
+ popts .HasText = obj .Get (k ).String ()
312
+ case "hasNotText" :
313
+ popts .HasNotText = obj .Get (k ).String ()
314
+ }
315
+ }
316
+
317
+ return & popts
318
+ }
0 commit comments