[{"data":1,"prerenderedAt":20},["ShallowReactive",2],{"blog-post-en-choose-stable-android-element-selectors":3},{"slug":4,"lang":5,"title":6,"summary":7,"date":8,"tags":9,"tag_slugs":15,"thumbnail_url":16,"translations":17,"body":18,"asset_base":19},"choose-stable-android-element-selectors","en","Choose Stable Android Element Selectors for Phoma Flows","Learn how to inspect an Android app's UI tree in Phoma and pick element selectors that keep visual automation flows working after app updates.","2026-11-06",[10,11,12,13,14],"android","phoma","selectors","ui-automation","qa",[10,11,12,13,14],"https://blog-api.phomaapp.com/api/blog/posts/choose-stable-android-element-selectors/thumbnail.svg?lang=en",[5],"## Why Android selectors break after an app update\n\nPhoma lets you build automation flows visually on your Windows PC and run them on Android phones over USB or Wi-Fi. Every step that touches the screen—tap, type, read text, wait for an element—relies on an element selector. When the app you are testing updates, the screen can look identical to a person while the underlying layout changes enough to break your flow.\n\nA button that was the third button on the screen might become the fourth. A resource ID might change from `login_button` to `sign_in_button`. Text that said \"Submit\" might change to \"Continue\" or be translated into another language. If your flow was built around a brittle selector, the step fails and the whole run stops.\n\nThis guide shows how to inspect an Android app's UI tree in Phoma, choose selectors that survive updates, and verify them before you save a flow.\n\n## Prerequisites\n\n- Phoma installed on a Windows PC\n- An Android phone with USB debugging or wireless debugging enabled\n- The phone connected to Phoma and visible in the device list\n- The app under test installed on the phone\n\nIf the phone does not appear in Phoma, check the USB cable, driver installation, and the \"USB debugging\" authorization dialog on the phone before continuing.\n\n## Step 1: Open the element inspector\n\nStart Phoma and select the phone you want to inspect. Open the screen in the app that contains the element you want to act on. Launch Phoma's element inspector from the device toolbar.\n\nThe inspector captures the current UI Automator tree: a hierarchy of every visible element on the screen. Each element exposes attributes such as:\n\n- `resource-id`: the internal ID assigned by the app developer\n- `content-desc`: the accessibility label, often used by screen readers\n- `text`: the visible label on a button or field\n- `class`: the type of Android view, such as `android.widget.Button`\n- `index`: the element's position among siblings of the same class\n\nSelect an element in the inspector and Phoma highlights it on the phone screen. This visual confirmation is your first safety check.\n\n## Step 2: Read the tree before you pick a selector\n\nHere is a simplified example of what the inspector might show for a login screen:\n\n```\n\u003Candroid.widget.FrameLayout>\n  \u003Candroid.widget.LinearLayout>\n    \u003Candroid.widget.EditText\n      resource-id='com.example.app:id/username_input'\n      text=''\n      class='android.widget.EditText'\n      index='0' />\n    \u003Candroid.widget.EditText\n      resource-id='com.example.app:id/password_input'\n      text=''\n      class='android.widget.EditText'\n      index='1' />\n    \u003Candroid.widget.Button\n      resource-id='com.example.app:id/login_button'\n      text='Log in'\n      class='android.widget.Button'\n      index='0' />\n    \u003Candroid.widget.Button\n      resource-id='com.example.app:id/forgot_password_button'\n      text='Forgot password?'\n      class='android.widget.Button'\n      index='1' />\n  \u003C/android.widget.LinearLayout>\n\u003C/android.widget.FrameLayout>\n```\n\nNotice that `resource-id` values are namespaced to the app and tend to stay stable between releases. `text` values are visible to users and change when copy is edited. `index` values change whenever a sibling element is added or removed above the target.\n\n## Step 3: Choose selectors by stability\n\nRank selector attributes from most stable to least stable:\n\n1. **resource-id** — Best choice when the app team owns the ID and does not refactor it.\n2. **content-desc** — Strong choice for accessibility labels that are intentionally stable.\n3. **text** — Acceptable for static labels, but risky if the app changes wording or supports multiple languages.\n4. **class plus index** — Brittle. Avoid unless no other attribute is available.\n5. **position or screen coordinates** — Most brittle. Do not use for flows that must survive updates.\n\nIn Phoma's step editor, configure the selector by attribute and value. For example, to tap the login button:\n\n```\nStep: Tap element\nAttribute: resource-id\nValue: com.example.app:id/login_button\nMatch: equals\n```\n\nThis tells Phoma to find the element by its internal ID regardless of where it appears on the screen or what its visible text says.\n\n## Step 4: Handle dynamic and semi-dynamic attributes\n\nSome apps generate IDs that include timestamps or build hashes, such as `button_1a2b3c4d`. If the suffix changes on every build, an exact match will fail.\n\nFirst, check whether the prefix remains constant. If it does, use a \"starts with\" or \"contains\" match in Phoma's selector field so the step keeps working across builds:\n\n```\nAttribute: resource-id\nValue: com.example.app:id/button_\nMatch: starts with\n```\n\nIf no stable attribute exists, combine two attributes. For example, match `class='android.widget.Button'` and `text='Log in'` together. Phoma will narrow the results to elements that satisfy both conditions.\n\nAvoid chaining too many attributes. A selector that depends on five attributes is harder to maintain than one that depends on one stable attribute.\n\n## Step 5: Verify before you save\n\nNever save a flow step based only on the attribute string. Use Phoma's highlight or preview function to confirm that exactly one element lights up on the phone. If zero elements match, the selector is too narrow. If multiple elements match, the step may act on the wrong one.\n\nRun the step manually once from the editor. Watch the phone screen. If the tap lands on the correct button, the selector is safe to use in a flow.\n\n## Real-world example: a weekly order-status check\n\nA small business runs a weekly flow on five test phones. The flow opens a retail app, taps \"Orders,\" and reads the status of the latest delivery. The original flow used `index='2'` to tap the Orders button because the inspector showed it as the third button on the home screen.\n\nAfter an app update, a new promotional banner added a button at the top of the screen. The Orders button moved to index 3. The flow started tapping the banner instead, and the weekly check failed.\n\nThe fix was to re-inspect the screen, find that the Orders button had `resource-id='com.retail.app:id/nav_orders'`, and rebuild the step using that ID. The flow now runs unchanged even when the home screen layout changes.\n\n## Do and don't\n\nDo:\n- Prefer `resource-id` and `content-desc` over `text` and `index`.\n- Re-inspect screens after every app update.\n- Verify each selector with Phoma's highlight before saving.\n- Document which attribute each flow step uses so teammates know what to check if a step breaks.\n\nDon't:\n- Use screen coordinates or index positions for production flows.\n- Build selectors around visible text that marketing can change.\n- Select an element just because it worked once; confirm it is unique on the screen.\n\n## Sharing stable flows with your team\n\nIf you are on a Phoma Team or Business plan, save the verified flow to a shared phone pool. Teammates can run the same flow on their devices without rebuilding selectors. Because everything runs on your own computers and phones, there are no per-phone limits on the free plan, and sharing controls are handled in the Team and Business plans.\n\nStable selectors turn a flow that breaks every week into a flow that runs reliably until the app's underlying IDs actually change. Invest ten minutes in the inspector now, and you will save hours of debugging later.\n","https://blog-api.phomaapp.com/api/blog/posts/choose-stable-android-element-selectors/assets",1793941157089]