Espresso——实例分析

     通过实例来分析Espreeso的使用


<span style="font-size:10px;">import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;

public class Tes extends ActivityInstrumentationTestCase2{

private static final String TARGET_PACKAGE_ID = "com.example.android.notepad" ;
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.example.android.notepad.NotesList" ;
            
    private static Class<?> launcherActivityClass;
    
    static {
        try {
        launcherActivityClass =
        Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
        }
        }

    //add one note
    public void testCreateNote(){
        //input sth in inputfiled
        String text = "this is" ;
        
        <span style="color:#3366FF;">//get object from </span></span><span style="font-size:10px;color:#3366FF;">withId</span><span style="color:#3366FF;">  </span>      
  <span style="font-size:10px;">      ViewInteraction create_button = onView(ViewMatchers.withId(getInstrumentation().getTargetContext().getResources()
                .getIdentifier("com.example.android.notepad:id/menu_add", null, null)));
        
        <span style="color:#3366FF;">//Open new note view</span>
        create_button.perform(ViewActions.click()) ;
        
        ViewInteraction inputFiled =  onView(ViewMatchers.withId(getInstrumentation().getTargetContext().getResources()
                .getIdentifier("com.example.android.notepad:id/note", null, null)));
        
        
        inputFiled.perform(ViewActions.typeText(text)) ;
        
        <span style="color:#3366FF;">//save this note and quite this note</span>
        this.quitNote();
        
        //verify this is new note
        ViewInteraction new_note = onView(ViewMatchers.withText(text));
        new_note.check(ViewAssertions.matches((ViewMatchers.isDisplayed()))) ;
    
    }

    //update note
    public void testUpdateNote(){
        //根据列表内note的名字,获取该对象,然后进行点击操作
                String name = "D" ;
                String updateName = "update" ;
                <span style="color:#3366FF;">//get boject from text</span>
                ViewInteraction note = onView(ViewMatchers.withText(name));
                note.perform(ViewActions.longClick()) ;
                
                ViewInteraction edit = onView(ViewMatchers.withText("Edit title"));
                
                //update note
                edit.perform(ViewActions.click()) ;
                
                ViewInteraction editFiled = onView(ViewMatchers.withId(getInstrumentation().getTargetContext().getResources()
                        .getIdentifier("com.example.android.notepad:id/title", null, null)));
                
                
                editFiled.perform(ViewActions.typeText(updateName)) ;
                
                //save this note and quite this note
                this.okButton();
                
                <span style="color:#3366FF;">//verify this is new note</span>
                ViewInteraction new_note = onView(ViewMatchers.withText(name + updateName));
                new_note.check(ViewAssertions.matches((ViewMatchers.isDisplayed()))) ;
    }
   

}</span>


然后补充以下内容:

ViewMatchers

下面介绍ViewMatchers的一些常用方法

1、

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值