API: Execute ADB Commands from an Espresso Test
The ADB Commands API allows you to execute Android Debug Bridge (ADB) shell commands remotely from within your Espresso tests. This API provides direct access to the device's shell environment, enabling advanced testing scenarios that require system-level interactions.
Key Benefits:
- Direct Device Control: Execute shell commands on test devices
- Advanced Testing: Perform system-level operations during tests
- Flexible Integration: Works seamlessly with Espresso test framework
- Remote Execution: Execute commands without physical device access
Common Use Cases:
- File System Operations: List, create, or modify files
- App State Management: Clear app data or cache
- System Settings: Modify device settings for testing
- Network Operations: Configure network conditions
API Definition
Method: POST
URL: http://<RobusTest URL>/v3/device/shell?accesskey=<user_access_key>
Request Headers:
Content-Type: application/json
Payload:
{
"_id": "<device_id>",
"command": "<ADB SHELL COMMAND>"
}
Success Response:
{
"status": true,
"msg": "command execution result"
}
Error Response:
{
"status": false,
"msg": "error message"
}
Authentication:
Your access key is required as a query parameter in the URL. You can obtain your access key from:
- User Profile: Navigate to User Profile → Access Keys
- Project Settings: Found in Project Dashboard → Settings
Keep your access key secure and never commit it to version control.
Parameters Reference
| Parameter | Type | Description | Example | Required |
|---|---|---|---|---|
| _id | String | Device ID of the target device for command execution | "2132SDSFDSFDSF" |
Yes |
| command | String | ADB shell command to execute on the device | "ls /data/local/tmp/" |
Yes |
| accesskey | String | User access key for authentication (URL parameter) | "1234DFFGG24FDSD" |
Yes |
Getting the Device ID
The device ID is injected as a test argument by RobusTest. Retrieve it in your test code as follows:
Java:
Bundle testBundle = InstrumentationRegistry.getArguments();
String deviceID = testBundle.getString("deviceID");
// Use the deviceID in your API call
System.out.println("Current Device ID: " + deviceID);
Kotlin:
val testBundle = InstrumentationRegistry.getArguments()
val deviceID = testBundle.getString("deviceID")
// Use the deviceID in your API call
println("Current Device ID: $deviceID")
Example
URL: http://devicelab.robustest.com/v3/device/shell?accesskey=1234DFFGG24FDSD
Payload:
{
"_id": "2132SDSFDSFDSF",
"command": "ls /data/local/tmp/"
}
Response:
{
"status": true,
"msg": "file1.txt\nfile2.txt\ntest_data/"
}
cURL:
curl -X POST \
'http://devicelab.robustest.com/v3/device/shell?accesskey=1234DFFGG24FDSD' \
-H 'Content-Type: application/json' \
-d '{
"_id": "2132SDSFDSFDSF",
"command": "ls /data/local/tmp/"
}'
Advanced Examples:
Clear app data:
{
"_id": "2132SDSFDSFDSF",
"command": "pm clear com.example.app"
}
Install APK:
{
"_id": "2132SDSFDSFDSF",
"command": "pm install /data/local/tmp/test.apk"
}
Check battery level:
{
"_id": "2132SDSFDSFDSF",
"command": "dumpsys battery | grep level"
}
Common ADB Commands
File Operations:
- List Directory:
ls /path/to/directory - Create Directory:
mkdir /path/to/new/directory - Copy File:
cp /source/file /dest/file - Remove File:
rm /path/to/file - Change Permissions:
chmod 755 /path/to/file
System Operations:
- Clear App Data:
pm clear com.package.name - Install APK:
pm install /path/to/app.apk - Check Processes:
ps | grep process_name - Memory Info:
dumpsys meminfo - Battery Status:
dumpsys battery
Best Practices
Security Considerations:
- Only execute trusted commands
- Validate command parameters to prevent injection attacks
- Use appropriate error handling for failed commands
- Monitor command execution for security anomalies
Performance Tips:
- Keep commands lightweight to avoid test timeouts
- Use specific paths rather than wildcards when possible
- Cache device IDs to avoid repeated retrievals
- Implement retry logic for transient failures
Error Handling
| Error Type | Description | Solution |
|---|---|---|
| Invalid Device ID | Device ID not found or inactive | Verify device availability and correct ID |
| Command Timeout | Command execution exceeded timeout | Use simpler commands or increase timeout |
| Permission Denied | Insufficient permissions for command | Use appropriate permissions or alternative commands |
| Authentication Error | Invalid access key | Verify access key validity and permissions |
Related Documentation
- Espresso Hub - Espresso Hub Integration
- Switch Wi-Fi API - WiFi Network Switching API
- Job Payload API - Job Payload API Reference
- Automation Reports - Understanding Test Reports
