Skip to content

Commit

Permalink
Add pauseOnBreakpoint helper
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Dec 3, 2024
1 parent 4348bfe commit bc4a855
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion browser/browser_context_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions browser/locator_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
13 changes: 4 additions & 9 deletions browser/page_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
32 changes: 28 additions & 4 deletions browser/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"sync"
"sync/atomic"
"time"

"github.com/mstoykov/k6-taskqueue-lib/taskqueue"
"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -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),
}
Expand All @@ -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
Expand Down

0 comments on commit bc4a855

Please sign in to comment.