1

I'm trying to launch a 3rd-party app using UiAutomator with this code:

val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
        device.pressHome()
        val context = ApplicationProvider.getApplicationContext<Context>()
        val intent = context.packageManager.getLaunchIntentForPackage(packageName)?.apply {
            // Clear out any previous instances
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
        }

        println("Component: ${intent?.component?.flattenToShortString()}")

        Assert.assertNotNull("Failed to find launch intent", intent)

        context.startActivity(intent)

Intent is OK and it works for, for example, Google Play, but doesn't work for another app I want to launch (I can't disclose it).

Even more, adb shell am start -n package/Class works, but Runtime.getRuntime().exec("am start -n package/Class") reports that Starting: Intent ... but it doesn't appear on the screen when running from the test.

If I start the activity using adb shell while running the test it starts fine too.

I think that this app detects somehow that it's launched from instrumented test. Is it possible to hide the fact that I'm launching it from test?

artem
  • 16,382
  • 34
  • 113
  • 189

1 Answers1

1

Instead of getting the context from ApplicationProvider, use InstrumentationRegistry.getInstrumentation().getTargetContext()

Dhanush
  • 822
  • 1
  • 8
  • 15