我正在尝试测试单击将用户发送到“电话” Activity 的按钮...如下所示:
public void callNumber(String number){
Uri numberUri = Uri.parse("tel:"+number);
Intent callIntent = new Intent(Intent.ACTION_DIAL, numberUri);
startActivity(callIntent);
}
嗯...但是当我运行这个测试时:
@Rule
public IntentsTestRule<HelpView> mActivityRule = new IntentsTestRule<>(HelpView.class, true, true);
@ClassRule
static public DeviceAnimationTestRule deviceAnimationTestRule = new DeviceAnimationTestRule();
@Test
public void clickDialButtonTest(){
onView(withId(R.id.help_viewpager)).perform(swipeLeft());
onView(withId(R.id.help_viewpager)).perform(swipeLeft());
onView(withId(R.id.phone_call_btn)).perform(click());
intended(allOf(hasAction(Intent.ACTION_DIAL)));
}
我明白了:
想要匹配 1 个 Intent 。实际上匹配了 0 个 Intent 。
编辑:
实际上,Espresso 不会等待 ViewPager 中的切换完成。因此,当单击该按钮时,什么也没有发生。我可以用这个解决问题:
@Test
public void clickOnMapButtonTest() {
onView(withId(R.id.help_viewpager)).perform(swipeLeft());
onView(withId(R.id.help_viewpager)).perform(swipeLeft());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Assert.fail();
}
onView(withId(R.id.phone_call_btn)).perform(click());
intended(allOf(hasAction(Intent.ACTION_DIAL), toPackage("com.android.dialer")));
}
但这看起来有点错误......也许有更好的方法。有没有人有更好的选择?
感谢任何帮助!
最佳答案
您可能使用的是 ActivityTestRule
而不是 IntentsTestRule
。
编辑:
你不应该使用Thread.sleep(2000)
...Espresso 的“等待”方式是使用IdlingResource
。 Here's a simple explanation of you can follow.
关于java - 使用 Espresso 测试表盘 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43001367/