使用浓缩咖啡测试表盘活动

时间:2017-03-24 13:56:27

标签: java android testing android-espresso android-testing

我正在尝试测试点击按钮,将用户发送到电话活动...就像这样:

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个意图。实际上匹配0意图。

修改

实际上,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")));
}

但这看起来有点不对......也许有更好的方法。有人有更好的选择吗?

感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

您可能正在使用ActivityTestRule而不是IntentsTestRule

编辑:

您不应该使用Thread.sleep(2000) ...“等待”的Espresso方式正在使用IdlingResourceHere's a simple explanation of you can follow.

答案 1 :(得分:0)

使用Call<Student> studentCall = client.getStudentById(id); studentCall.enqueue(new Callback<Student>() { @Override public void onResponse(Call<Student> call, Response<Student> response) { student = response.body(); Log.i("STUDENT :", student.toString()); } @Override public void onFailure(Call<Student> call, Throwable t) { Log.i("FAIL STUDENT :", t.getMessage()); } }); Call<Course> courseCall = client.getCourse(c); courseCall.enqueue(new Callback<Course>() { @Override public void onResponse(Call<Course> call, Response<Course> response) { course = response.body(); Log.i("COURSE : ", course.toString()); } @Override public void onFailure(Call<Course> call, Throwable t) { Log.i("FAIL COURSE ", t.getMessage()); } }); Absence absence = new Absence(); absence.setCourse(course); absence.setCourse_session(session); absence.setStudent(student); Log.i("ABSENCE :" , absence.toString()); Call<Absence> absenceCall = client.addAbsence(absence); absenceCall.enqueue(new Callback<Absence>() { @Override public void onResponse(Call<Absence> call, Response<Absence> response) { } @Override public void onFailure(Call<Absence> call, Throwable t) { } }); 时,IntentsTestRule将在创建活动后开始记录意图,而Intents.init()将在活动结束后清除所有记录的意图。这里使用Intents.release()是有问题的,因为IntentsTestRule活动结束时Intents.release()会清除所有记录的意图,并在您有机会通过{{ 1}}。

相反,您可以使用HelpView并在启动活动之前调用Dialer,并在测试后调用intended,如下所示:

ActivityTestRule