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?