0
我有一個按鈕的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」的相應按鈕?
還是有可能在濃縮咖啡回到原來的活動,並有像?
@Test
public void testButtons() {
for (int j=0; j<string.array.length; j++) {
// test button with id "j"
// return to original activity
}
}