Espresso——实例分析

来源:互联网 发布:淘宝网云裳广场舞服装 编辑:程序博客网 时间:2024/06/06 19:50

     通过实例来分析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、withClassName(Matcher<String> classNameMatcher)

Returns a matcher that matches Views with class namematching the given matcher.

根据ClassName来获取对象

 

2、withContentDescription(String text)

Returns an Matcher that matches Views based oncontent description property value.

根据content description来获取对象

 

3、withId(int id)

Same as withId(is(int)), but attempts to look up resourcename of the given id and use an R.id.myView style description with describeTo.

根据id来获取对象

 

4、withText(String text)

Returns a matcher that matches TextView based on its text property value.

根据text来获取对象

 

5、withText(int resourceId)

Returns a matcher that matches a descendant ofTextView that is displaying the string associated with the given resource id.

根据resource中的text来获取对象

 

6、isChecked()

Returns a matcher that accepts if and only if the view isa CompoundButton (or subtype of) and is in checked state.

判断对象是checked

 

7、isDisplayed()

Returns a matcher that matches Views that arecurrently displayed on the screen to the user.

判断是显示

8、isEnabled()

Returns a matcher that matches Views that areenabled.

判断是enableed

 

9、isFocusable()

Returns a matcher that matches Views that arefocusable.

判断是可聚焦的

 

10、isNotChecked()

Returns a matcher that accepts if and only if the view isa CompoundButton (or subtype of) and is not in checked state.

判断不能checked

 

11、isSelected()

Returns a matcher that matches Views that areselected.

判断是selected的

 

ViewInteraction

下面介绍ViewInteraction常用的方法

1、perform(ViewAction... viewActions)

Performs the given action(s) on the view selected by thecurrent view matcher.

通过ViewActions来执行动作

2、check(ViewAssertion viewAssert)

Checks the given ViewAssertion on the the view selected by the current view matcher.

通过ViewAssertion来进行验证

 

ViewActions

1、clearText()

Returns an action that clears text on the view.

清除对象的文本内容

 

2、click()

Returns an action that clicks the view.

对象进行单次点击操作

 

3、doubleClick()

Returns an action that double clicks the view.

对象进行双击操作

 

4、longClick()

Returns an action that long clicks the view.

对象进行长按操作

 

5、pressBack()

Returns an action that clicks the back button.

按返回键

 

6、pressKey(int keyCode)

Returns an action that presses the key specified by thekeyCode (eg.

发送keycode,例如Keyevent.KEYCODE_BACK

 

7、pressKey(EspressoKey key)

Returns an action that presses the specified key with thespecified modifiers.

发送特殊的key

 

8、pressMenuKey()

Returns an action that presses the hardware menu key.

按menukey

 

9、replaceText(String stringToBeSet)

Returns an action that updates the text attribute of aview.

 

替换文本

 

10、scrollTo()

Returns an action that scrolls to the view.

目前不好使

 

11、swipeLeft()

Returns an action that performs a swipe right-to-leftacross the vertical center of the view.

向左滑动

 

12、swipeRight()

Returns an action that performs a swipe left-to-rightacross the vertical center of the view.

向右滑动

 

13、 typeText(String stringToBeTyped)

Returns an action that selects the view (by clicking onit) and types the provided string into the view.

输入text




0 0
原创粉丝点击