failed to make test using uiautomator 2.0

273 Views Asked by At

I want to make uiautomator 2.0 test from the command line. My steps are below:

  1. create an Instrumentation project, the code is below:

    package com.android.uiautomator.client;
    
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import android.app.Application;
    import android.app.Instrumentation;
    import android.content.Context;
    import android.support.test.InstrumentationRegistry;
    import android.support.test.runner.AndroidJUnit4;
    import android.test.ApplicationTestCase;
    
    @RunWith(AndroidJUnit4.class)
    public class Initialize {
    
    @Test
    public void testStartServer() {
        System.out.println("this is the test");
    }
    }
    
  2. use gradlew to generate the test apk

  3. install the test apk into the android emulator

    adb shell push ${apkPath}/test.apk /data/local/tmp/com.android.test
    
    adb shell pm install -r /data/local/tmp/com.android.test
    
  4. check if the test apk is installed successfully. And I can find the installed apk.

    adb shell pm list packages
    
  5. after install, I check the Instrumentation in the emulator, but I cant't find the Instrumentation about the test apk. This will lead to failed on executing the command:

    adb shell am instrument -e class com.android.uiautomator.client.Initialize -w com.android.uiautomator.client.test/android.support.test.runner.AndroidJUnitRunner
    

so, I am confused. Why can't I find the instrumentation after I have installed the test apk?

1

There are 1 best solutions below

2
On

You should run

$ adb shell pm list instrumentation | grep 'target=applicationId'

replacing applicationId by your applicationId to check the available instrumentations for an app.