您的位置:首页 > 产品设计 > UI/UE

Android UiAutomator 初试。

2016-03-14 17:10 369 查看
今天自己看了一下google的UiAutomator,尝试写了一个简单的例子,进行记录。

用eclipse创建一个新的java工程,添加引用 jUnit,然后添加相应的sdk/platforms下的android.jar,和uiautomator.jar到工程的外部引用jar中。

下面就是写用例了。我对自己手机上的计算器写了一步测试用例。剩下的脑补了。

import android.util.Log;

import com.android.uiautomator.core.UiCollection;
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 extends UiAutomatorTestCase{

private UiObject buttonPlus;
private UiObject buttonMinus;
private UiObject buttonMult;
private UiObject buttonDiv;
private UiObject buttonEqual;
private UiObject buttonClear;
private UiObject buttonDel;
private UiObject buttonDot;
private UiObject button1;
private UiObject button2;
private UiObject button3;
private UiObject button4;
private UiObject button5;
private UiObject button6;
private UiObject button7;
private UiObject button8;
private UiObject button9;
private UiObject button0;

public void testDemo() throws Exception {
// if run app scuess.
if(Init()){

// init the frameLayout and buttons.
InitButton();

// plus test once
TestPlus();
// minus test

// mul test

// div test
}else{

}

}

public boolean Init() throws UiObjectNotFoundException{
/**
* find the calcultor and run it
*/
UiObject appItem = new UiObject(new UiSelector().className("android.widget.FrameLayout")
.instance(1).childSelector(new UiSelector().description("系统工具")));

if(appItem.click()){
this.sleep(1000);
UiCollection app = new UiCollection(new UiSelector().className("android.widget.FrameLayout")
.index(1).childSelector(new UiSelector().description("计算器")));
app.clickAndWaitForNewWindow();
Log.d("Init", "Now it's running OK.App start scuess!");
return true;
}else{
return false;
}
}

public void InitButton() throws UiObjectNotFoundException{
buttonPlus = new UiObject(new UiSelector().text("+"));
buttonMinus = new UiObject(new UiSelector().text("-"));
buttonMult = new UiObject(new UiSelector().text("×"));
buttonDiv = new UiObject(new UiSelector().text("÷"));
buttonEqual = new UiObject(new UiSelector().description("等于"));
buttonClear = new UiObject(new UiSelector().description("清除"));
buttonDel = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/del"));
buttonDot = new UiObject(new UiSelector().description("小数点"));
button1 = new UiObject(new UiSelector().text("1"));
button2 = new UiObject(new UiSelector().text("2"));
button3 = new UiObject(new UiSelector().text("3"));
button4 = new UiObject(new UiSelector().text("4"));
button5 = new UiObject(new UiSelector().text("5"));
button6 = new UiObject(new UiSelector().text("6"));
button7 = new UiObject(new UiSelector().text("7"));
button8 = new UiObject(new UiSelector().text("8"));
button9 = new UiObject(new UiSelector().text("9"));
button0 = new UiObject(new UiSelector().text("0"));
Log.d("InitButton", "All buttons had been found.");
}

public void TestPlus() throws UiObjectNotFoundException{
String num = String.valueOf((int)(Math.random() * 100));
String num2 = String.valueOf((int)(Math.random() * 100));

for (int i = 0;i<num.length();i++){
KeyPress(num.charAt(i));
this.sleep(600);
}
KeyPress('+');
for (int j = 0;j<num2.length();j++){
KeyPress(num2.charAt(j));
this.sleep(600);
}
KeyPress('=');

int num_int = Integer.valueOf(num);
int num2_int = Integer.valueOf(num2);
UiObject sum = new UiObject(new UiSelector().className("android.widget.EditText"));
int result = Integer.valueOf(sum.getText());
assertEquals(num_int + num2_int,result);
this.sleep(1000);
KeyPress('c');
}

public void KeyPress(char operation) throws UiObjectNotFoundException{
switch(operation){
case '+':
buttonPlus.click();
break;
case '-':
buttonMinus.click();
break;
case 'x':
buttonMult.click();
break;
case '÷':
buttonDiv.click();
break;
case '=':
buttonEqual.click();
break;
case '.':
buttonDot.click();
break;
case '1':
button1.click();
break;
case '2':
button2.click();
break;
case '3':
button3.click();
break;
case '4':
button4.click();
break;
case '5':
button5.click();
break;
case '6':
button6.click();
break;
case '7':
button7.click();
break;
case '8':
button8.click();
break;
case '9':
button9.click();
break;
case '0':
button0.click();
break;
case 'c':
buttonClear.click();
break;
case 'd':
buttonDel.click();
break;
default:
}
this.sleep(500);
}
}


本来是打算把测试用例写在文件里,然后通过程序读进来 ,进行执行,但是发现打包后,文件不知道如何打入到.jar包中,所以随便写了一个随机。

编译过程是这样的,把sdk的环境变量配置好的直接可以用android create uitest-project -n 你的工程名字 -t id -p 工程目录

-t如果不清楚有哪些,可以使用android list来查看。build通过后会在工程中出现build.xml 然后把default改成build,右键run as ant build就可以了。

然后就是把.jar文件推送到手机上。adb push xxx.jar  /xx/xx/ 运行 adb shell uiautomator runtest xxx.jar -c xx.xx.xx.xx.youtest

如果没有包名就直接写你的主类名字就好了。然后就可以看到结果了。ENJOY!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uiautomator android