Espresso 自動化測試使用
Espresso是一個Android UI測試框架,由三部分組成
- ViewMachers:尋找View
- ViewActions: 執行交互事件
- ViewAssertions:檢驗測試結果
添加加依賴庫
android{
defaultConfig{
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies{
androidTestCompile 'com.android.support:support-annotations:24.1.1'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
-
com.android.support:support-annotations
是注解庫 -
com.android.support.test:runner
一個非捆綁的測試運行庫 -
com.android.support.test:rules
一套使用在AndroidJUnitRunner的規則庫 -
com.android.support.test.espresso:espresso-core
UI測試的核心庫
使用Espresso測試
-
測試的UI對象TestActivity
-
布局文件 activity_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test"/> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="botton" </LinearLayout>
-
TestActivity
public class TestActivity extends Activity{ TextView text; Button btn; public void onCreate(Bundle data){ setContentView(R.layout.activity_test); text = (TextView)findViewById(R.id.text); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(view -> text.setText("finish")); } }
-
-
在androidTest文件目錄下建一個測試類
@RunWith(AndroidJUnit4.class)
public class UITest{
//設置初始啟動測試Activity,并啟動
@Rule
public ActivityTestRule<TestActivity> mActivityRule
= new ActvitiyTestRule<>(TestActivity.class);
@Test
public void testClick(){
Espresso.onView(ViewMatchers.withId(R.id.btn)).preform(ViewActions.click()).check(ViewAssertions.matches(ViewMatchers.withText("finish")));
}
}
ActivityTestRule 這個rule是用來測試單個Activity的,Activity將在@Test和@Before前啟動
-
當你啟動的Activity需要帶參數時
@RunWith(AndroidJUnit4.class)
public class UITest{
//設置初始啟動測試Activity,ActivityTestRule的構造函數的第三個參數是否啟動Activity
@Rule
public ActivityTestRule<TestActivity> mActivityRule
= new ActvitiyTestRule<>(TestActivity.class,false,false);
@Test
public void startActivity(){
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
Intent intent = new Intent(context,TestActivity.class);
intent.putExtra("id",2);
mActivityRule.launchActivity(intent);
}
@Test
public void testClick(){
Espresso.onView(ViewMatchers.withId(R.id.btn)).perform(ViewActions.click()).check(ViewAssertions.matches(ViewMatchers.withText("finish")));
}
}
Espresso
-
AdapterView、ListView、GridView 事件測試
- ListView 的點擊事件
//點擊指定數據所在的 Item Area area = new Area(); onData(allOf(is(instanceOf(Area.class)),is(area))).perform(click()); //點擊指定位置的 Item onData(is(instanceOf(Area.class))).atPosition(position).perform(click()); //使用自定義匹配數據的方法 onData(allOf(is(instanceOf(Area.class)),withAreaId("12"))).perform(click()); //點擊特殊的 Item,如這里的 HeadView onData(is(instanceOf(Area.class))).onChildView(instanceOf(HeadView.class)).perform(click());
withAreaId 方法是返回一個自定義的Matcher,后面會講到Matcher的自定義
-
RecyclerView 事件測試
測試 RecyclerView 需要使用RecyclerViewActions,使用該類需要在build.Gradle文件引入該庫
RecyclerViewActions的使用androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
//點擊ID為recycler的RecyclerView的第2個位置的子View onView(withId(R.id.recycler)).perform(RecyclerViewActions.actionOnItemAtPosition(2,click())); //以下是RcyclerViewActions的一些Action方法 //操作匹配的HolderView actionOnHolderItem (Matcher<VH> viewHolderMatcher,ViewAction viewAction) //操作匹配的Item actionOnItem (Matcher<View> itemViewMatcher, ViewAction viewAction) //操作指定位置的Item actionOnItemAtPosition (int position,ViewAction viewAction) //移動到匹配的Item位置 scrollTo (Matcher<View> itemViewMatcher) //移動到匹配的ViewHolder的位置 scrollToHolder (Matcher<VH> viewHolderMatcher) //移動到指定位置 scrollToPosition (int position)
-
自定義Matcher
定義前面提到的AdapterView的WithAreaId的Matcher
public class CustomViewMatchers{ public static Matcher<Object> withAreaId(String id){ public static Matcher<Object> withAreaId(final String areaId){ return new BoundedMatcher<Object, Area>(Area.class) { @Override public void describeTo(Description description) { description.appendText("this a Matcher as match areaId"); } @Override protected boolean matchesSafely(Area item) { return item.getAreaId().equals(areaId); } }; } } public static Matcher<View> withStartText(String startStr){ return new BaseMatcher<View>() { @Override public boolean matches(Object item) { TextView text = (TextView) item; return text.getText().toString().startsWith(startStr); } @Override public void describeTo(Description description) { description.appendText("this is a Matcher as match head of String"); } }; } }
-
自定義ViewAction
public class CustomViewActions{
static final class SeekBarAction implements ViewAction{
private int progress;
public SeekBarAction(int progress){
this.progress = progress;
}
@Override
public Matcher<View> getConstaints(){
return allOf(ViewMatchers.isAssignableFrom(SeekBar.class),ViewMatchers.isDisplayed());
}
@Override
public String getDescription(){
return "this is a seek ation with SeekBar";
}
@Override
public void perform(UiController uiController,View view){
SeekBar seekBar = (SeekBar)view;
seekBar.seekTo(progress);
}
}
}
-
異步操作測試
需要自定義 IdlingResouce,看了幾篇博客描述的都有問題,建議直接參考Google官方項目 IdlingResourceSample
IdlingResource的使用已經補上,由于這篇文章篇幅已經較長,故在專門寫了一篇
Espresso IdlingResource 測試延時操作的使用
ps:以上是本人對Espress的使用經驗,有什么問題可以評論
參考資料:
- https://github.com/googlesamples/android-testing/
- http://gavinliu.cn/2015/12/30/Android-%E6%B5%8B%E8%AF%95%E6%A1%86%E6%9E%B6%E4%B9%8B-UI-Testing-Espresso/
- https://google.github.io/android-testing-support-library/
- http://tbfungeek.github.io/2016/07/01/Android-%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95Espreso/
- http://ztzt123.cn/android/2016/08/19/Android%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E5%92%8CUI%E6%B5%8B%E8%AF%95.html
- https://developer.android.com/reference