码迷,mamicode.com
首页 > 移动开发 > 详细

Android无线测试之—UiAutomator UiObject API介绍四

时间:2015-06-11 18:28:06      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

输入文本与清除文本

一、输入文本与清除文本相关API

返回值 API 描述
boolean setText(String test) 在对象中输入文本
void clearTextField() 清除编辑框中的文本

 

 

 

 

 

二、输入文本与清除文本实现步骤说明

1)输入文本:清除文本—>输入文本

2)清除文本:长按—>清除文本

三、API应用举例

技术分享
package com.test.uiobject;

import android.view.KeyEvent;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class Demo extends UiAutomatorTestCase {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String jarName,testClass,testName,androidId;
        jarName="demo";
        testClass="com.test.uiobject.Demo";
        testName="testSetTextAndClearText";
        androidId="1";
        new UiAutomatorHelper(jarName,testClass,testName,androidId);

    }
    
    public void testSetTextAndClearText() throws UiObjectNotFoundException{
        UiDevice.getInstance().pressHome();
        sleep(2000);
        UiObject message=new UiObject(new UiSelector().text("Messaging"));
        message.clickAndWaitForNewWindow();
        UiObject createMessage=new UiObject(new UiSelector().resourceId("com.android.mms:id/action_compose_new"));
        createMessage.clickAndWaitForNewWindow();
        
        //输入信息内容
        UiObject typeMessage=new UiObject(new UiSelector().resourceId("com.android.mms:id/embedded_text_editor"));
        typeMessage.setText("hello, my name is fsw!");
        sleep(5000);
        typeMessage.clearTextField();
        
        //通讯地址处这样删除会有问题,需要另外一种方法删除
        UiObject address=new UiObject(new UiSelector().resourceId("com.android.mms:id/recipients_editor"));

        //光标移动到末尾,点击backspace键删除
        address.setText("15288810187");
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_END);
        while(address.getText() != "") {
            UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_DEL);
        }
        
        //光标移动到开始,使用delete见删除
        address.setText("15288810187");
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_HOME);
        while(address.getText() != "") {
            UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_FORWARD_DEL);
        }
        
    }

}
Demo.java

 

Android无线测试之—UiAutomator UiObject API介绍四

标签:

原文地址:http://www.cnblogs.com/fsw-blog/p/4567291.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!