居preSSO - 断言与异步加载的数据一个TextView断言、加载、数据、preSSO

2023-09-06 00:20:43 作者:比张岳还要命的男らRen

我在写与谷歌居preSSO为Android的UI测试和我卡在如何认定一个TextView的文本,它的内容是从异步Web服务加载。我现在的code是:

I'm writing a UI test with Google Espresso for Android and I'm stuck on how to assert a TextView text, which content is asynchronously loaded from a web service. My current code is:

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),我告诉我的活动加载来自Web服务的元素。 (2)上,我在等的观点断言其内容。这是测试失败的部分,因为它执行的Web服务应答应用程序之前。

On (1), I inform my activity to load an element from the webservice. On (2), I'm waiting for the view asserting its content. This is the part that the test fails, because it executes before the webservice answers the app.

我怎么能告诉居preSSO等待一段特定的数据出现在屏幕上?或者我应该想以不同的方式来写这样的测试?

How can I tell Espresso to wait for a particular piece of data to appear on the screen? Or should I think in a different way to write such test?

推荐答案

您可以通过注册一个IdlingResource与居preSSO Web服务处理这种情况。看看这写:https://$c$c.google.com/p/android-test-kit/wiki/Es$p$pssoSamples#Using_registerIdlingResource_to_synchronize_with_custom_resource

You can handle this case by registering an IdlingResource for your web service with Espresso. Take a look at this write-up: https://code.google.com/p/android-test-kit/wiki/EspressoSamples#Using_registerIdlingResource_to_synchronize_with_custom_resource

最有可能的,你会想用CountingIdlingResource (它使用一个简单的计数器来跟踪事情的时候处于闲置状态)。该sample测试演示了如何可以做到这一点。

Most likely, you'll want to use CountingIdlingResource (which uses a simple counter to track when something is idle). This sample test demonstrates how this can be done.

 
精彩推荐
图片推荐