uiautomator应用示例——测试便签/记事本(note)

来源:互联网 发布:unity3d 增强现实 编辑:程序博客网 时间:2024/05/13 01:49

前段时间用到了uiautomator,完了之后就没再继续把关于自己学习到的关于uiautomator的东西整理出来,这不是一种好习惯。今天得空,这里给出一个uiautomator使用的例子。

前面提到,在uiautomator中每一个想要模拟操作的ui控件都是UiObject的对象,UiColletion和UiScollable都是UiOject的子类。



UiSelector用于条件筛选,根据text,description,packegeName,index还有状态信息等来取出符合条件的控件。其对象总是作为UiObject的构造噐参数。需要注意的是,官网中对于UiObjectNotFoundException,提到“Generated in test runs when aUiSelector selector could not be matched to any UI element displayed.”,即该异常在UiSelector没有任何匹配的时候抛出。对于如下代码

UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
即使没有找到text为Apps的控件,也没有抛出异常,而是在调用appsTab方法(如appsTab.click())时才回抛出。因为
    public UiObject(UiSelector selector) {        mSelector = selector;    }    public boolean click() throws UiObjectNotFoundException {           .......    }
这时候可以UiObject.exists()来判断该对象是否存在。



下面是“便签”应用新建,删除,修改操作测试的示例

public class NoteOperationTestCase extends UiAutomatorTestCase {    UiObject mSelectedItem;//当前操作对象    UiScrollable mUiScrollable;//这里是ListView对象    UiDevice mDevice;//设备对象    UiObject mBu_del;//确定按钮    UiObject mBu_del_confirm;//取消按钮    public void testDemo() throws UiObjectNotFoundException {        mDevice = getUiDevice();        mDevice.pressHome();//按下home键,返回桌面        UiObject allAppTextView = new UiObject(                new UiSelector().description("Apps"));        allAppTextView.clickAndWaitForNewWindow();//找到应用程序按钮按下        UiObject appsTab = new UiObject(new UiSelector().text("Apps"));        appsTab.click();//切换到应用程序        UiObject appViews = new UiObject(                new UiSelector().scrollable(true));                UiObject noteApp;        noteApp=appViews.getChild(new UiSelector().text("Note"));              while(!noteApp.exists()){              appViews.swipeLeft(30);//如果当前界面没有Note,则滑动翻页。参数代表滑动速度              noteApp=appViews.getChild(new UiSelector().text("Note"));      }        noteApp.clickAndWaitForNewWindow();//进入Note应用        UiObject noteValidation = new UiObject(                new UiSelector().packageName("com.XXXX.note"));        assertTrue("Unable to detect noteApp", noteValidation.exists());//判断是否是指定包名的应用        // 新建两条记录        newRecord();        newRecord();        // 修改记录        editRecord();        // 删除记录,长按删除          delNote_longClick();        // 菜单删除          delNote_menu();                    mDevice.pressBack();    }    private void editRecord() throws UiObjectNotFoundException {        mSelectedItem = getRandUiRecord();        mSelectedItem.clickAndWaitForNewWindow();        mSelectedItem = new UiObject(                new UiSelector().className("android.widget.EditText"));        mSelectedItem.setText("modified text");//调用软键盘输入字串        finishOperate();    }    private void finishOperate() {
        //第一次按Back是关闭软键盘,第二次是退出保存
        mDevice.pressBack();
        mDevice.pressBack();
    }

    private void newRecord() throws UiObjectNotFoundException {
        UiObject newRecord = new UiObject(new UiSelector().description("New Note"));//根据找到新建按钮description
        newRecord.clickAndWaitForNewWindow();
        // 输入内容
        mSelectedItem = new UiObject(
                new UiSelector().className("android.widget.EditText"));
        mSelectedItem.setText("new text");//调用软键盘输入字串
        finishOperate();
    }

    private UiObject getRandUiRecord() throws UiObjectNotFoundException {
        mUiScrollable = new UiScrollable(
                new UiSelector().className("android.widget.ListView"));//获得记录列表
        int totalCount = mUiScrollable.getChildCount();
        int editRecordIndex = (int) (Math.random() * (totalCount - 1));
        Log.i("NOTE", "totalCount = "+totalCount+" editRecordIndex = "+editRecordIndex);
        mUiScrollable.setAsVerticalList();//垂直滑动
        UiObject mSelectedItem = mUiScrollable.getChildByInstance(
                new UiSelector().className("android.widget.RelativeLayout"),
                editRecordIndex);
        return mSelectedItem;
    }
    

    //长按删除
    private void delNote_longClick() throws UiObjectNotFoundException {
      mSelectedItem = getRandUiRecord();
      mSelectedItem.longClick();
      mBu_del = new UiObject(new UiSelector().text("Delete"));
      mBu_del.click();
      
      mBu_del_confirm = new UiObject(new UiSelector().text("OK"));
      mBu_del_confirm.click();//点击确定
    }
    

    //菜单删除
    private void delNote_menu() throws UiObjectNotFoundException{
        mDevice.pressMenu();
        UiObject list_options = new UiObject(
                new UiSelector().className("android.widget.ListView"));
        mBu_del = list_options.getChild(new UiSelector().index(1));//列表中下标为1的是删除
        mBu_del.clickAndWaitForNewWindow();

        mSelectedItem = getRandUiRecord();

        mSelectedItem.click();

        UiObject bu_del_finish = new UiObject(
                new UiSelector().description("Ok"));
        bu_del_finish.click();

        mBu_del_confirm = new UiObject(new UiSelector().text("OK"));
        mBu_del_confirm.clickAndWaitForNewWindow();
    }

}
                                             
0 0
原创粉丝点击