3

I am writing some Automated TestCase using UiAutomator across apps from my app. My aim is to find the Current Activity of all the app which i click.

I have project called MyApp with package called com.example with one Activity, MainActivity

I tried the following (everything inside my app under androidTest)

public class ActivityTester extends InstrumentationTestCase {

private UiDevice device;

@Test
public void testAdd() throws Exception {

}

@Override
protected void setUp() throws Exception {
    super.setUp();

    Instrumentation instrumentation = getInstrumentation();

    Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor("com.example.MainActivity", null, false);


    device = UiDevice.getInstance(instrumentation);

    device.pressHome();

    device.wait(Until.hasObject(By.desc("Apps")), 3000);

    UiObject2 appsButton = device.findObject(By.desc("Apps"));
    appsButton.click();

    device.wait(Until.hasObject(By.text("MyApp")), 3000);

    UiObject2 calculatorApp = device.findObject(By.text("MyApp"));
    calculatorApp.click();

    Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 3000);

}

Here I am Clicking on HomeMenu and launch Myapp and attach to the monitor with com.example.MyActivity, I am able to get the activity instance in this line of Code

Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 3000);

Now If i change the flow. HomeMenu --> SomeOtherApp and attach to the monitor with the fully qualified launcherActivity of SomeOtherApp say com.someotherapp.MainActivity. I am not able to get the activity instance. currentActivity is null

Is there a way I can get the current Activity instance of any app that I launch through UiAutomator?

Arnold Laishram
  • 1,801
  • 2
  • 18
  • 25

1 Answers1

1

seems only activities in the same package can be monitored, for another app's acitivities waitForActivityWithTimeout just returns null.

If u only get activity instance for context-needed-api, may do follow as i do

  1. start activity in the same package of ui-automator-test-class
  2. use monitor to get that activity

    public class ExampleInstrumentedTest {

      private UiDevice mDevice;
      private Activity mActivity;

      @Before
      public void before() {
        // Initialize UiDevice instance
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        mActivity = getActivity();

        // Start from the home screen
        mDevice.pressHome();
      }

      private Activity getActivity() {
        Activity activity = null;
        Instrumentation inst = InstrumentationRegistry.getInstrumentation();
        Context context = inst.getContext();
        Instrumentation.ActivityMonitor monitor =
                inst.addMonitor("com.wispeedio.uiautomator2hello.MainActivity", null, false);

        Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage("com.wispeedio.uiautomator2hello");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        activity = monitor.waitForActivityWithTimeout(2000);

        return activity;
      }

      private void takeScreenshot(String name) {
        File file = Utils.CreateFileToExternal(mActivity, name, "test-screenshots");
        mDevice.takeScreenshot(file);
      }
    }