Counting UI Elements in Android UiAutomator with a Custom Java Method
The article describes a problem encountered while learning Android UiAutomator—determining the number of assignment items on a screen—and presents a Java solution that iterates over UI elements using a custom getCountByResourceId() method, similar to Selenium's findElements(), with a code example illustrating the approach.
When learning Android UiAutomator, the author needed to count the number of assignment items displayed on a screen; the built‑in getChildCount() only counts rows, so they created a custom method inspired by Selenium's findElements() that iterates through UI elements.
1 //获取页面某种控件的个数
2 public int getCountByResourceId() {
3 int num = 0;
4 for(int i=0;i<100;i++){
5 try {
6 getUiObjectByResourceIdIntance("com.gaotu100.superclass:id/assignmentitemview_upload_text", i).getText();
7 } catch (UiObjectNotFoundException e) {
8 // e.printStackTrace();
9 num = i;
10 break;
11 }
12 }
13 return num;
14 }The post also provides a list of links to previous articles covering topics such as Java one‑line heart printing, Linux performance monitoring, test coverage with JaCoCo, performance testing frameworks, HTTP mind maps, programming mindset, JVM command mind maps, JSON formatting, probabilistic API testing, double‑spending bug sharing, Swagger automation, and high‑concurrency configuration examples.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
