UIAutomator的讲解:
首先了解这个工具的初始化查看相应的内容的UI的层次结构。
在android的sdk的子目录下的tools中打开,UIAutomatorviewer打开这个应用程序。
链接上手机以后可以常看到相应的图片如下:
然后就是相应的adb 和 automator的命令行的使用,因为automator是一个命令行的工具。
所以我们需要链接上手机之后,测试脚本的可以查看相应的文档:
链接上之后, 我们使用相应的ant来进行编译,将编译之后的脚本发送到手机上,然后在命令含上控制手机上脚本的运行。
个人测试的一点脚本:
package SetLanguage;
//import Point.java;
import android.graphics.*;
import android.os.RemoteException;
import com.android.uiautomator.core.UiObject;
import
com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import
com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class Runner extends UiAutomatorTestCase {
public void testDemo() throws
UiObjectNotFoundException, RemoteException {
//开启的解锁 手机解锁
Point q0 = new Point();
Point q1 = new Point();
Point q2 = new Point();
Point q3 = new Point();
Point q4 = new Point();
q0.x = 360 ;//(360,616)
q0.y = 616 ; //
q1.x = 560 ; //(560,616)
q1.y = 616 ;
q2.x = 360 ;//(360,816)
q2.y = 816 ;
q3.x = 160 ; //(160,816)
q3.y = 816 ;
q4.x = 360 ;//(360,1016)
q4.y = 1016 ;
Point q[]= new Point[5];
q[0]=q0;
q[1]=q1;
q[2]=q2;
q[3]=q3;
q[4]=q4;
//密码解锁
Point p0 = new Point();
Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point();
Point p4 = new Point();
p0.x = 600 ;
p0.y = 500 ;
p1.x = 400 ;
p1.y = 500 ;
p2.x = 400 ;
p2.y = 800 ;
p3.x = 400 ;
p3.y = 1050 ;
p4.x = 600 ;
p4.y = 1050;
Point p[]= new Point[5];
p[0]=p0;
p[1]=p1;
p[2]=p2;
p[3]=p3;
p[4]=p4;
for(int i=0; i< 20 ;i++){
getUiDevice().wakeUp();
getUiDevice().swipe(400, 1200, 400, 500, 100);
getUiDevice().swipe(q,80);
getUiDevice().pressHome();
getUiDevice().click(600, 700);
//登录的界面的相关的内容
//count
UiObject countObj = new UiObject(new UiSelector().text("Account"));
countObj.clearTextField();//清除相应的内容
countObj.setText("a1");
//password
UiObject passwordObj = new UiObject(new UiSelector().resourceId("com.bignerdranch.android.mfailab:id/Password"));
passwordObj.setText("z");
//点击登录
getUiDevice().click(680, 1250);
UiObject loginObj= new UiObject(new UiSelector().resourceId("com.bignerdranch.android.mfailab:id/Login"));
loginObj.click();
//跳转到手势界面
UiObject warnObj= new UiObject(new UiSelector().resourceId("android:id/button1"));
warnObj.click();
//手势界面
for(int j=0 ; j< 2; j++){
getUiDevice().click(600, 500);
getUiDevice().swipe(p,40);
//若是这个界面的手势没有起到滑动的作用,需要在设置界面点击关于手机,开发者账号,重新点一下,开启location
}
//member Biz界面
UiObject scanObj = new UiObject(new UiSelector().resourceId("com.bignerdranch.android.mfailab:id/creatbitmap"));
scanObj.click();
UiObject scanBackObj = new UiObject(new UiSelector().resourceId("com.bignerdranch.android.mfailab:id/title_btn"));
scanBackObj.click();
//生成的二维码
UiObject createCodeObj = new UiObject(new UiSelector().resourceId("com.bignerdranch.android.mfailab:id/scanbitmap"));
createCodeObj.click();
UiObject createCodeObjBack = new UiObject(new UiSelector().resourceId("com.bignerdranch.android.mfailab:id/title_btn"));
createCodeObjBack.click();
//account to account
UiObject accountToObj = new UiObject(new UiSelector().text("Account to Account"));
accountToObj.click();
createCodeObjBack.click();
UiObject cashToObj = new UiObject(new UiSelector().text("Account to Cash"));
cashToObj.click();
createCodeObjBack.click();
UiObject buyCodeObj = new UiObject(new UiSelector().text("Buy Pin Code"));
buyCodeObj.click();
createCodeObjBack.click();
//切换到向印度的own界面
UiObject Own = new UiObject(new UiSelector().text("Own Biz"));
Own.click();
//own 界面
//1
UiObject loanObj = new UiObject(new UiSelector().text("Loan"));
loanObj.click();
//2
UiObject BalanceObj = new UiObject(new UiSelector().text("Balance"));
BalanceObj.click();
createCodeObjBack.click();
//3
UiObject Deposit = new UiObject(new UiSelector().text("Deposit"));
Deposit.click();
createCodeObjBack.click();
//4
UiObject Receive = new UiObject(new UiSelector().text("Receive"));
Receive.click();
createCodeObjBack.click();
//5
UiObject Exchange = new UiObject(new UiSelector().text("Exchange"));
Exchange.click();
createCodeObjBack.click();
//6
UiObject Withdraw = new UiObject(new UiSelector().text("Withdraw"));
Withdraw.click();
createCodeObjBack.click();
//点击设置
UiObject setting = new UiObject(new UiSelector().resourceId("com.bignerdranch.android.mfailab:id/title_btn_set"));
setting.click();
// //点击手势密码
UiObject gesture = new UiObject(new UiSelector().resourceId("com.bignerdranch.android.mfailab:id/gesture_password"));
gesture.click();
//弹出框
UiObject getstureChange = new UiObject(new UiSelector().text("No"));
getstureChange.click();
//点击about
UiObject about = new UiObject(new UiSelector().resourceId("com.bignerdranch.android.mfailab:id/about"));
about.click();
UiObject aboutBack = new UiObject(new UiSelector().resourceId("com.bignerdranch.android.mfailab:id/title_btn"));
aboutBack.click();
//退出
UiObject Signout = new UiObject(new UiSelector().text("Sign out"));
Signout.click();
UiObject Yes = new UiObject(new UiSelector().text("Yes"));
Yes.click();
getUiDevice().sleep();
}
}
}
总结:
1、最为主要的就是搭建好写脚本的环境,(本人是使用eclipse,而不是使用android studio);使用的框架:
2、属性相应的测试的java对象进行写相应的脚本对app的控制。
相应的UIAutomator的脚本的api的链接:
http://developer.android.com/intl/zh-cn/reference/android/support/test/uiautomator/UiDevice.html