Skip to content

Commit a549fb1

Browse files
committed
Add waitForURL in frame mapping
1 parent 6f4bb32 commit a549fb1

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package browser
22

33
import (
44
"fmt"
5+
"reflect"
56

67
"github.com/grafana/sobek"
78

@@ -422,6 +423,31 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping {
422423
return nil, nil
423424
})
424425
},
426+
"waitForURL": func(url sobek.Value, opts sobek.Value) (*sobek.Promise, error) {
427+
popts := common.NewFrameWaitForURLOptions(f.Timeout())
428+
if err := popts.Parse(vu.Context(), opts); err != nil {
429+
return nil, fmt.Errorf("parsing waitForURL options: %w", err)
430+
}
431+
432+
var val string
433+
switch url.ExportType() {
434+
case reflect.TypeOf(string("")):
435+
val = fmt.Sprintf("'%s'", url.String()) // Strings require quotes
436+
default: // JS Regex, CSS, numbers or booleans
437+
val = url.String() // No quotes
438+
}
439+
440+
// Inject JS regex checker for URL pattern matching
441+
ctx := vu.Context()
442+
jsRegexChecker, err := injectRegexMatcherScript(ctx, vu, f.Page().TargetID())
443+
if err != nil {
444+
return nil, err
445+
}
446+
447+
return k6ext.Promise(ctx, func() (result any, reason error) {
448+
return nil, f.WaitForURL(val, popts, jsRegexChecker)
449+
}), nil
450+
},
425451
}
426452
maps["$"] = func(selector string) *sobek.Promise {
427453
return k6ext.Promise(vu.Context(), func() (any, error) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ type frameAPI interface { //nolint:interfacebloat
433433
WaitForNavigation(opts sobek.Value) (*common.Response, error)
434434
WaitForSelector(selector string, opts sobek.Value) (*common.ElementHandle, error)
435435
WaitForTimeout(timeout int64)
436+
WaitForURL(url string, opts sobek.Value) (*sobek.Promise, error)
436437
}
437438

438439
// elementHandleAPI is the interface of an in-page DOM element.

0 commit comments

Comments
 (0)