关于Uiautomator的学习历程(1)
来源:互联网 发布:mac的office 编辑:程序博客网 时间:2024/06/05 22:52
话不多说,代码先贴上。反正我备注的都很详细啦。。。关于人生中的第一段代码。(BaseTest是android开发写的)
package com.example.xiaofeng.uiautotest;import android.content.Context;import android.content.Intent;import android.os.RemoteException;import android.support.test.InstrumentationRegistry;import android.support.test.uiautomator.By;import android.support.test.uiautomator.BySelector;import android.support.test.uiautomator.UiDevice;import android.support.test.uiautomator.UiObject2;import android.support.test.uiautomator.Until;import android.widget.Button;import org.junit.Assert;import org.junit.Before;import static android.support.test.InstrumentationRegistry.getContext;/** * Created by nanfeng on 2017/4/24. */public class BaseTest { protected UiDevice mUIDevice = null; protected Context mContext = null;//Context是android中上下文 String APP = "com.maihaoche.bentley.debug"; // String BASE_ID = "com.maihaoche.bentley.debug:id/" ; String BASE_ID2 = "com.maihaoche.bentley.debug" ; int displayTimeOut = 5 * 2000 ; //默认的界面刷新超时时间 String userPhone = "18868816760" ; String password = "111111" ; @Before public void setUp() throws RemoteException { mUIDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); //获得device对象 mContext = getContext();// APP = getLauncherPackageName() ; if(!mUIDevice.isScreenOn()){ //判断屏幕是否唤醒状态,如果非,唤醒屏幕 mUIDevice.wakeUp(); } mUIDevice.pressHome(); //因为可能会处于其他的应用中,所有先按home键 } protected void waitUpdate(){ //等待界面渲染 mUIDevice.waitForWindowUpdate(APP,displayTimeOut) ; } public void openApp(String packageName) { //打开APP Context context = InstrumentationRegistry.getInstrumentation().getContext(); Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(intent); } /** * 第一次进APP,走引导页时的逻辑 */ protected void firstEntrance() { new Thread(){ @Override public void run() { super.run(); UiObject2 powerView = mUIDevice.wait(Until.findObject(By.clazz(Button.class).text("允许")), 20000); if (powerView != null) { powerView.click(); //允许打电话权限 } } }.start(); UiObject2 skipButton = getUiByText("跳过",5 * 1000); //从页面获取"跳过"按钮 if (skipButton != null){ //如果页面存在"跳过"按钮,则需要跳过引导页,然后登陆 skipButton.click(); //点击跳过按钮 getUiByText("登录").click(); //点击登陆按钮 getUiById("login_phone_txt").setText(userPhone); getUiById("login_password").setText(password); //录入登录名和密码// UiObject2 loginButton = getUiByClass(Button.class); UiObject2 loginButton = getUiById("login_login",3000) ; //获取登陆按钮,等待3s Assert.assertNotNull("没有拿到登录按钮",loginButton); //没有拿到登陆按钮,抛出断言和异常 loginButton.click();// getUiById("login_login").click(); //登录 } } int delayTime = 10 * 1000 ; //默认等待获取View的时间 public UiObject2 getUiById(String id){ return mUIDevice.wait(Until.findObject(By.res(BASE_ID2, id)), delayTime); } public UiObject2 getUiById(String id, int waitTime){ return mUIDevice.wait(Until.findObject(By.res(BASE_ID2, id)), waitTime); } public UiObject2 getUiByText(String text){ return mUIDevice.wait(Until.findObject(By.text(text)), delayTime); } public UiObject2 getUiByText(String text, int waiTime){ return mUIDevice.wait(Until.findObject(By.text(text)), waiTime); } public UiObject2 getUiByClass(Class uiClass){ return mUIDevice.wait(Until.findObject(By.clazz(uiClass)),delayTime); } /*---------------------------待斟酌的方法--------------------------*/ public UiObject2 findObject(BySelector selector) throws InterruptedException { UiObject2 object = null; int timeout = 30000; int delay = 1000; long time = System.currentTimeMillis(); while (object == null) { object = mUIDevice.findObject(selector);// Thread.sleep(delay); if (System.currentTimeMillis() - timeout > time) { break; } } return object; }// private UiObject getUiById(String id){// return mUIDevice.findObject(new UiSelector().resourceId(BASE_ID + id));// }//// private UiObject getUiByText(String text){// return mUIDevice.findObject(new UiSelector().text(text));// }//// private String getLauncherPackageName() {// // Create launcher Intent// final Intent intent = new Intent(Intent.ACTION_MAIN);// intent.addCategory(Intent.CATEGORY_HOME);//// // Use PackageManager to get the launcher package name// PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();// ResolveInfo resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);// return resolveInfo.activityInfo.packageName;// }}
package com.example.xiaofeng.uiautotest;import android.content.Context;import android.support.test.espresso.InjectEventSecurityException;import android.support.test.runner.AndroidJUnit4;import android.support.test.uiautomator.By;import android.support.test.uiautomator.Configurator;import android.support.test.uiautomator.SearchCondition;import android.support.test.uiautomator.StaleObjectException;import android.support.test.uiautomator.UiObject2;import android.support.test.uiautomator.UiObjectNotFoundException;import android.support.test.uiautomator.Until;import android.util.Log;import android.view.WindowManager;import android.widget.Button;import android.widget.TextView;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import java.util.List;import java.util.Random;/** * Created by datou on 17/4/26.用于创建纯物流运单 */@RunWith(AndroidJUnit4.class)public class WuLiuCase extends BaseTest { //Base是一个封装了一部分功能的代码,继承后就可以直接引用其中的方法啦 @Test public void test1() throws UiObjectNotFoundException,InjectEventSecurityException{ openApp(APP); //打开app waitUpdate(); //等待页面渲染 Configurator.getInstance().setActionAcknowledgmentTimeout(2000).setKeyInjectionDelay(200); /*--------------第一次进入APP需要通过引导页-----------*/ firstEntrance(); /*---------------点击物流服务-----------------------*/ getUiByText("物流服务",5000).click(); /*---------------获取是否有手机弹框提示获取用户对应权限,有,选择允许--------------*/ UiObject2 ok = mUIDevice.findObject(By.clazz(Button.class).textContains("允许")); if (ok != null){ mUIDevice.wait(Until.findObject(By.clazz(Button.class).textContains("允许")),2000).click(); } getUiByText("始发城市").click(); // getUiByText("浙江省",2000).click(); getUiByText("杭州市",2000).click(); getUiByText("目的城市").click(); // getUiByText("安徽省",2000).click(); getUiByText("合肥市",2000).click(); addCarInfo(); //调用添加车辆信息 getUiByText("查询运价").click(); //点击查询运价 /*------------进入询价结果页面啦----------------*/ downOrder(); //调用下单 getUiByText("发运时间").click();//选择发运时间 getUiByText("完成").click(); //在弹出的时间选择框中点击完成按钮,因为其他的都比较难,哈哈 getUiById("ed_name").setText("大头");//录入发车人姓名 getUiById("ed_tel").setText("15212099082");//录入发车人手机号码 /*------------此时需要滑动屏幕---------*/ int height = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight() ; mUIDevice.swipe(0,height - 100,0,0,100) ; //swip方法是整个屏幕滑动的方法,里面的五个参数分别是起始点的坐标和终点的坐标以及滑动的速度。因为x坐标是相对不动的,所以相对坐标都选为0 //起始点的y轴坐标是因为y轴如果从屏幕的底端"height"开始滑动可能会导致屏幕滑动出很多手机的进程等内容,所以写成"height-100"表示屏幕底端向上一点开始滑动,滑动至屏幕顶端 getUiById("ed_detail").setText("我是提车验车详细地址"); //录入运单详细地址信息,但是为了防止每次都一致,可以在固定的文本后添加随机数 int index = new Random().nextInt(10000) ;//Random是java中获取随机数的方法,nextInt中表示的是随机数为0-10000中的某个数 String flagStr = "我是android自动化测试跑出来的纯物流单子" + index ; //把备注固定为一个模版,然后定义/声明一个对象flagStr去引用它 getUiById("ed_notice").setText(flagStr); //把前几行的这个提车详细地址和随机数的文本全部塞进详细地址这个字段 getUiByText("提交运单").click();//点击提交运单按钮// mUIDevice.wait(Until.findObject(By.clazz(Button.class).textContains("确认")),2000).click(); //确认提交运单 getUiByText("确认").click();//在弹出的对话框中确定提交运单,进入运单待付款页面 String orderId = getUiById("tv_orderid").getText();//获取提交运单后的待付款页面的本运单id,(等于号后面的意思是定位运单id的位置,获取这个id的值;等于号前面是把这个获取的订单id声明为一个新的对象,方便后面调用) /*--------下面是进入我的运单列表页面----------*/ getUiById("left_btn").click(); //点击页面左上角返回按钮 getUiByText("我的运单").click(); //点击右上角"我的运单"按钮,进入我的运单列表页面// mUIDevice.swipe(0,100,0,400,50) ; //下面是通过前面获取的待付款页面的运单id在我的运单列表页面进行查找又没有对应一致的运单,如果有,即这个bytext值不为空,代表前面的运单创建成功// UiObject2 byText = getUiByText(orderId,5 * 1000);// Assert.assertNull("没有找到匹配的订单",byText);//断言这个运单没有查找到,则打印此提示信息// UiObject2 order = getUiById("list").findObject(By.desc("运单编号"));// Assert.assertNull("没有拿到对象",order);//// UiObject2 order = getUiById("tv_id");// Assert.assertFalse("没有找到匹配的订单",order != null && order.getText().equals(orderId)); /*----------为了查看最后运转到那个页面,所以设置在这个页面停留10s中--------------*/ try { Thread.sleep(10 * 1000); //线程睡眠10s } catch (InterruptedException e) { e.printStackTrace(); //如果没有停留10s中就打印抛出的异常 } } /** * 下单方法 递归(因为这个方法在循环里还要被重复用到,所以封装一下,封装的过程是:选中要封装的代码段,右键-refactor-extract-method-录入方法名(首字母小写)-点击OK。封装成功) */ private void downOrder() { UiObject2 xiadan = getUiByText("下单");//获取对象(因为后面要对这个对象进行其他操作,所以可以进行获取,获取的步骤是:选中要获取的对象-点击"alt+return"-Introduce...-enter-给对象重命名-enter,对象获取成功。) if (xiadan != null) { xiadan.click(); }else { getUiById("left_btn").click(); //返回上级页面 getUiById("btn_delete").click(); //删除添加的车辆 getUiById("btn_ok").click(); // 确认删除 addCarInfo(); //继续添加车辆 getUiByText("查询运价").click(); //点击查询运价 downOrder(); } } //添加车辆信息等方法 private void addCarInfo() { getUiByText("添加车辆信息").click(); getUiById("item_type").click(); getUiByText("中规/国产",3000).click(); getUiByText("奥迪",2000).click(); getRandomUiFromList("recycler_choose",TextView.class).click(); //随机选择车系 getCarTypeClick(); getUiById("ed_car_num").setText("1"); //输入车辆数量为1辆 getUiByText("确认添加",3000).click(); //确认添加 } private void getCarTypeClick(){ UiObject2 carType = getRandomUiFromList("recycler_choose", TextView.class);//随机选择车型 if (carType.getText().endsWith("自定义")){ getCarTypeClick(); //如果获取的车型中存着文字"自定义",重新获取车型 }else { carType.click(); } } private UiObject2 getRandomUiFromList(String listId,Class childClass){ List<UiObject2> uiList ; try { uiList = getUiById(listId).findObjects(By.clazz(childClass)); int index = new Random().nextInt(uiList.size() -1 ) ; uiList.get(index) ; return uiList.get(index) ;// }catch (StaleObjectException e) { }catch (Exception e) {// UiObject2 okButton = mUIDevice.findObject(By.clazz(Button.class.getName()).text("确定"));// if(okButton != null){//// UiObject2 okButton = mUIDevice.findObject(By.clazz(Button.class.getName()).text("确定"));// okButton.click();// waitUpdate();// } e.printStackTrace(); //打印异常 return getUiById(listId).findObject(By.clazz(childClass));// getRandomUiFromList(listId,childClass); } }}
0 0
- 关于Uiautomator的学习历程(1)
- 关于Uiautomator的学习历程(2)
- uiautomator学习1-配置
- 【转载】关于模拟电路的学习历程
- 关于UIAutomator 滚动的steps
- TypeScript的学习历程1
- 关于一个人创建网站的历程(1)
- DM8148学习历程(1)
- Struts2 学习历程(1)
- 学习历程2016522(1)
- NodeJS学习历程(1)
- C++学习历程(1)
- ionic3学习历程(1)
- UIAutomator学习笔记V0.1
- android uiautomator学习(二)主要类的简单介绍
- android uiautomator学习(二)主要类的简单介绍
- android uiautomator学习(二)主要类的简单介绍 .
- 小Insect的VB学习历程(1)。
- 啥是工具链和GNU工具链
- Ubuntu 提取CHM并转PDF的方法
- 【学习笔记】java面向对象复习01
- 正则表达式之基本概念
- wakelock机制
- 关于Uiautomator的学习历程(1)
- YUM工具介绍
- 矩阵——bzoj1898: [Zjoi2005]Swamp 沼泽鳄鱼
- elasticsearch的基本用法
- FORTRAN中の时间函数
- RabbitMQ
- 遍历SD卡或者具体文件夹下面具体的文件的实现
- 关于spi flash芯片m25p80驱动以及其简单的mtd驱动分析
- 用JS将一个升序数组转换成一个二叉排序树