我正在使用Google espresso为Android编写UI测试,我暂时暂时断言TextView文本,其中内容从Web服务中加载了哪些内容。我当前的代码是:

public class MyTest extends BaseTestCase<MyActivity>{
    public void setUp() throws Exception {
        // (1) Tell the activity to load 'element-to-be-loaded' from webservice
        this.setActivityIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("data://data/element-to-be-loaded")));
        getActivity();

        super.setUp();
    }

    public void testClickOnReviews(){
        // (2) Check the element is loaded and its name is displayed
        Espresso
            .onView(ViewMatchers.withId(R.id.element_name))
            .check(ViewAssertions.matches(ViewMatchers.withText("My Name")));

        // (3) Click on the details box
        Espresso
            .onView(ViewMatchers.withId(R.id.details_box))
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        // (4) Wait for the details screen to open
        Espresso
            .onView(ViewMatchers.withId(R.id.review_box));

        // Go back to element screen
        Espresso.pressBack();
    }
}
.

在(1)上,我通知我的活动将元素从WebService加载。在(2)上,我正在等待查看其内容的视图。这是测试失败的部分,因为它在WebService回答应用程序之前执行。

如何告诉espresso等待特定的数据才能出现在屏幕上?或者我应该以不同的方式思考来写这样的测试吗?

有帮助吗?

解决方案

您可以通过使用espresso注册Web服务的idlingResource来处理这种情况。看看这个写作: https://developer.android.com /培训/测试/ espresso / idling-resource.html

很可能,您将想要使用 countingidlingresource (它使用一个简单的计数器在某些东西空闲时跟踪)。这个样本测试演示如何完成。

其他提示

如果你没有用浓缩咖啡用Uiautomator打扰,你可以在你的步骤4中做这样的事情。

UiObject object = mDevice.findObject(new UiSelector().resourceId(packageName + ":id/" + "review_box"));
object.waitForExists(5000);
.

https://开发人员。android.com/reference/android/support/test/uiautomator/uiobject.html#waitforexists (long)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top