【问题标题】:Espresso loop over tests浓缩咖啡循环测试
【发布时间】:2015-11-22 21:15:17
【问题描述】:

我有一个带有按钮 GridView 的活动,我在 for 循环中以编程方式从字符串数组中获取文本。

for(int j=0; j < string_array.length; j++) {
        final String str = string_array[j];
        Button b= new Button(getApplicationContext());
        b.setText(str);
        b.setId(j);

        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent i = new Intent(MainActivity.this, NewActivity.class);
                i.putExtra("key",str);
                startActivity(i);
            }
        });

        gridLayout.addView(b,j);
    } 

使用 Espresso 我想测试按钮是否存在、是否有正确的文本以及单击按钮是否会将我带到另一个活动(检查标题文本)。

对于一个按钮,我进行了以下测试:

@Test
public void testOneButton() {
    onView(withId(R.id.button)).check(matches(notNullValue()));
    onView(withId(R.id.button)).check(matches(withText(R.string.button_text)));
    onView(withId(R.id.button)).perform(click());

    // in new activity
    CharSequence title = InstrumentationRegistry.getTargetContext()
            .getString(R.string.new_activity_title);
    matchToolbarTitle(title);

}

如何为 GridView 中的所有按钮编写测试而不修复 string_array?在“执行(单击())”之后,我在一个新活动中,因此无法再次单击旧/原始活动中的按钮。是否可以写类似的东西,

for(int j=0; j < string_array.length; j++) {
    @Test
    testButton(j)
}

其中“testButton(j)”与 testOneButton 基本上是相同的测试,为 id 为“j”的各个按钮参数化?

或者是否有可能在 Espresso 中返回原始活动并具有类似的结构?

@Test
public void testButtons() {
    for (int j=0; j<string.array.length; j++) {
        // test button with id "j"
        // return to original activity
    }
}

【问题讨论】:

    标签: android testing android-espresso


    【解决方案1】:

    我将第二个版本用于我需要在我的应用中执行的操作。您可以使用 Espresso 方法 pressBack() 回到主要活动。希望这可以帮助。祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多