Skip to content

Commit 76e7366

Browse files
committed
Add waitForURL in page mapping
1 parent a549fb1 commit 76e7366

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

internal/js/modules/k6/browser/browser/mapping_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ type pageAPI interface { //nolint:interfacebloat
365365
WaitForNavigation(opts sobek.Value) (*common.Response, error)
366366
WaitForSelector(selector string, opts sobek.Value) (*common.ElementHandle, error)
367367
WaitForTimeout(timeout int64)
368+
WaitForURL(url string, opts sobek.Value) (*sobek.Promise, error)
368369
Workers() []*common.Worker
369370
}
370371

internal/js/modules/k6/browser/browser/page_mapping.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,31 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop
535535
return nil, nil
536536
})
537537
},
538+
"waitForURL": func(url sobek.Value, opts sobek.Value) (*sobek.Promise, error) {
539+
popts := common.NewFrameWaitForURLOptions(p.Timeout())
540+
if err := popts.Parse(vu.Context(), opts); err != nil {
541+
return nil, fmt.Errorf("parsing waitForURL options: %w", err)
542+
}
543+
544+
var val string
545+
switch url.ExportType() {
546+
case reflect.TypeOf(string("")):
547+
val = fmt.Sprintf("'%s'", url.String()) // Strings require quotes
548+
default: // JS Regex, CSS, numbers or booleans
549+
val = url.String() // No quotes
550+
}
551+
552+
// Inject JS regex checker for URL pattern matching
553+
ctx := vu.Context()
554+
jsRegexChecker, err := injectRegexMatcherScript(ctx, vu, p.TargetID())
555+
if err != nil {
556+
return nil, err
557+
}
558+
559+
return k6ext.Promise(ctx, func() (result any, reason error) {
560+
return nil, p.WaitForURL(val, popts, jsRegexChecker)
561+
}), nil
562+
},
538563
"workers": func() *sobek.Object {
539564
var mws []mapping
540565
for _, w := range p.Workers() {

0 commit comments

Comments
 (0)