如何在UiAutomator中获取ImageView字段文本并验证消息收发结果
Hey there! Let's tackle your two UiAutomator questions— I’ve worked through similar message verification scenarios before, so here’s what I’ve found works reliably:
First off, it’s important to note that ImageView itself doesn’t hold text in the same way a TextView does. Most apps store text associated with an ImageView in the contentDescription attribute— this is primarily for accessibility purposes, but it’s also the go-to for automation tests to retrieve "text" linked to an image.
Here’s how to grab it in code:
// 1. Locate your target ImageView using a unique identifier (resource ID is best) UiObject messageImageView = new UiObject(new UiSelector().resourceId("com.your.app.package:id/message_image_view")); // 2. Retrieve the content description as your text value String imageAssociatedText = messageImageView.getContentDescription();
Edge Case Note:
If the app uses a different method to attach text to the ImageView (like setting a Tag), you can fetch that instead:
String imageTagText = messageImageView.getTag().toString();
But this is less common— contentDescription is the standard approach for image-related text in UI automation.
Once you can pull the text from the ImageView, verifying message send/receive follows a straightforward three-step process:
- Define your expected message: First, store the exact text you’re sending (so you have a baseline to compare against).
- Retrieve the actual message from the receiving end: After sending, locate the corresponding ImageView in the chat/feed and pull its associated text.
- Run an assertion: Compare the expected and actual values to confirm they match.
Here’s a concrete code example using JUnit assertions:
// Step 1: Set your expected message content String expectedMessage = "Test message for verification"; // Step 2: Perform the send action (adapt this to your app's UI) UiObject inputField = new UiObject(new UiSelector().resourceId("com.your.app.package:id/chat_input")); inputField.setText(expectedMessage); UiObject sendButton = new UiObject(new UiSelector().resourceId("com.your.app.package:id/send_btn")); sendButton.click(); // Step 3: Wait for the received message to load (critical to avoid race conditions) UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); device.wait(Until.findObject(By.res("com.your.app.package:id/received_message_image")), 5000); // Step 4: Retrieve the actual message text from the received ImageView UiObject receivedMessageImageView = new UiObject(new UiSelector().resourceId("com.your.app.package:id/received_message_image")); String actualMessage = receivedMessageImageView.getContentDescription(); // Step 5: Verify the match Assert.assertEquals("Sent and received messages do not match!", expectedMessage, actualMessage);
Pro Tips:
- Always add a wait for the target control to appear— using
UiDevice.wait()ensures you don’t try to fetch text before the message has loaded. - If your chat has multiple messages, use more specific selectors (like
textContains()or combining resource ID with index) to target the correct message ImageView. Avoid relying solely on index if possible, as UI order can shift.
内容的提问来源于stack exchange,提问作者Ajay Singh Chouhan

