Skip to content

Conversation

rajan2345
Copy link

What?

Implements locator.pressSequentially() to type text character by character, simulating real keyboard input. This is useful for testing features like autocomplete, input validation, and character counters that require gradual typing.

Why?

This feature enables k6 users to test scenarios that require real keyboard events for each character, which locator.fill() doesn't provide. It's particularly useful when migrating from Playwright to k6, as this is a commonly used Playwright API that k6 currently lacks.

Fixes #5038

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • I have run linter and tests locally (make check) and all pass.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

  • I have added the correct milestone and labels to the PR.
  • I have updated the release notes: link
  • I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
  • I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable

Implementation Details

  • Reuses existing FramePressOptions (which already has the delay field from Playwright)
  • Iterates through text as runes to properly handle multi-byte Unicode characters
  • Calls existing frame.press() for each character to fire proper keyboard events
  • Applies delay BETWEEN characters (not before first or after last)
  • Follows the same pattern as other locator methods (public + private implementation)

Test Coverage

  • ✅ Basic functionality: types text correctly
  • ✅ Delay option: timing verified (300ms for 3 delays between 4 characters)
  • ✅ Works with textarea elements
  • ✅ Timeout handling
  • ✅ Strict mode enforcement

Related PR(s)/Issue(s)

Related to issue #4934 (Reduce friction in migrating Playwright scripts to k6)

Closes #5038


Thank you

@rajan2345 rajan2345 requested a review from a team as a code owner October 13, 2025 05:38
@rajan2345 rajan2345 requested review from inancgumus and joanlopez and removed request for a team October 13, 2025 05:38
@CLAassistant
Copy link

CLAassistant commented Oct 13, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing! Very much appreciated! 🙇

I've left a couple of comments that need to be addressed before we can merge this in.

@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 13, 2025 13:44 — with GitHub Actions Inactive
@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 13, 2025 13:46 — with GitHub Actions Inactive
Copy link
Contributor

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution 🙇 LGTM overall. There are some minor adjustments we need to make. We should fix the tests and reduce duplication (see the linter error).

Copy link
Contributor

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking into considerations the changes we've asked for. I've left a couple more comments that should be addressed, but we're on the home stretch 😄

@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 14, 2025 14:14 — with GitHub Actions Inactive
@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 14, 2025 14:16 — with GitHub Actions Inactive
@rajan2345 rajan2345 requested a review from ankur22 October 15, 2025 07:53
ankur22
ankur22 previously approved these changes Oct 15, 2025
Copy link
Contributor

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

Copy link
Contributor

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall. One more small thing to do.

We should also fix the conflicts with the master branch.

@rajan2345 rajan2345 force-pushed the feature/locator-press-sequentially branch from 897ee46 to 97d4f44 Compare October 15, 2025 16:53
@rajan2345 rajan2345 requested a review from inancgumus October 15, 2025 16:54
@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 16, 2025 11:43 — with GitHub Actions Inactive
@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 16, 2025 11:47 — with GitHub Actions Inactive
@inancgumus
Copy link
Contributor

@rajan2345 There are linter and test fixes we need to make.

You can run these checks locally using make.

@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 17, 2025 12:29 — with GitHub Actions Inactive
@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 17, 2025 12:30 — with GitHub Actions Inactive
@rajan2345
Copy link
Author

Thanks for the feedback, @inancgumus. I updated the branch and resolved the linter/test problems. All set for another look.

@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 20, 2025 08:01 — with GitHub Actions Inactive
@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 20, 2025 08:03 — with GitHub Actions Inactive
Copy link
Contributor

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates. Some minor suggestions.

Comment on lines 323 to 336
{
"PressSequentiallyTextarea", func(_ *testBrowser, p *common.Page) {
lo := p.Locator("textarea", nil)
require.NoError(t, lo.Clear(common.NewFrameFillOptions(lo.Timeout())))

opts := common.NewFramePressOptions(lo.Timeout())
require.NoError(t, lo.PressSequentially("some text", opts))

value, err := p.InputValue("textarea", common.NewFrameInputValueOptions(p.MainFrame().Timeout()))
require.NoError(t, err)
require.Equal(t, "some text", value)
},
},

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we remove this test? Is it no longer required?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bringing this up!

I removed that test because it would sometimes fail, and I thought it was just unreliable, and the testing for word only in place a sentence would work.

I checked the original Playwright implementation again and noticed they specifically test strings with spaces, so I realized it was important to get this right. The failure was occurring because the loop was sending keystrokes much faster than the browser could handle—a classic race condition.

My original design aimed to make it fast, thinking each keypress would fully complete before the next one started.

The fix involved moving the SlowMo pause inside the loop, giving the browser a moment to catch up after each character, and now the function works reliably and maybe the tests will pass. Thanks again!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation ❤️ Did you try restoring this test with your new fixes?

Implements locator.pressSequentially() to type text character by
character, simulating real keyboard input. This is useful for testing
features like autocomplete, input validation, and character counters
that require gradual typing.

Fixes grafana#5038
@rajan2345 rajan2345 force-pushed the feature/locator-press-sequentially branch from c902e86 to c250446 Compare October 21, 2025 10:07
@rajan2345 rajan2345 requested a review from inancgumus October 21, 2025 10:08
@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 21, 2025 15:22 — with GitHub Actions Inactive
@rajan2345 rajan2345 temporarily deployed to azure-trusted-signing October 21, 2025 15:25 — with GitHub Actions Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement locator.pressSequentially

4 participants