简历
UiAutomator是android的一种自动化测试工具, 其优点是可以对所有应用进行跨应用的操作自动化测试.
uiautomator的实现是在Android Studio应用下的androidTest目录下:
Demo
第一步:在build.gradle文件中添加uiautomator:
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'
第二步:实现具体的测试
package android.com.uiautomatortestdemo;
import android.support.test.uiautomator.UiAutomatorTestCase;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import java.io.IOException;
public class ExampleInstrumentedTest extends UiAutomatorTestCase{
private static final int longPressCameraCount = 20;
public void testDemo() throws UiObjectNotFoundException, IOException {
//testCalculator();
//testCamera();
testMyDemoApp();
}
private void testCalculator() throws UiObjectNotFoundException{
getUiDevice().pressHome();
//UiObject Calculator = new UiObject(new UiSelector().description("计算器"));
UiObject Calculator = new UiObject(new UiSelector().description("Calculator"));
Calculator.clickAndWaitForNewWindow();
//UiObject seven = new UiObject(new UiSelector().resourceId(
// "com.android.calculator2:id/digit7"));
UiObject seven = new UiObject(new UiSelector().resourceId(
"com.ape.calculator2:id/digit7"));
seven.click();
//UiObject plus = new UiObject(new UiSelector().resourceId(
// "com.android.calculator2:id/plus"));
UiObject plus = new UiObject(new UiSelector().resourceId(
"com.ape.calculator2:id/plus"));
plus.click();
//UiObject one = new UiObject(new UiSelector().resourceId(
// "com.android.calculator2:id/digit1"));
UiObject one = new UiObject(new UiSelector().resourceId(
"com.ape.calculator2:id/digit1"));
one.click();
//UiObject result = new UiObject(new UiSelector().resourceId(
// "com.android.calculator2:id/equal"));
UiObject result = new UiObject(new UiSelector().resourceId(
"com.ape.calculator2:id/equal"));
result.click();
getUiDevice().pressBack();
}
private void testCamera() throws IOException, UiObjectNotFoundException {
for(int i =0;i<longPressCameraCount;i++){
getUiDevice().pressHome();
String command = "am start -n com.myos.camera/.activity.CameraActivity";
getUiDevice().executeShellCommand(command);
sleep(1000);
UiObject photo_shutter_button_photo = new UiObject(new UiSelector().resourceId(
"com.myos.camera:id/photo_shutter_button_photo"));
//photo_shutter_button_photo.longClick();
getUiDevice().swipe(
photo_shutter_button_photo.getBounds().centerX(),
photo_shutter_button_photo.getBounds().centerY(),
photo_shutter_button_photo.getBounds().centerX(),
photo_shutter_button_photo.getBounds().centerY(),
50*8);//最后一个参数单位是5ms
getUiDevice().pressBack();
}
}
private void testMyDemoApp() throws IOException, UiObjectNotFoundException {
getUiDevice().pressHome();
String command = "am start -n android.com.debugdemo/.MainActivity";
getUiDevice().executeShellCommand(command);
sleep(2000);
UiObject testRecyclerView = new UiObject(new UiSelector().resourceId(
"android.com.debugdemo:id/testRecyclerView"));
testRecyclerView.click();
sleep(2000);
UiObject switchButton = new UiObject(new UiSelector().resourceId(
"android.com.debugdemo:id/switchButton"));
UiObject id_recyclerview = new UiObject(new UiSelector().resourceId(
"android.com.debugdemo:id/id_recyclerview"));
id_recyclerview.swipeUp(2);
sleep(2000);
switchButton.click();
id_recyclerview.swipeUp(2);
sleep(2000);
switchButton.click();
sleep(2000);
id_recyclerview.swipeUp(2);
sleep(2000);
getUiDevice().pressBack();
}
}
第三步:运行,右键你的测试类,Run
第四步:如果后期还需要运行测试用例,可以通过如下的adb命令调用:
android.com.uiautomatortestdemo.ExampleInstrumentedTest android.com.uiautomatortestdemo.test/android.support.test.runner.AndroidJUnitRunner
备注
此自动化测试经常需要uiautomatorviewer工具来配合,uiautomatorviewer的使用如下:
/SDK/tools$ ./uiautomatorviewer
参考资料
1.在Android Sudio中使用Uiautomator
http://blog.csdn.net/cxq234843654/article/details/51203143