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