Espresso 循环测试
Espresso loop over tests
我有一个带有按钮 GridView 的 activity,我在 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 我想测试按钮是否存在、是否有正确的文本以及单击一个按钮是否会将我带到另一个按钮 activity(检查标题文本)。
对于一个按钮,我进行了以下有效测试:
@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);
}
如何在不修复 string_array 的情况下为 GridView 中的所有按钮编写测试?在 "perform(click())" 之后,我在一个新的 activity 中,因此无法再次单击 old/original activity 中的按钮。是否可以这样写,
for(int j=0; j < string_array.length; j++) {
@Test
testButton(j)
}
其中 "testButton(j)" 基本上与 testOneButton 相同的测试,为具有 id "j"?
的各个按钮参数化
或者在 Espresso 中是否可以 return 到原来的 activity 并具有类似的结构?
@Test
public void testButtons() {
for (int j=0; j<string.array.length; j++) {
// test button with id "j"
// return to original activity
}
}
我使用第二个版本来完成我需要在我的应用程序中执行的操作。您可以使用 Espresso 方法 pressBack() 返回主 activity。希望这可以帮助。祝你好运!
我有一个带有按钮 GridView 的 activity,我在 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 我想测试按钮是否存在、是否有正确的文本以及单击一个按钮是否会将我带到另一个按钮 activity(检查标题文本)。
对于一个按钮,我进行了以下有效测试:
@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);
}
如何在不修复 string_array 的情况下为 GridView 中的所有按钮编写测试?在 "perform(click())" 之后,我在一个新的 activity 中,因此无法再次单击 old/original activity 中的按钮。是否可以这样写,
for(int j=0; j < string_array.length; j++) {
@Test
testButton(j)
}
其中 "testButton(j)" 基本上与 testOneButton 相同的测试,为具有 id "j"?
的各个按钮参数化或者在 Espresso 中是否可以 return 到原来的 activity 并具有类似的结构?
@Test
public void testButtons() {
for (int j=0; j<string.array.length; j++) {
// test button with id "j"
// return to original activity
}
}
我使用第二个版本来完成我需要在我的应用程序中执行的操作。您可以使用 Espresso 方法 pressBack() 返回主 activity。希望这可以帮助。祝你好运!