UiAutomator 2.0 test from command line

6.6k views Asked by At

I'm trying to use Android UIAutomator for some simple tests, I'm aware of that it needs to be built by Gradle since UIAutomator 2.0, I can run my simple test which only presses home button via Android Studio or command line by "gradlew.bat cC", I was wondering how I can run it with adb command?

I tried

adb shell am instrument -w test.simple.uiatest/android.test.InstrumentationTestRunner

as suggested here , but I get

INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{test.simple.uiatest/android.test.InstrumentationTestRunner}
INSTRUMENTATION_STATUS_CODE: -1
android.util.AndroidException: INSTRUMENTATION_FAILED: test.simple.uiatest/android.test.InstrumentationTestRunner
    at com.android.commands.am.Am.runInstrument(Am.java:951)
    at com.android.commands.am.Am.onRun(Am.java:316)
    at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
    at com.android.commands.am.Am.main(Am.java:99)
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:250)

Below are my code snippet and build.gradle, what am I doing wrong?

package test.simple.uiatest;

import android.support.test.uiautomator.UiDevice;
import android.test.InstrumentationTestCase;

public class ApplicationTest extends InstrumentationTestCase {
    private UiDevice theDevice;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        theDevice = UiDevice.getInstance(getInstrumentation());

        theDevice.pressHome();
    }

    public void testName() throws Exception {
        theDevice.pressHome();
    }
}

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc2"

    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        applicationId "test.simple.uiatest"
        minSdkVersion 21
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile 'com.android.support.test:runner:0.2'
    androidTestCompile 'com.android.support.test:rules:0.2'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'
}
3

There are 3 answers

0
Allen Hair On BEST ANSWER

The error you're seeing will occur if the test APK is not installed, or if the <test_package>/<runner_class> in your am instrument ... command is wrong.

To list the instrumentation tests that are available on your device, you can run adb shell pm list instrumentation. You should see a line like:

instrumentation:test.simple.uiatest/android.test.InstrumentationTestRunner (target=simple.target.app)

If you don't see a line matching your test, then you need to install the test APK. If it is installed, double check that the test.simple.uiatest/android.test.InstrumentationTestRunner portion of the pm list instrumentation output matches your am instrument command.

0
Eaway Lu On

As the build.gradle in your post, you should use "android.support.test.runner.AndroidJUnitRunner"

adb shell am instrument  -w test.simple.uiatest/android.support.test.runner.AndroidJUnitRunner
0
FkinH On

for your package, try this:

adb shell am instrument -w test.simple.uiatest.test/android.support.test.runner.AndroidJUnitRunner

use package test.simple.uiatest.test instead.