UI Testing Using ADB Commands Notes By ShariqSP
UI Testing Using ADB Commands
1. Setting Up ADB for UI Testing
ADB can automate UI interactions like tapping, swiping, and typing, making it a powerful tool for UI testing. Follow these steps to set up:
- Ensure ADB is installed and configured correctly on your computer.
- Connect your device and verify the connection:
adb devices - Enable USB Debugging on your device:
- Navigate to Settings > Developer Options and enable USB Debugging.
2. Automating UI Interactions
Use ADB commands to simulate user interactions like taps, swipes, and input:
- Tap on Screen:
adb shell input tap x y- Replace
xandywith the screen coordinates of the desired location. - Example: Tap on the center of the screen:
adb shell input tap 500 1000
- Replace
- Swipe on Screen:
- Swipe Up:
adb shell input swipe 500 1500 500 500 - Swipe Down:
adb shell input swipe 500 500 500 1500 - Swipe Left:
adb shell input swipe 900 1000 100 1000 - Swipe Right:
adb shell input swipe 100 1000 900 1000
- Swipe Up:
- Type Text:
adb shell input text 'SampleText'- This types "SampleText" in the currently focused input field.
- Simulate Button Press:
- Back Button:
adb shell input keyevent 4 - Home Button:
adb shell input keyevent 3 - Power Button:
adb shell input keyevent 26
- Back Button:
- Two-Finger Tap:
adb shell input touchscreen swipe 300 300 600 300 50- This simulates a two-finger tap by swiping two points simultaneously.
- Three-Finger Gesture:
adb shell input touchscreen swipe 300 300 300 600 50 && adb shell input touchscreen swipe 400 300 400 600 50 && adb shell input touchscreen swipe 500 300 500 600 50- This simulates a three-finger swipe down gesture.
3. Verifying UI Elements
Use ADB to extract and verify UI element details:
- Dump UI Hierarchy:
adb shell uiautomator dump- This saves the UI hierarchy to a file. Use the following command to pull it:
adb pull /sdcard/window_dump.xml
- This saves the UI hierarchy to a file. Use the following command to pull it:
- Locate UI Elements:
adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'- This identifies the currently focused UI element or app.
4. Testing App Navigation
Automate navigation scenarios to validate app workflows:
- Open an App:
adb shell monkey -p com.example.app 1- Replace
com.example.appwith your app's package name.
- Replace
- Perform Sequential Navigation:
- Tap on a button:
adb shell input tap 300 400 - Swipe to navigate:
adb shell input swipe 1000 1000 100 1000 - Return to the previous screen:
adb shell input keyevent 4
- Tap on a button:
5. Practical UI Testing Scenarios
Use ADB commands to automate and validate various UI scenarios:
- Form Validation: Automate text input and button taps to test form behavior.
- Gesture Navigation: Use swipe and tap commands to test navigation between screens.
- App Launch Testing: Open and close the app repeatedly to ensure stability.
- Multitasking: Simulate switching between apps using the Home and Recents commands:
adb shell input keyevent 187
By leveraging ADB commands for UI testing, you can automate repetitive tasks and thoroughly validate user interface behavior in your app.