Verify Android Toggle States in Phoma With Accessibility Checks
Why state matters more than visible text
A switch can look enabled while the accessibility node is still animating. A checkbox can show a checkmark drawn by the app, but the node may not report checked until the app finishes saving the setting. If a flow taps and immediately reads text, it can pass by accident or fail for the wrong reason.
Phoma flows can inspect the Android accessibility tree, so they can check semantic properties such as checked and selected. This is better than comparing a label because labels change with language, theme, or marketing copy. State is what the app actually uses.
Pick the element before reading its state
Start in the mirrored phone view. Open the screen you want to test, then inspect the element tree. Look for a node whose class matches the control.
- For a switch, look for android.widget.Switch.
- For a checkbox, look for android.widget.CheckBox.
- For a radio button, look for android.widget.RadioButton.
- For a selected list row, look for a node with selected=true.
Prefer selectors that do not depend on position. A good selector is usually one of these, in order:
resource-id='com.example.app:id/notifications_switch'
class='android.widget.Switch' AND content-desc='Notifications'
Avoid selectors based only on text, bounds, or child index. Text changes. Bounds change with font size. Indexes change when a list inserts an item.
Inspect first, automate second
Before writing the flow, answer three questions in the element tree.
- Which node represents the control itself, not the label next to it?
- Which property proves its state: checked, selected, or text?
- What selector will still match after a small app update?
For example, a settings screen may have a row with the label Notifications and a separate switch node. If you tap the row, the app may open a subpage. If you tap the switch node, the state changes. The element tree makes that difference visible.
Build a read-assert-adjust pattern
A reliable flow does not just tap and hope. It reads the current state, changes it if needed, waits for the new state, and asserts it.
Example: turn on notifications
Use a flow like this in the visual builder.
Open App: com.example.app
Wait For Element: content-desc='Settings', timeout=10s
Tap: content-desc='Settings'
Wait For Element: resource-id='com.example.app:id/notifications_switch', timeout=10s
Read Element: resource-id='com.example.app:id/notifications_switch'
Property: checked
Store: notifications_checked
If notifications_checked = false
Tap: resource-id='com.example.app:id/notifications_switch'
End If
Wait For Element: resource-id='com.example.app:id/notifications_switch'
Read Element: resource-id='com.example.app:id/notifications_switch'
Property: checked
Store: notifications_checked_after
Assert: notifications_checked_after = true
This pattern is reusable. Change the selector and expected state to test dark mode, analytics consent, or any switch.
Wait for the state to settle
Android can update a control after a network request, database write, or animation. A 300 millisecond pause is not a strategy. Use a short repeat loop instead.
Repeat up to 5 times
Read Element: resource-id='com.example.app:id/notifications_switch'
Property: checked
Store: current_state
If current_state = true
Exit Loop
End If
Wait: 300 ms
End Repeat
Then assert current_state. This keeps the flow fast when the app responds quickly and patient when it does not.
Use conditions for guards, not guesses
Conditions should express product rules. If a switch is already on, do not tap it. If it is off, turn it on and verify.
If notifications_checked = false
Tap: resource-id='com.example.app:id/notifications_switch'
End If
For radio buttons, read selected. For checkboxes, read checked. For list rows, read selected only if the app reports it. Do not treat a highlight drawable as proof.
Make failures explain themselves
When a state assertion fails, take a screenshot and record what the flow expected. A useful failure message says what was read, not only that something went wrong.
Assert: notifications_checked_after = true
On Failure: Save Screenshot
On Failure: Continue With Next Step
If the state property is unreliable on a custom control, use content-desc as a secondary signal, but keep the primary assertion on the property whenever possible. Screenshot checks are a fallback, not the first choice.
Keep the flow portable across phones
Because these steps use element properties instead of coordinates, the same flow can run on several test phones. A selector based on resource-id or content-desc is more likely to work after a screen rotation, font-size change, or device manufacturer skin.
Keep flows visual and readable. Name steps after the intent: Enable notifications, Verify notifications switch is on, Save failure screenshot. When you share the flow with a team using Phoma Team or Business, the meaning stays clear. Phoma flows run on the user's own computer and phones, and using your own phones has no phone or flow limit.
This is the practical difference between tapping where something looks like a switch and checking the Android node that represents it. State-based checks are less brittle, easier to debug, and closer to what the app actually does.
