Android无线测试之—UiAutomator UiObject API介绍四
输入文本与清除文本
一、输入文本与清除文本相关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); } } }