java - 使用 Espresso 测试表盘 Activity

标签 java android testing android-espresso android-testing

我正在尝试测试单击将用户发送到“电话” 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 的“等待”方式是使用IdlingResourceHere's a simple explanation of you can follow.

关于java - 使用 Espresso 测试表盘 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43001367/

相关文章:

java - 检查程序是否从 java applet 安装在 linux 机器上

java - 使用 ScheduledExecutorService 安排每月任务

android - Appcelerator 钛 : Open activity without animation

php - 将二维数组提交到单元测试 laravel 失败

使用 mocha 的 nodejs(连接)应用程序的 http 请求测试?

java - 正则表达式提取由换行符分隔的两个字符串之间的字符串

java获取类包的名称

android - 在运行时绘制 HSV 圆

javascript - window.orientation 在 iOS 和 Android 中返回不同的值

python - 开始自动化测试