From 4cf3ca0d66d287e4076dee07d446cab87878ec65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 3 Dec 2024 10:41:25 -0500 Subject: [PATCH] Add detect breakpoint --- browser/module.go | 16 +++++++++++++--- browser/modulevu.go | 1 + browser/page_mapping.go | 3 ++- browser/registry.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/browser/module.go b/browser/module.go index 57bc62c34..0ede80bc0 100644 --- a/browser/module.go +++ b/browser/module.go @@ -98,6 +98,15 @@ func (m *RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance { mapper = syncMapBrowserToSobek } + breakpoints := &breakpointRegistry{ + breakpoints: []breakpoint{ + { + File: "file:///Users/inanc/grafana/k6browser/main/examples/fillform.js", + Line: 26, + }, + }, + } + return &ModuleInstance{ mod: &JSModule{ Browser: mapper(moduleVU{ @@ -110,9 +119,10 @@ func (m *RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance { m.PidRegistry, m.tracesMetadata, ), - taskQueueRegistry: newTaskQueueRegistry(vu), - filePersister: m.filePersister, - testRunID: m.testRunID, + taskQueueRegistry: newTaskQueueRegistry(vu), + breakpointRegistry: breakpoints, + filePersister: m.filePersister, + testRunID: m.testRunID, }), Devices: common.GetDevices(), NetworkProfiles: common.GetNetworkProfiles(), diff --git a/browser/modulevu.go b/browser/modulevu.go index ea6597985..6dc4334be 100644 --- a/browser/modulevu.go +++ b/browser/modulevu.go @@ -18,6 +18,7 @@ type moduleVU struct { *pidRegistry *browserRegistry + *breakpointRegistry *taskQueueRegistry diff --git a/browser/page_mapping.go b/browser/page_mapping.go index 1521acb5a..17d2a7ecc 100644 --- a/browser/page_mapping.go +++ b/browser/page_mapping.go @@ -135,7 +135,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }) }, "goto": func(url string, opts sobek.Value) (*sobek.Promise, error) { - fmt.Println(getCurrentLineNumber(vu)) + pos := getCurrentLineNumber(vu) + fmt.Println(pos, vu.breakpointRegistry.matches(pos)) gopts := common.NewFrameGotoOptions( p.Referrer(), diff --git a/browser/registry.go b/browser/registry.go index 0872a61db..741f4d861 100644 --- a/browser/registry.go +++ b/browser/registry.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "math/big" + "slices" "strconv" "strings" "sync" @@ -33,6 +34,34 @@ import ( var errBrowserNotFoundInRegistry = errors.New("browser not found in registry. " + "make sure to set browser type option in scenario definition in order to use the browser module") +type breakpoint struct { + File string `json:"file"` + Line int `json:"line"` + // Condition string `json:"condition,omitempty"` +} + +type breakpointRegistry struct { + muBreakpoints sync.RWMutex + breakpoints []breakpoint +} + +func (b *breakpointRegistry) add(bp breakpoint) { + b.muBreakpoints.Lock() + defer b.muBreakpoints.Unlock() + + b.breakpoints = append(b.breakpoints, bp) +} + +func (b *breakpointRegistry) matches(p position) bool { + b.muBreakpoints.RLock() + defer b.muBreakpoints.RUnlock() + + return slices.Contains(b.breakpoints, breakpoint{ + File: p.Filename, + Line: p.Line, + }) +} + // pidRegistry keeps track of the launched browser process IDs. type pidRegistry struct { mu sync.RWMutex