Handling Auto-Complete Fields in Playwright
Auto-complete fields are commonly used in modern web applications to provide dynamic suggestions as users type. While they enhance user experience, they can pose challenges for automation testing tools like Playwright. Unlike static input fields, auto-complete fields rely on asynchronous data fetching and DOM updates, making automation slightly tricky. In this blog, we’ll explore how to handle auto-complete fields in Playwright effectively.
Understanding the Auto-Complete Mechanism
Auto-complete fields usually work by detecting user input and fetching matching results via an API. These results are then rendered in a dropdown list below the input box. Selecting an item involves either clicking on the dropdown item or using keyboard navigation (e.g., arrow keys and enter).
Since the results appear dynamically, automation scripts need to:
- Wait for the results to appear.
- Interact with the correct suggestion.
- Ensure that the selection reflects in the input field.
Let’s break down the process of handling auto-complete fields using Playwright.
Step-by-Step Example
Assume we have an input field with the placeholder "Search location". As the user types "New", the dropdown suggests locations like "New York", "New Delhi", etc.
1. Type into the input field
javascript
await page.fill('input[placeholder="Search location"]', 'New');
You can also use type() instead of fill() if you want to simulate realistic typing delays.
javascript
await page.type('input[placeholder="Search location"]', 'New', { delay: 100 });
2. Wait for suggestions to appear
Wait for the dropdown list to become visible. Usually, this involves waiting for a specific selector that represents the list items.
javascript
await page.waitForSelector('.suggestion-item');
Here, .suggestion-item is a sample class that could be used for suggestion elements.
3. Select a suggestion
There are multiple ways to do this. One common approach is to click on the first matching item:
javascript
await page.click('.suggestion-item >> text=New York');
Alternatively, you can use keyboard navigation if the component supports it:
javascript
await page.keyboard.press('ArrowDown');
await page.keyboard.press('Enter');
This simulates user behavior more closely.
Tips and Best Practices
Use waitForSelector Smartly
Avoid hardcoded delays like waitForTimeout. Instead, use waitForSelector or expect from Playwright Test to wait for UI changes.
javascript
await expect(page.locator('.suggestion-item')).toBeVisible();
Handle Dynamic Dropdowns
Some dropdowns disappear if you click elsewhere or don’t interact in time. Make sure your script interacts quickly after results appear.
Handle Multiple Matches
If there are multiple suggestions and you want to select a specific one, use Playwright’s locator() to target the desired item:
javascript
await page.locator('.suggestion-item', { hasText: 'New Delhi' }).click();
Conclusion
Auto-complete fields are a staple in modern UI, and automating them in Playwright requires a good understanding of how the UI renders suggestions dynamically. By typing input, waiting for suggestions, and choosing the right item either via click or keyboard, you can effectively handle such elements in your tests.
Playwright’s robust API makes it easy to write clean, readable, and reliable scripts for such dynamic behaviors. Mastering these interactions will help you create more accurate and user-like automated tests for real-world web applications.
Learn Playwright Testing Training
Read More: Testing Real-Time Notifications Using Playwright
Visit IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment