如何在uiautomator测试用例中获取上下文?

Mel*_*lon 6 performance android android-intent android-uiautomator

我有我的uiautomator测试用例:

public class clickTest extends UiAutomatorTestCase {   

   public void myTest() throws UiObjectNotFoundException {   
       ...
       //Is it possible to get Context or Activity here?  

   }
}
Run Code Online (Sandbox Code Playgroud)

我想知道,是否有可能获得ContextActivity实例UiAutomatorTestCase

或如何获得PackageManagerUiAutomatorTestCase

小智 0

您可以通过直接从 uiautomator 测试用例运行命令来使用 PackageManager:

Runtime.getRuntime().exec("pm uninstall com.example.MyApp");
Run Code Online (Sandbox Code Playgroud)

但是,如果您需要访问上下文、活动等,最好使用 Android 的 InstrumentationTestRunner,请参阅优秀的 Android 文档以获取更多信息。 http://developer.android.com/tools/testing/activity_test.html