如何通过Espresso测试方法更新活动的UI元素?

时间:2016-02-16 07:21:53

标签: android android-espresso android-instrumentation

我试图为我的活动编写测试用例。我的活动中有一个更新我的UI的方法。

public void setBooking(Booking booking)
{
    if (booking == null)
    {
        return;
    }

    this.mBooking = booking;
    this.mBookingExt.updateUI();
}

这是我的测试类:

@RunWith(AndroidJUnit4.class)
public class LocatingActivityTest
{
    @Rule
    public ActivityTestRule<BookingActivity> mActivityTestRule = new ActivityTestRule<>(BookingActivity.class);

    private BookingActivity mBookingActivity;

    @Before
    public void setup()
    {
        mBookingActivity = mActivityTestRule.getActivity();
        mBookingActivity.setBooking(BookingTest.getStandardTaxi()); // << My Fake Booking Object

        onView(withId(R.id.book_now)).perform(click());
    }

    @Test
    public void viewsMustBeVisible()
    {
        onView(withId(R.id.locating_text)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.sonarView)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.cancel_booking)).check(matches(isCompletelyDisplayed()));
    }
}

我在运行测试时遇到以下错误:

  

android.view.ViewRootImpl $ CalledFromWrongThreadException:只有   创建视图层次结构的原始线程可以触及其视图。

我认为@Before正在UI线程上运行所以不知道这个抱怨来自哪里! This is my referenceActivityTestRule延伸UiThreadTestRule)。

所以我决定改变一下类的架构。因此setup()viewsMustBeVisible()更改为:

    @Before
    public void setup()
    {
        mBookingTaxiActivity = mActivityTestRule.getActivity();
    }

    @UiThreadTest // from android.support.test.annotation.UiThreadTest
    public void viewsMustBeVisible()
    {
        mBookingTaxiActivity.setBooking(BookingTest.getStandardTaxi()); // << My Fake Booking Object

        onView(withId(R.id.fab_booking)).perform(click());
        onView(withId(R.id.booking_book_now)).check(matches(isEnabled()));
        onView(withId(R.id.booking_book_now)).perform(click());

        onView(withId(R.id.locating_text)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.sonarView)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.locating_cancel_booking)).check(matches(isCompletelyDisplayed()));
    }

但是,这次我收到了以下错误:

  

java.lang.Exception:没有可运行的方法

最后我更改了上面的方法来在UI线程上运行测试:

    @Test
    public void viewsMustBeVisible()
    {
        mBookingTaxiActivity.runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                mBookingTaxiActivity.setBooking(BookingTest.getStandardTaxi()); // << My Fake Booking Object

                onView(withId(R.id.fab_booking)).perform(click());
                onView(withId(R.id.booking_book_now)).check(matches(isEnabled()));
                onView(withId(R.id.booking_book_now)).perform(click());

                onView(withId(R.id.locating_text)).check(matches(isCompletelyDisplayed()));
                onView(withId(R.id.sonarView)).check(matches(isCompletelyDisplayed()));
                onView(withId(R.id.locating_cancel_booking)).check(matches(isCompletelyDisplayed()));
        });
    }

这里的问题是测试永远不会完成。我等了6-7分钟,它的进度指示器仍在旋转,似乎永远不会完成。

我想我已经检查了更新我的Booking对象以及UI元素的所有方法。你有任何经验吗?感谢。

1 个答案:

答案 0 :(得分:0)

很酷,很长一段时间后我都知道了。我不确定有什么不同,但我在活动中移动了UI线程。新的测试方法看起来像这样:

    @Test
    public void viewsMustBeVisible()
    {
        onView(withId(R.id.fab_booking)).perform(click());

        mBookingTaxiActivity.setTestBooking(BookingTest.getStandardTaxi());

        onView(withId(R.id.booking_book_now)).check(matches(isEnabled()));
        onView(withId(R.id.booking_book_now)).perform(click());

        onView(withId(R.id.locating_text)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.sonarView)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.locating_cancel_booking)).check(matches(isCompletelyDisplayed()));
    }

我在活动方面创建了一个方法来更新我的对象:

    // Required just for Testing purposes
    @VisibleForTesting
    public void setTestBooking(Booking booking)
    {
        if (booking == null)
        {
            return;
        }

        this.mBooking = booking;

        this.runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                BookingActivity.this.mBookingExt.updateUI();
                findViewById(R.id.booking_book_now).setEnabled(true);
            }
        });
    }

希望如果你遇到同样的问题会对你有帮助。