Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/js/modules/k6/browser/browser/locator_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,17 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {
return nil, lo.Press(key, copts) //nolint:wrapcheck
}), nil
},

"pressSequentially": func(text string, opts sobek.Value) (*sobek.Promise, error) {
copts := common.NewFrameTypeOptions(lo.Timeout())
if err := copts.Parse(vu.Context(), opts); err != nil {
return nil, fmt.Errorf("parsing locator press sequentially options: %w", err)
}
return promise(vu, func() (any, error) {
return nil, lo.PressSequentially(text, copts) //nolint:wrapcheck
}), nil
},

"type": func(text string, opts sobek.Value) (*sobek.Promise, error) {
copts := common.NewFrameTypeOptions(lo.Timeout())
if err := copts.Parse(vu.Context(), opts); err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/js/modules/k6/browser/browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ type locatorAPI interface { //nolint:interfacebloat
Nth(nth int) *common.Locator
SelectOption(values sobek.Value, opts sobek.Value) ([]string, error)
Press(key string, opts sobek.Value) error
PressSequentially(text string, opts sobek.Value) error
Type(text string, opts sobek.Value) error
Hover(opts sobek.Value) error
Tap(opts sobek.Value) error
Expand Down
23 changes: 23 additions & 0 deletions internal/js/modules/k6/browser/common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,29 @@ func (l *Locator) Press(key string, opts *FramePressOptions) error {
return nil
}

// PressSequentially focuses on the element and sequentially sends a keydown,
// keypress, and keyup events for each character in the provided string.
// For handling special keys, use the [Locator.Press] method.
func (l *Locator) PressSequentially(text string, opts *FrameTypeOptions) error {
l.log.Debugf(
"Locator:PressSequentially", "fid:%s furl:%q sel:%q text:%q opts:%+v",
l.frame.ID(), l.frame.URL(), l.selector, text, opts,
)
_, span := TraceAPICall(l.ctx, l.frame.page.targetID.String(), "locator.pressSequentially")
defer span.End()

opts.Strict = true
if err := l.frame.typ(l.selector, text, opts); err != nil {
err := fmt.Errorf("pressing sequentially %q on %q: %w", text, l.selector, err)
spanRecordError(span, err)
return err
}

applySlowMo(l.ctx)

return nil
}

// Type text on the element found that matches the locator's
// selector with strict mode on.
func (l *Locator) Type(text string, opts *FrameTypeOptions) error {
Expand Down
45 changes: 45 additions & 0 deletions internal/js/modules/k6/browser/tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,46 @@ func TestLocator(t *testing.T) {
require.Equal(t, "xsomething", inputValue)
},
},

{
"PressSequentially", func(_ *testBrowser, p *common.Page) {
lo := p.Locator("#inputText", nil)
require.NoError(t, lo.Clear(common.NewFrameFillOptions(lo.Timeout())))

require.NoError(t, lo.PressSequentially("hello", common.NewFrameTypeOptions(lo.Timeout())))

value, err := p.InputValue("#inputText", common.NewFrameInputValueOptions(p.MainFrame().Timeout()))
require.NoError(t, err)
require.Equal(t, "hello", value)
},
},
{
"PressSequentiallyWithDelayOption", func(_ *testBrowser, p *common.Page) {
lo := p.Locator("#inputText", nil)
require.NoError(t, lo.Clear(common.NewFrameFillOptions(lo.Timeout())))

opts := common.NewFrameTypeOptions(lo.Timeout())
opts.Delay = 100

require.NoError(t, lo.PressSequentially("text", opts))

value, err := p.InputValue("#inputText", common.NewFrameInputValueOptions(p.MainFrame().Timeout()))
require.NoError(t, err)
require.Equal(t, "text", value)
},
},
{
"PressSequentiallyTextarea", func(_ *testBrowser, p *common.Page) {
lo := p.Locator("textarea", nil)
require.NoError(t, lo.Clear(common.NewFrameFillOptions(lo.Timeout())))

require.NoError(t, lo.PressSequentially("some text", common.NewFrameTypeOptions(lo.Timeout())))

value, err := lo.InputValue(common.NewFrameInputValueOptions(lo.Timeout()))
require.NoError(t, err)
require.Equal(t, "some text", value)
},
},
{
"SelectOption", func(tb *testBrowser, p *common.Page) {
l := p.Locator("#selectElement", nil)
Expand Down Expand Up @@ -467,6 +507,11 @@ func TestLocator(t *testing.T) {
return l.Press("a", common.NewFramePressOptions(100*time.Millisecond))
},
},
{
"PressSequentially", func(l *common.Locator, tb *testBrowser) error {
return l.PressSequentially("text", common.NewFrameTypeOptions(100*time.Millisecond))
},
},
{
"SetChecked", func(l *common.Locator, tb *testBrowser) error {
return l.SetChecked(true, common.NewFrameCheckOptions(100*time.Millisecond))
Expand Down
Loading