Android自动化测试中,adb reboot后如何确认设备就绪前的事件完成?
adb reboot in Automation Scripts Hey there! Great question—adding adb reboot to start with a clean device state is a smart move for reliable automation. The key here is to build in wait checks at each critical stage to ensure the device is fully ready before proceeding with your wake/unlock and test cases. Let's break this down step by step.
1. Start with the Reboot Command
First, send the reboot signal to the device:
adb reboot
Right after this, the device will disconnect from ADB, so we can't immediately run any other commands. We need to wait for it to come back online.
2. Wait for ADB Connection to Restore
The first check confirms the device is visible to ADB again. Poll the adb devices output until you see the device in the device state (not offline or missing entirely):
echo "Waiting for device to reconnect to ADB..." MAX_WAIT_ADB=300 # 5 minutes timeout wait_time=0 until adb devices | grep -w "device" > /dev/null; do sleep 2 wait_time=$((wait_time + 2)) if [ $wait_time -ge $MAX_WAIT_ADB ]; then echo "ERROR: Timeout waiting for ADB connection!" exit 1 fi done echo "Device reconnected to ADB successfully!"
3. Wait for System Boot to Complete
Even if ADB is connected, the Android system might still be loading apps and services. Check the sys.boot_completed system property—it returns 1 when core system startup finishes:
echo "Waiting for system to finish booting..." MAX_WAIT_BOOT=300 wait_time=0 until adb shell getprop sys.boot_completed | grep -w "1" > /dev/null; do sleep 2 wait_time=$((wait_time + 2)) if [ $wait_time -ge $MAX_WAIT_BOOT ]; then echo "ERROR: Timeout waiting for system boot!" exit 1 fi done echo "System fully booted!"
4. Wake and Unlock the Screen (With Validation)
Your original wake/unlock commands work, but we should verify the screen is responsive before running them. Check if the screen is off, send the wake event, and confirm it's on before swiping:
# Wake the device if it's off echo "Waking up the device..." adb shell input keyevent 26 MAX_WAIT_SCREEN=60 wait_time=0 until adb shell dumpsys power | grep -q "Display Power: state=ON"; do sleep 1 adb shell input keyevent 26 wait_time=$((wait_time + 1)) if [ $wait_time -ge $MAX_WAIT_SCREEN ]; then echo "ERROR: Timeout waiting for screen to wake!" exit 1 fi done echo "Screen is awake!" # Execute your swipe unlock command adb shell input touchscreen swipe 240 480 480 640 100 echo "Screen unlocked to home screen!"
Full Integrated Script Example
Putting it all together into a robust bash script:
#!/bin/bash # Configuration - adjust timeouts as needed MAX_WAIT_ADB=300 MAX_WAIT_BOOT=300 MAX_WAIT_SCREEN=60 # Step 1: Reboot the device echo "Initiating device reboot..." adb reboot # Step 2: Wait for ADB reconnection echo "Waiting for device to reconnect to ADB..." wait_time=0 until adb devices | grep -w "device" > /dev/null; do sleep 2 wait_time=$((wait_time + 2)) if [ $wait_time -ge $MAX_WAIT_ADB ]; then echo "ERROR: Timeout waiting for ADB connection!" exit 1 fi done echo "ADB connection restored!" # Step 3: Wait for system boot completion echo "Waiting for system to finish booting..." wait_time=0 until adb shell getprop sys.boot_completed | grep -w "1" > /dev/null; do sleep 2 wait_time=$((wait_time + 2)) if [ $wait_time -ge $MAX_WAIT_BOOT ]; then echo "ERROR: Timeout waiting for system boot!" exit 1 fi done echo "System fully booted!" # Step 4: Wake and unlock screen echo "Waking up the device..." adb shell input keyevent 26 wait_time=0 until adb shell dumpsys power | grep -q "Display Power: state=ON"; do sleep 1 adb shell input keyevent 26 wait_time=$((wait_time + 1)) if [ $wait_time -ge $MAX_WAIT_SCREEN ]; then echo "ERROR: Timeout waiting for screen to wake!" exit 1 fi done echo "Screen is awake!" adb shell input touchscreen swipe 240 480 480 640 100 echo "Screen unlocked to home screen!" # Proceed with your test cases here echo "Starting automation test cases..." # ... your test logic goes here
Key Notes
- Adjust Timeouts: The timeout values are reasonable defaults, but tweak them based on your device's boot speed.
- Device-Specific Adjustments: Swipe coordinates work for standard devices—modify them if you have a higher-res or different aspect ratio device.
- Alternative Lock Methods: If your device uses PIN/password/fingerprint unlock, update the unlock step (e.g., send keyevents for PIN digits).
内容的提问来源于stack exchange,提问作者Jamil Rahman




