Android 如何在uiautomator测试用例中获取上下文?
我有我的uiautomator测试用例:Android 如何在uiautomator测试用例中获取上下文?,android,performance,android-intent,android-uiautomator,Android,Performance,Android Intent,Android Uiautomator,我有我的uiautomator测试用例: public class clickTest extends UiAutomatorTestCase { public void myTest() throws UiObjectNotFoundException { ... //Is it possible to get Context or Activity here? } } Runtime.getRuntime().exec("pm u
public class clickTest extends UiAutomatorTestCase {
public void myTest() throws UiObjectNotFoundException {
...
//Is it possible to get Context or Activity here?
}
}
Runtime.getRuntime().exec("pm uninstall com.example.MyApp");
我想知道,是否可以在UiAutomatorTestCase
中获取Context
或Activity
实例
或者如何在
UiAutomatorTestCase
中获取PackageManager
?您可以通过直接从uiautomator测试用例运行命令来使用PackageManager:
public class clickTest extends UiAutomatorTestCase {
public void myTest() throws UiObjectNotFoundException {
...
//Is it possible to get Context or Activity here?
}
}
Runtime.getRuntime().exec("pm uninstall com.example.MyApp");
但是,如果您需要访问上下文、活动等,最好使用Android的InstrumentationTestRunner,查看优秀的Android文档以获取更多信息。
这在UiAutomator 2.0或更高版本中是可能的 UiAutomator的原始版本作为shell程序运行(
adb shell UiAutomator runtest…
)。因为它不是作为Android应用程序运行的,所以它没有访问应用程序上下文对象的权限
UiAutomator 2.0基于。测试编译为APK,并在应用程序进程中运行(通过adb shell am instrument…)。如果测试扩展,可以使用getInstrumentation().getContext()
获取上下文
有关更多详细信息,请参阅。此选项适用于-
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class MyTest {
@Test
public void test1() {
Context context = InstrumentationRegistry.getContext();
}
}
InstrumentationRegistry.getInstrumentation().targetContext
您当然无法访问
活动
,因为它在单独的进程中运行。我不知道你也可以获得上下文
,更不用说PackageManager
。谢谢,我也这么想,我发布这个问题只是为了确保:),我想访问上下文的原因是我想在测试中使用PackageManager。让我们看看他们的其他人是否知道更多关于它的信息。你会声明并实例化clickText吗?