DEV Community

Cover image for Filling Out Forms with Playwright: Choosing Between `fill()` and `pressSequentially()`
Nočnica Mellifera
Nočnica Mellifera

Posted on

Filling Out Forms with Playwright: Choosing Between `fill()` and `pressSequentially()`

Filling out forms is one of the most common tasks in web automation and testing. When using Playwright, you have two primary methods for entering text into input fields: .fill() and .pressSequentially(). While both accomplish the same basic goal, choosing the right method can significantly impact the stability, realism, and effectiveness of your tests.

The Default Choice: locator.fill()

For most scenarios, locator.fill() should be your go-to method. It's fast, efficient, and simulates what happens when a user pastes text into a field.

When to use fill():

  • Simple text inputs without dynamic behavior
  • When you need to quickly populate form fields
  • For basic form submissions where typing simulation isn't critical
// Simple and efficient for most cases
await page.locator('#username').fill('testuser');
await page.locator('#email').fill('user@example.com');
Enter fullscreen mode Exit fullscreen mode

When to Switch to pressSequentially()

The community discussion reveals several specific scenarios where pressSequentially() becomes necessary:

1. Testing Auto-complete and Search Suggestions

If you're searching for "Doug Bowser" and want to test the intermediate suggestions that appear as you type "D", "Do", "Dou", etc., fill() won't trigger these incremental updates.

// Triggers search suggestions at each keystroke
await page.locator('#search').pressSequentially('Doug Bowser', { delay: 100 });
Enter fullscreen mode Exit fullscreen mode

2. Triggering Field Validations

Some forms validate input as users type. If you need to test this real-time validation behavior, pressSequentially() allows you to simulate the actual typing process that triggers these validations.

3. Making Automation Less Detectable

When scraping or performing long-running automation tasks, instantaneous text entry can be a telltale sign of bot activity. pressSequentially() with appropriate delays can help mimic human typing patterns.

// More human-like typing behavior
await page.locator('#message').pressSequentially('Hello, this is a longer message', {
  delay: 50 // milliseconds between keystrokes
});
Enter fullscreen mode Exit fullscreen mode

You could even randomize these delay times for multiple form fills.

4. Testing Learning Applications

For applications that grade typing velocity and accuracy, or any scenario where the timing and sequence of keystrokes matter, pressSequentially() is clearly the right choice.

Practical Recommendations

Based on the community consensus:

  1. Start with fill() - It's faster and sufficient for most cases
  2. Switch to pressSequentially() when you need:

    • To trigger intermediate API calls or UI updates
    • To test real-time validation
    • More realistic user behavior simulation
    • To avoid detection in scraping scenarios
  3. Use the delay option to control typing speed when realism matters:

await page.locator('#input').pressSequentially('text', { delay: 100 });
Enter fullscreen mode Exit fullscreen mode

Conclusion

While fill() will handle most of your form-filling needs efficiently, pressSequentially() is your tool for scenarios requiring realistic typing behavior. The key is understanding your application's behavior and choosing the method that best matches how real users interact with your forms. Whether you're testing search suggestions, form validations, or simply trying to make your automation more human-like, having both methods in your Playwright toolkit ensures you can handle any form-filling scenario effectively.

Top comments (0)