Handling Auto-Suggestions in Mobile Application Testing - Notes By ShariqSP
Handling Auto-Suggestions in Mobile Application Testing
Auto-suggestions are a common feature in mobile applications, often seen in search bars or input fields. Testing and interacting with auto-suggestions programmatically requires precise handling to ensure the expected suggestion is selected or validated. Below is a detailed guide for handling auto-suggestions using UIAutomator2.
1. Locate the Input Field
Identify the input field where auto-suggestions are displayed. Use attributes like resourceId, text, or className to locate the element.
// Locate the input field
UiObject inputField = new UiSelector().resourceId("com.example.app:id/search_input").makeUiObject();
2. Input Search Text
Enter the text into the input field to trigger the auto-suggestions. Use the setText() method to populate the field.
// Input text to trigger suggestions
inputField.setText("keyword");
3. Wait for Suggestions
Auto-suggestions may take some time to appear, especially if they are dynamically fetched from a server. Use the waitForExists() method to wait for the suggestion list to appear.
// Wait for the suggestions to appear
UiObject suggestionList = new UiSelector().resourceId("com.example.app:id/suggestions_list").makeUiObject();
if (!suggestionList.waitForExists(5000)) {
throw new AssertionError("Suggestions did not appear");
}
4. Select a Specific Suggestion
Locate and select a specific suggestion from the list. Use attributes like text or index to identify the desired suggestion.
// Select a specific suggestion by text
UiObject suggestion = new UiSelector().text("Expected Suggestion").makeUiObject();
suggestion.click();
5. Validate the Selected Suggestion
Verify that the correct suggestion has been selected and populated into the input field or used as the search term.
// Validate the selected suggestion
String selectedText = inputField.getText();
assert selectedText.equals("Expected Suggestion") : "Selected suggestion is incorrect";
6. Handling Dynamic Suggestions
For suggestions that are dynamically updated, ensure the input triggers the suggestions correctly. Use appropriate waits and re-check the suggestion list if needed.
// Handle dynamic suggestions
inputField.setText("dynamic keyword");
suggestionList.waitForExists(5000);
UiObject dynamicSuggestion = new UiSelector().textContains("Dynamic Result").makeUiObject();
dynamicSuggestion.click();
7. Common Issues and Solutions
- Suggestions Not Appearing: Check if the input triggers the suggestions and verify the server response if applicable.
- Incorrect Selection: Validate the attributes (e.g., text or index) used to locate the suggestion.
- UI Lag: Use adequate waits to account for delays in rendering suggestions.
Example Code
// Full example for handling auto-suggestions
UiObject inputField = new UiSelector().resourceId("com.example.app:id/search_input").makeUiObject();
inputField.setText("search term");
UiObject suggestionList = new UiSelector().resourceId("com.example.app:id/suggestions_list").makeUiObject();
if (!suggestionList.waitForExists(5000)) {
throw new AssertionError("Suggestions did not appear");
}
UiObject suggestion = new UiSelector().text("Expected Suggestion").makeUiObject();
suggestion.click();
String selectedText = inputField.getText();
assert selectedText.equals("Expected Suggestion") : "Selected suggestion is incorrect";
8. Best Practices
- Use precise locators for suggestions (e.g., text or partial text).
- Always validate the input field after selecting a suggestion.
- Handle edge cases, such as no suggestions or multiple similar suggestions.