今天我们讲讲UiAutomator自动化测试的步骤
我使用的工具是eclipse 下面就ecpilse和Android studio这两种开发工具写一个测试脚本
主要功能呢是获取支付宝付款码截取二维码部分并且保存到手机SD卡。
eclipse
环境的搭建简易搭建去百度一下哦!网址给大家贴出来http://blog.csdn.net/whorus1/article/details/51076062?locationNum=10
OK下面开始啦
新建一个类继承UiAutomatorTestCase
public class mytake extends UiAutomatorTestCase{
private String mybecase;
private String imgname;
public void testDemo() throws UiObjectNotFoundException, RemoteException {
if(!UiDevice.getInstance().isScreenOn()){ //判断屏幕是否亮起
UiDevice.getInstance().wakeUp(); //唤醒屏幕
}
getUiDevice().pressHome(); //回到主桌面
UiObject aplayObject =new UiObject(new UiSelector().text("支付宝")); //寻找支付宝
for(int i=1;i<10;i++){ //循环10次查找(可以考虑写一个while循环)
if(aplayObject.exists()){ //判断当前视图上是否有支付宝
aplayObject.click(); //点击支付宝图片
break; //循环结束
}else {
getUiDevice().swipe(1032, 1248, 0, 1248, 10); //滑动屏幕
}
}
for(int i=1;i<=10;i++){ //金额
for(int j=1; j<=10; j++){ //该金额你想要多少张
System.out.println("("+i+")"+j);
UiObject loginObject=new UiObject(new UiSelector().resourceId("com.alipay.mobile.base.commonbiz:id/launcher_title_search_item_button"));
//查找支付宝右上角的加号这个方法是根据ID查找
if(loginObject.exists()){
loginObject.click(); //点击支付宝右上角的加号
sleep(1000); //等1秒,睡眠一秒
}
UiObject textObject=new UiObject(new UiSelector().text("我要收款")); //查找支付宝右上角的加号(根据text查找)
if(textObject.exists()){
textObject.click();
sleep(1000);
}
UiObject setmoney=new UiObject(new UiSelector().text("设置金额"));//查找设置金额按钮(根据text查找)
if(setmoney.exists()){
setmoney.click();
sleep(1000);
}
UiObject moneyObject=new UiObject(new UiSelector().resourceId("com.alipay.mobile.ui:id/content"));//查找设置金额输入框(根据id查找)
if(moneyObject.exists()){
moneyObject.setText(String.valueOf(i)); //设置输入框的输入值
}
UiObject becase=new UiObject(new UiSelector().resourceId("com.alipay.mobile.payee:id/payee_QRAddBeiZhuLink"));
if(becase.exists()){
becase.click();
}
UiObject edtbecase=new UiObject(new UiSelector().text("收款理由"));
if(edtbecase.exists()){
edtbecase.setText(String.valueOf(j));
}
UiObject sureObject=new UiObject(new UiSelector().text("确定"));
if(sureObject.exists()){
sureObject.click();
sleep(2000);
}
if(UiDevice.getInstance().takeScreenshot(new File("/sdcard/test"+i+"-"+j+".png"))){ //截取屏幕
Bitmap bit1 = BitmapFactory.decodeFile("/sdcard/test"+i+"-"+j+".png");
Bitmap bit2=jietu.centerSquareScaleBitmap(bit1, 1000);//截图
upimg.saveMyBitmap(bit2, "jietu"+i+j);
//保存截取的图片
delete.deleteimg("/sdcard/test"+i+"-"+j+".png");//删除之前的截取屏幕的图片
UiObject backObject=new UiObject(new UiSelector().resourceId("com.alipay.mobile.ui:id/title_bar_back_button"));
if(backObject.exists()){
backObject.click();
}
}else {
break;
}
}
}
}
}
这样就能获取到截取的二维码图片了
截图的方法
public class jietu {
public static Bitmap centerSquareScaleBitmap(Bitmap bitmap, int edgeLength){
if(null == bitmap || edgeLength <= 0){
return null;
}
Bitmap result = bitmap;
int widthOrg = bitmap.getWidth();
int heightOrg = bitmap.getHeight();
/* if(widthOrg > edgeLength && heightOrg > edgeLength)
{*/
//压缩到一个最小长度是edgeLength的bitmap
int longerEdge = (int)(edgeLength * Math.max(widthOrg, heightOrg) / Math.min(widthOrg, heightOrg));
int scaledWidth = widthOrg > heightOrg ? longerEdge : edgeLength;
int scaledHeight = widthOrg > heightOrg ? edgeLength : longerEdge;
Bitmap scaledBitmap;
try{
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
}
catch(Exception e){
return null;
}
//从图中截取正中间的正方形部分。
int xTopLeft = (scaledWidth - edgeLength) / 2;
int yTopLeft = (scaledHeight - edgeLength-600) / 2;
try{
result = Bitmap.createBitmap(scaledBitmap, 200, 400, 600, 600);
scaledBitmap.recycle();
}
catch(Exception e){
return null;
}
/* } */
return result;
}
}
保存截图到指定路径
public class upimg {
public static void saveMyBitmap(Bitmap mBitmap,String bitName) {
File f = new File( "/sdcard/ewmtp/"+bitName + ".jpg");
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
删除截图
public class delete {
public static void deleteimg(String path){
File file=new File(path);
file.delete();
}
}
好的,这个时候,整个脚本就写完了
这个时候就可以测试啦
按住Windows键+R,输入cmd进入doc命令
输入
android create uitest-project -n yajdjb-t 1 -p C:\Users\Administrator\workspace\aaaaa
yajdjb是打出的jar的名字,随意起,C:\Users\Administrator\workspace\aaaaa是项目路径
cd C:\Users\Administrator\workspace\aaaaa
找到该文件C:\Users\Administrator\workspace\aaaaa是项目路径
ant build
成功后可以看道jar输出的路径,如果没有成功就是android create uitest-project -n yajdjb-t 1 -p C:\Users\Administrator\workspace\aaaaa命令中的1错了,对照上面给的网址好好检查。
adb push C:\Users\Administrator\workspace\aaaaa\bin\yajdjb.jar data/local/tmp
C:\Users\Administrator\workspace\aaaaa\bin\yajdjb.jar是上面的路径
adb shell uiautomator runtest yajdjb.jar -c test.mytake
test.mytake是包名加类名
好的,插上手机就可以运行了!
就可以看到手机按照脚本一步步的执行了
下面是Android studio的步骤
自己去这个网址看http://blog.csdn.net/cxq234843654/article/details/52605441
不想写了,步骤差不多
就是一些方法不是这样用的了。
代码懒得找了,自己百度改改方法就OK了,总体来说还是很简单的。