Skip to content

Commit 20e9642

Browse files
committed
Refactor locator.pressSequentially
This method is no different than locator.Type, which was deprecated by Playwright.
1 parent cccd171 commit 20e9642

File tree

3 files changed

+9
-47
lines changed

3 files changed

+9
-47
lines changed

internal/js/modules/k6/browser/browser/locator_mapping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {
357357
},
358358

359359
"pressSequentially": func(text string, opts sobek.Value) (*sobek.Promise, error) {
360-
copts := common.NewFramePressOptions(lo.Timeout())
360+
copts := common.NewFrameTypeOptions(lo.Timeout())
361361
if err := copts.Parse(vu.Context(), opts); err != nil {
362362
return nil, fmt.Errorf("parsing locator press sequentially options: %w", err)
363363
}

internal/js/modules/k6/browser/common/locator.go

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -534,46 +534,11 @@ func (l *Locator) Press(key string, opts *FramePressOptions) error {
534534

535535
// PressSequentially focuses on the element and sequentially sends a keydown,
536536
// keypress, and keyup events for each character in the provided string.
537-
// For handling special keys, use the Locator.Press method.
538-
func (l *Locator) PressSequentially(text string, opts *FramePressOptions) error {
539-
l.log.Debugf(
540-
"Locator:PressSequentially", "fid:%s furl:%q sel:%q text:%q opts:%+v",
541-
l.frame.ID(), l.frame.URL(), l.selector, text, opts,
542-
)
543-
_, span := TraceAPICall(l.ctx, l.frame.page.targetID.String(), "locator.pressSequentially")
544-
defer span.End()
545-
546-
opts.Strict = true
547-
548-
focusOpts := NewFrameBaseOptions(l.frame.defaultTimeout())
549-
if opts.Timeout != 0 {
550-
focusOpts.Timeout = opts.Timeout
551-
}
552-
553-
if err := l.Focus(focusOpts); err != nil {
554-
err := fmt.Errorf("focusing before typing: %w", err)
555-
spanRecordError(span, err)
556-
return err
557-
}
558-
559-
for i, char := range text {
560-
select {
561-
case <-l.ctx.Done():
562-
err := fmt.Errorf("cancelled at char %d/%d: %w", i+1, len(text), l.ctx.Err())
563-
spanRecordError(span, err)
564-
return err
565-
default:
566-
}
567-
568-
if err := l.frame.press(l.selector, string(char), opts); err != nil {
569-
err := fmt.Errorf("pressing %q at %d/%d: %w", char, i+1, len(text), err)
570-
spanRecordError(span, err)
571-
return err
572-
}
573-
574-
applySlowMo(l.ctx)
537+
// For handling special keys, use the [Locator.Press] method.
538+
func (l *Locator) PressSequentially(text string, opts *FrameTypeOptions) error {
539+
if err := l.Type(text, opts); err != nil {
540+
return fmt.Errorf("pressing sequentially %q on %q: %w", text, l.selector, err)
575541
}
576-
time.Sleep(200 * time.Millisecond)
577542
return nil
578543
}
579544

internal/js/modules/k6/browser/tests/locator_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@ func TestLocator(t *testing.T) {
298298
lo := p.Locator("#inputText", nil)
299299
require.NoError(t, lo.Clear(common.NewFrameFillOptions(lo.Timeout())))
300300

301-
opts := common.NewFramePressOptions(lo.Timeout())
302-
require.NoError(t, lo.PressSequentially("hello", opts))
301+
require.NoError(t, lo.PressSequentially("hello", common.NewFrameTypeOptions(lo.Timeout())))
303302

304303
value, err := p.InputValue("#inputText", common.NewFrameInputValueOptions(p.MainFrame().Timeout()))
305304
require.NoError(t, err)
@@ -311,7 +310,7 @@ func TestLocator(t *testing.T) {
311310
lo := p.Locator("#inputText", nil)
312311
require.NoError(t, lo.Clear(common.NewFrameFillOptions(lo.Timeout())))
313312

314-
opts := common.NewFramePressOptions(lo.Timeout())
313+
opts := common.NewFrameTypeOptions(lo.Timeout())
315314
opts.Delay = 100
316315

317316
require.NoError(t, lo.PressSequentially("text", opts))
@@ -326,8 +325,7 @@ func TestLocator(t *testing.T) {
326325
lo := p.Locator("textarea", nil)
327326
require.NoError(t, lo.Clear(common.NewFrameFillOptions(lo.Timeout())))
328327

329-
opts := common.NewFramePressOptions(lo.Timeout())
330-
require.NoError(t, lo.PressSequentially("some text", opts))
328+
require.NoError(t, lo.PressSequentially("some text", common.NewFrameTypeOptions(lo.Timeout())))
331329

332330
value, err := lo.InputValue(common.NewFrameInputValueOptions(lo.Timeout()))
333331
require.NoError(t, err)
@@ -511,8 +509,7 @@ func TestLocator(t *testing.T) {
511509
},
512510
{
513511
"PressSequentially", func(l *common.Locator, tb *testBrowser) error {
514-
opts := common.NewFramePressOptions(100 * time.Millisecond)
515-
return l.PressSequentially("text", opts)
512+
return l.PressSequentially("text", common.NewFrameTypeOptions(100*time.Millisecond))
516513
},
517514
},
518515
{

0 commit comments

Comments
 (0)