Jithin Gangadharan
Jithin Gangadharan

Reputation: 53

How to execute UiAutomator test cases programatically in android latest versions

Below code is working fine when I execute with adb shell command. But if I tried with apk, it is not working. Any help?

MainActivity.class

public void onStart() {

    super.onStart();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Bundle bundle = new Bundle();
            bundle.putString("Test", "value");

            try {
                startInstrumentation(new ComponentName("com.example.mypro.uiauto.test", "android.support.test.runner.AndroidJUnitRunner"),null, bundle);

            } catch (Exception e) {

            }
        }
    });


}

ExampleInstrumentedTest.class

@Test
public void pressHome() {

    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    uiDevice.pressHome();
}

Upvotes: 1

Views: 1243

Answers (1)

Sushant Somani
Sushant Somani

Reputation: 1500

I landed into the same situation last year in August. So far you cannot trigger instrumented tests from the APK. Here is the link of Android guide. Because in that case you can access UI of other applications too. This will be a security issue in the overall android OS where one app is opening another app and doing random stuff which is a threat.

You can execute instrumented tests either via command prompt/terminal or via Android studio only. The link I've shared, there is a column with heading "Run UI Automator tests on a device or emulator".

Upvotes: 2

Related Questions