Espresso 自动化测试的使用

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
    
        
        
    • 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 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 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 自动化测试的使用_第1张图片
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文件引入该库

    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
    
    RecyclerViewActions的使用
    //点击ID为recycler的RecyclerView的第2个位置的子View
    onView(withId(R.id.recycler)).perform(RecyclerViewActions.actionOnItemAtPosition(2,click()));
    //以下是RcyclerViewActions的一些Action方法
    //操作匹配的HolderView
    actionOnHolderItem (Matcher viewHolderMatcher,ViewAction viewAction)
    //操作匹配的Item
    actionOnItem (Matcher itemViewMatcher, ViewAction viewAction)
    //操作指定位置的Item
    actionOnItemAtPosition (int position,ViewAction viewAction)
    //移动到匹配的Item位置
    scrollTo (Matcher itemViewMatcher)
    //移动到匹配的ViewHolder的位置
    scrollToHolder (Matcher viewHolderMatcher)
    //移动到指定位置
    scrollToPosition (int position)
    
  • 自定义Matcher

    定义前面提到的AdapterView的WithAreaId的Matcher

    public class CustomViewMatchers{
    
        public static Matcher withAreaId(String id){
        
             public static  Matcher withAreaId(final String areaId){
                return new BoundedMatcher(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 withStartText(String startStr){
               return new BaseMatcher() {
                   @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 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

    你可能感兴趣的:(Espresso 自动化测试的使用)