Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Integrate getCurrentLineNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Dec 3, 2024
1 parent 2a9b04f commit 6fff033
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 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 {
getCurrentLineNumber(vu)
fmt.Println(getCurrentLineNumber(vu))

return k6ext.Promise(vu.Context(), func() (any, error) {
return bc.Cookies(urls...) //nolint:wrapcheck
Expand Down
28 changes: 18 additions & 10 deletions browser/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package browser
import (
"context"
"errors"
"fmt"
"strconv"
"strings"

"github.com/grafana/sobek"
Expand Down Expand Up @@ -69,17 +69,25 @@ func sobekEmptyString(v sobek.Value) bool {
return !sobekValueExists(v) || strings.TrimSpace(v.String()) == ""
}

func getCurrentLineNumber(vu moduleVU) string {
func getCurrentLineNumber(vu moduleVU) position {
rt := vu.Runtime()
var parent string
// var parent string
var buf [2]sobek.StackFrame
frames := rt.CaptureCallStack(2, buf[:0])
if len(frames) == 0 || frames[1].SrcName() == "file:///-" {
return vu.InitEnv().CWD.JoinPath("./-").String()
}
parent = frames[1].SrcName()
fmt.Println(frames[1].FuncName(), frames[1].Position(), frames[1].SrcName())
fmt.Println(frames[0].FuncName(), frames[0].Position(), frames[0].SrcName())
// if len(frames) == 0 || frames[1].SrcName() == "file:///-" {
// return vu.InitEnv().CWD.JoinPath("./-").String()
// }
return position(frames[1].Position())
}

type position struct {
Filename string // The filename where the error occurred, if any
Line int // The line number, starting at 1
Column int // The column number, starting at 1 (The character count)
}

return parent
func (p position) String() string {
return "Filename: " + p.Filename +
" Line: " + strconv.Itoa(p.Line) +
" Column: " + strconv.Itoa(p.Column)
}
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) {
getCurrentLineNumber(vu)
fmt.Println(getCurrentLineNumber(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 {
getCurrentLineNumber(vu)
fmt.Println(getCurrentLineNumber(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 {
getCurrentLineNumber(vu)
fmt.Println(getCurrentLineNumber(vu))

return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, lo.Type(text, opts) //nolint:wrapcheck
Expand Down
8 changes: 4 additions & 4 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 {
getCurrentLineNumber(vu)
fmt.Println(getCurrentLineNumber(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,7 +135,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop
})
},
"goto": func(url string, opts sobek.Value) (*sobek.Promise, error) {
getCurrentLineNumber(vu)
fmt.Println(getCurrentLineNumber(vu))

gopts := common.NewFrameGotoOptions(
p.Referrer(),
Expand Down Expand Up @@ -357,7 +357,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop
})
},
"waitForNavigation": func(opts sobek.Value) (*sobek.Promise, error) {
getCurrentLineNumber(vu)
fmt.Println(getCurrentLineNumber(vu))

popts := common.NewFrameWaitForNavigationOptions(p.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
Expand All @@ -373,7 +373,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop
}), nil
},
"waitForSelector": func(selector string, opts sobek.Value) *sobek.Promise {
getCurrentLineNumber(vu)
fmt.Println(getCurrentLineNumber(vu))

return k6ext.Promise(vu.Context(), func() (any, error) {
eh, err := p.WaitForSelector(selector, opts)
Expand Down
10 changes: 6 additions & 4 deletions examples/fillform.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default async function() {
await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });

await page.locator('a[href="/my_messages.php"]').click()
await page.waitForSelector('input[name="login"]');
await page.waitForSelector('input[name="login"]')

// Enter login credentials and login
await page.locator('input[name="login"]').type('admin');
Expand All @@ -35,9 +35,11 @@ export default async function() {
// We expect the form submission to trigger a navigation, so to prevent a
// race condition, setup a waiter concurrently while waiting for the click
// to resolve.
await page.locator('input[type="submit"]').click(),

await page.waitForSelector('h2');
await Promise.all([
page.waitForNavigation(), // TODO: Removing Promise.all should work
page.locator('input[type="submit"]').click(),
]);

await check(page.locator('h2'), {
'header': async lo => {
return await lo.textContent() == 'Welcome, admin!'
Expand Down

0 comments on commit 6fff033

Please sign in to comment.