diff --git a/browser/browser_context_mapping.go b/browser/browser_context_mapping.go index 5c2fe7baf..09767a9d0 100644 --- a/browser/browser_context_mapping.go +++ b/browser/browser_context_mapping.go @@ -71,7 +71,7 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin }) }, "cookies": func(urls ...string) *sobek.Promise { - fmt.Println(getCurrentLineNumber(vu)) + pauseOnBreakpoint(vu) return k6ext.Promise(vu.Context(), func() (any, error) { return bc.Cookies(urls...) //nolint:wrapcheck diff --git a/browser/locator_mapping.go b/browser/locator_mapping.go index e03b8867a..3187c61ae 100644 --- a/browser/locator_mapping.go +++ b/browser/locator_mapping.go @@ -22,7 +22,7 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { //nolint:funlen }), nil }, "click": func(opts sobek.Value) (*sobek.Promise, error) { - fmt.Println(getCurrentLineNumber(vu)) + pauseOnBreakpoint(vu) popts, err := parseFrameClickOptions(vu.Context(), opts, lo.Timeout()) if err != nil { @@ -116,7 +116,7 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { //nolint:funlen }) }, "textContent": func(opts sobek.Value) *sobek.Promise { - fmt.Println(getCurrentLineNumber(vu)) + pauseOnBreakpoint(vu) return k6ext.Promise(vu.Context(), func() (any, error) { s, ok, err := lo.TextContent(opts) @@ -145,7 +145,7 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { //nolint:funlen }) }, "type": func(text string, opts sobek.Value) *sobek.Promise { - fmt.Println(getCurrentLineNumber(vu)) + pauseOnBreakpoint(vu) return k6ext.Promise(vu.Context(), func() (any, error) { return nil, lo.Type(text, opts) //nolint:wrapcheck diff --git a/browser/page_mapping.go b/browser/page_mapping.go index da22b8b2e..0e804972d 100644 --- a/browser/page_mapping.go +++ b/browser/page_mapping.go @@ -40,7 +40,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }), nil }, "close": func(opts sobek.Value) *sobek.Promise { - fmt.Println(getCurrentLineNumber(vu)) + pauseOnBreakpoint(vu) return k6ext.Promise(vu.Context(), func() (any, error) { // It's safe to close the taskqueue for this targetID (if one @@ -135,12 +135,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }) }, "goto": func(url string, opts sobek.Value) (*sobek.Promise, error) { - bp := vu.breakpointRegistry - pos := getCurrentLineNumber(vu) - if bp.matches(pos) { - time.AfterFunc(5*time.Second, func() { bp.resume(pos) }) - bp.pause(pos) - } + pauseOnBreakpoint(vu) gopts := common.NewFrameGotoOptions( p.Referrer(), @@ -363,7 +358,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }) }, "waitForNavigation": func(opts sobek.Value) (*sobek.Promise, error) { - fmt.Println(getCurrentLineNumber(vu)) + pauseOnBreakpoint(vu) popts := common.NewFrameWaitForNavigationOptions(p.Timeout()) if err := popts.Parse(vu.Context(), opts); err != nil { @@ -379,7 +374,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }), nil }, "waitForSelector": func(selector string, opts sobek.Value) *sobek.Promise { - fmt.Println(getCurrentLineNumber(vu)) + pauseOnBreakpoint(vu) return k6ext.Promise(vu.Context(), func() (any, error) { eh, err := p.WaitForSelector(selector, opts) diff --git a/browser/registry.go b/browser/registry.go index c004d90c5..30d49a741 100644 --- a/browser/registry.go +++ b/browser/registry.go @@ -12,6 +12,7 @@ import ( "strings" "sync" "sync/atomic" + "time" "github.com/mstoykov/k6-taskqueue-lib/taskqueue" "go.opentelemetry.io/otel/attribute" @@ -53,6 +54,10 @@ func newBreakpointRegistry(vu k6modules.VU) *breakpointRegistry { File: "file:///Users/inanc/grafana/k6browser/main/examples/fillform.js", Line: 26, }, + { + File: "file:///Users/inanc/grafana/k6browser/main/examples/fillform.js", + Line: 32, + }, }, pauser: make(chan chan struct{}, 1), } @@ -76,20 +81,39 @@ func (b *breakpointRegistry) matches(p position) bool { } // pause pauses the script execution. -func (b *breakpointRegistry) pause(p position) { +func (b *breakpointRegistry) pause() { c := make(chan struct{}) b.pauser <- c - fmt.Println("pausing at", p.Filename, p.Line) <-c } // resume resumes the script execution -func (b *breakpointRegistry) resume(p position) { +func (b *breakpointRegistry) resume() { c := <-b.pauser - fmt.Println("resuming at", p.Filename, p.Line) close(c) } +// pauseOnBreakpoint is a helper that pauses the script execution +// when a breakpoint is hit in the script. +func pauseOnBreakpoint(vu moduleVU) { + bp := vu.breakpointRegistry + + pos := getCurrentLineNumber(vu) + fmt.Println("current line:", pos) + + if !bp.matches(pos) { + return + } + + time.AfterFunc(5*time.Second, func() { + fmt.Println("resuming at", pos.Filename, pos.Line) + bp.resume() + }) + + fmt.Println("pausing at", pos.Filename, pos.Line) + bp.pause() +} + // pidRegistry keeps track of the launched browser process IDs. type pidRegistry struct { mu sync.RWMutex