Frontend Development 2 min read

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.

FunTester
FunTester
FunTester
Counting UI Elements in Android UiAutomator with a Custom Java Method

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.

JavaAndroidautomationCode ExampleUI testingUiAutomator
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.