Android测试三:Uiautomator封装
来源:互联网 发布:酷安软件下载 编辑:程序博客网 时间:2024/05/19 22:59
Uiautomator封装的具体类:
import android.os.Environment;import android.support.test.uiautomator.UiAutomatorTestCase;import android.support.test.uiautomator.UiDevice;import android.support.test.uiautomator.UiObject;import android.support.test.uiautomator.UiObjectNotFoundException;import android.support.test.uiautomator.UiSelector;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.Calendar;public class UiautomatorAssistant extends UiAutomatorTestCase {private UiDevice uiDevice;private String m_logpathString = Environment.getExternalStorageDirectory().getPath()+"/PerformanceLog.txt";private final int CLICK_ID = 2000;private final int CLICK_TEXT = 2001;public UiautomatorAssistant(UiDevice uiDevice) {this.uiDevice = uiDevice;}// 通过id来进行点击操作public boolean ClickById(String id) {return ClickByInfo(CLICK_ID, id);}// 通过text来进行点击操作public boolean ClickByText(String text) {return ClickByInfo(CLICK_TEXT, text);}public void setText(String id,String text){UiObject uiObject = uiDevice.findObject(new UiSelector().resourceId(id));try {uiObject.clearTextField();uiObject.setText(text);} catch (UiObjectNotFoundException e) {e.printStackTrace();}}private boolean ClickByInfo(int CLICK, String str) {UiSelector uiselector = null;// switch根据不同的CLICK标识,创建出UiSelector的对象switch (CLICK) {case CLICK_ID:uiselector = new UiSelector().resourceId(str);break;case CLICK_TEXT:uiselector = new UiSelector().text(str);break;default:return false;}// 根据UiSelector对象构造出UiObject的对象UiObject uiobject = new UiObject(uiselector);// 判断该控件是否存在int i = 0;while (!uiobject.exists() && i < 5) {SolveProblems();sleep(500);if (i == 4) {TakeScreen(str + "-not-find");return false;}i++;}// 点击try {UiAutomationLog("click type:" + CLICK + " content:" + str);uiobject.click();} catch (UiObjectNotFoundException e) {e.printStackTrace();}return true;}/* 当进不下去的时候,使用该方法,例如可能是出现了一些对话框遮挡,该方法会把对话框干掉 */private void SolveProblems() {}/* 保存屏幕截图参数descrip 为 描述该截图的内容 */private void TakeScreen(String descrip) {Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());String datestr = calendar.get(Calendar.HOUR_OF_DAY) + "_" + calendar.get(Calendar.MINUTE) + "_" + calendar.get(Calendar.SECOND);File files = new File(Environment.getExternalStorageDirectory().getPath()+"/" + datestr + "_" + descrip + ".jpg");UiAutomationLog("TakeScreen: " + datestr + "_" + descrip + ".jpg");uiDevice.takeScreenshot(files);}/* 打log记录在手机中 */private void UiAutomationLog(String str) {// 取得当前时间Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());String datestr = calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND) + calendar.get(Calendar.MILLISECOND) + ":";FileWriter fwlog = null;try {fwlog = new FileWriter(m_logpathString, true);fwlog.write(datestr + str + "\r\n");System.out.println(datestr + str);fwlog.flush();} catch (IOException e) {e.printStackTrace();} finally {try {if (fwlog != null) {fwlog.close();}} catch (IOException e) {e.printStackTrace();}}}}
具体使用方法:
import java.io.IOException;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 Test_wechat extends UiAutomatorTestCase{ UiautomatorAssistant uiautomatorAssistant ; public void testDemo() throws IOException, UiObjectNotFoundException { uiautomatorAssistant = new UiautomatorAssistant(getUiDevice()); // 启动App Runtime.getRuntime().exec("am start com.tencent.mm/com.tencent.mm.ui.LauncherUI"); sleep(3000); /*----------------------- 验证第一种小视频的打开方式------------------------------------*/ uiautomatorAssistant.ClickByText("通讯录"); // 点击一个好友 uiautomatorAssistant.ClickById("com.tencent.mm:id/gx"); // 点击发消息 uiautomatorAssistant.ClickByText("发消息"); // 点击发送栏的“+” uiautomatorAssistant.ClickById("com.tencent.mm:id/wm"); // 点击小视频 uiautomatorAssistant.ClickByText("小视频"); // 验证第一种小视频打开方式 UiObject obj_anzhupaiObject = new UiObject(new UiSelector().text("按住拍")); if (obj_anzhupaiObject.exists()){ uiautomatorAssistant.UiAutomationLog( "第一次进入小视频的方法测试通过"); }else { uiautomatorAssistant.TakeScreen("第一次进入小视频的方法测试不通过"); } } }
阅读全文
0 0
- Android测试三:Uiautomator封装
- 使用uiautomator的python封装进行android的UI测试
- 使用uiautomator的python封装进行android的UI测试
- Android自动化测试之UIAutomator(三)---比对测试截图
- android uiautomator自动化测试
- UiAutomator android自动化测试
- android ui测试uiautomator
- Android-自动化测试UIAutomator
- Android自动化测试uiautomator
- android studio测试--Uiautomator
- Android UiAutoMator自动化测试
- Android Uiautomator 自动化测试
- Android Uiautomator 自动化测试
- android自动化测试Uiautomator源码分析之三
- Android自动化测试之UIAutomator
- Android自动化测试(UiAutomator)
- Android自动化测试工具uiautomator
- Python + Android + Uiautomator自动化测试
- Hdfs存储负载均衡
- es6的核心
- 常用操作或者常见bug积累
- POJ-3683 Priest John's Busiest Day(2-SAT问题)
- App测试中ios和Android的区别
- Android测试三:Uiautomator封装
- studio中项目出错的问题小汇总
- 基于Swift的iOS应用程序开发:使用NSUserDefaults存储本地数据
- Scrapy-pipelines的使用
- Mongo Pub/Sub
- Pycharm教程(2)——代码风格
- 验证码调用和web中字节字符流的解释
- java设计模式——策略模式
- java新项目保存页面提示GBK编码,无法保存