UI Automator использовал такую штуку для автоматизации некоторых действий в чужих приложениях (Требуется зарученый аппарат).
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class AutomationTester {
private UiDevice mDevice;
@Before
public void before() {
mDevice = UiDevice.getInstance(getInstrumentation());
assertThat(mDevice, notNullValue());
}
@org.junit.Test
public void test() throws Exception
{
mDevice.pressHome();
Thread.sleep(1000);
openApp("com.viber.voip");
UiObject2 menu = waitForObject(By.res("com.viber.voip:id/fab_new_contact"));
menu.click();
UiObject2 phoneField = waitForObject(By.res("com.viber.voip:id/phone_number"));
phoneField.setText(phone);
UiObject2 nextBtn = mDevice.findObject(By.res("com.viber.voip:id/continue_btn"));
nextBtn.click();
// etc...
}
private void openApp(String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
}
private UiObject2 waitForObject(BySelector selector, int timeout) throws Exception {
UiObject2 object = null;
int delay = 1000;
long time = System.currentTimeMillis();
while (object == null) {
object = mDevice.findObject(selector);
Thread.sleep(delay);
if (System.currentTimeMillis() - timeout > time) {
throw new InterruptedException("Faild waiting object");
}
}
return object;
}
private UiObject2 waitForObject(BySelector selector) throws Exception {
return waitForObject(selector, 30000);
}
}
Установка
#!/bin/bash
app_package="com.jackkum.tester"
ADB="adb -s deviceId"
ADB_SH="$ADB shell"
path_sysapp="/system/app"
path_tmpapp="/data/local/tmp"
apk_host="./tester/build/outputs/apk/tester-debug-androidTest.apk"
./gradlew :tester:assembleDebug :tester:assembleDebugAndroidTest || exit -1
# Install APK: using adb root
$ADB push $apk_host $path_tmpapp/com.jackkum.tester.test || exit -1
$ADB_SH su -c "mv $path_tmpapp/com.jackkum.tester.test $path_sysapp/com.jackkum.tester.test" || exit -1
# Stop the app
$ADB_SH "am force-stop $app_package"
$ADB_SH pm install -r "$path_sysapp/com.jackkum.tester.test" || exit -1
Запуск
#!/bin/bash
ADB="adb -s deviceId"
ADB_SH="$ADB shell"
# Re execute the tests
$ADB_SH su -c "am instrument -w -r -e debug false com.jackkum.tester.test/android.support.test.runner.AndroidJUnitRunner"