1

I've just started learning to use Robotium for testing my app. I have written a test case that resets a list of stats, and then checks if the values are equal to 0. The code is below:

public void testClearStats() {
    solo.clickOnButton("Clear Stats");
    solo.clickOnButton("Yes");
    TextView views = (TextView) solo.getView(R.id.textViewsNum);
    TextView prompts = (TextView) solo.getView(R.id.textPromptsNum);
    TextView completions = (TextView) solo.getView(R.id.textCompleteNum);
    assertEquals("0", views.getText().toString());
    assertEquals("0", prompts.getText().toString());
    assertEquals("0", completions.getText().toString());
}

The test was failing when it shouldn't because it was checking the values of the TextViews before their results were reset. To get around this, I added this line:

solo.waitForActivity(solo.getCurrentActivity().toString());

With this statement the test passes but it seems to take an unnecessary long time to complete. I was wondering if there was a better/correct way of way doing this, or is this the best way of doing it?

Thanks

4

2 回答 2

4

您将不得不等待某些事情,您选择的内容将取决于您的应用程序,如果不看它,我无法回答什么是最好的等待。

您有哪些视觉指标可以让重置发生?你有新的活动开放吗?是否有文字告诉它已完成?如果它实际上只是三个文本字段。如果是这样,那么您可能会设法使用 solo.waitfortext("0") 尽管更好的方法是使用新的条件概念并使用 solo.waitForCondition(method) (条件可能是等待文本为 0,但您会将条件放在一个位置,然后如果您以后找到更好的方法,那么您只需更改一次)。

public class WaitForReset implements Condition
{

    public boolean isSatisfied()
    {
        TextView views = (TextView) solo.getView(R.id.textViewsNum);
        TextView prompts = (TextView) solo.getView(R.id.textPromptsNum);
        TextView completions = (TextView) solo.getView(R.id.textCompleteNum);
        if(isViewZero(views) && isViewZero(prompts) && isViewZero(completions))
        {
            return true
        }
        else
        {
            return false;
        }
    }

    private isViewZero(TextView textView)
    {
        if((textView!=null) && (textView.getText().toString() ==0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

然后,您可以断言 waitforcondition 的值为真!

于 2013-02-04T15:15:37.253 回答
-1

您始终可以使用 waitForActivity 并选择特定的超时。solo.waitForActivity(YourActivity.class, timeout);

于 2013-06-24T14:27:52.083 回答