1.调用jar包的方法,先用ant build,然后把bin目录下的jar包push到手机中:adb push 目录/.jar /data/local/tmp
运行:adb shell uiautomator runtest test.jar -c test.test1 -e key(key值即为下面paramName.getString("idName")方法中的“idName”这样的值)
2.通过Bundle paramName = getParams();创建一个键值对,getParams()会获取到向jar包传入的key值,然后用UiSelector().方法获得key值对应的页面元素,在打印处其坐标.getBounds();
3.多个参数查询时,用多个-e key
4.同时传入class和text时先用UiCollection查找对应class的区域,但要注意传入的class需要是一个section,比如一个linearout之类的,然后在找到的区域里面再去查找子元素,用collection.getChild
/***
源码演示
*/
package findObject;
import android.os.Bundle;
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;
/*
* @param:idName
* @param:classname
* @param:text
* */
public class findUiobject extends UiAutomatorTestCase{
public void testFindByClassName() throws UiObjectNotFoundException{
Bundle paramName = getParams();
if(paramName!=null){//如果key值不为空
//通过resourceID查找元素
if(paramName.getString("idName")!=null){
UiObject idButton = new UiObject(new UiSelector().resourceId(paramName.getString("idName")));
if (idButton.exists()) {
System.out.println(idButton.getBounds());
}else {
System.out.println("Rect is()");
}
}
else if(paramName.getString("classname")!=null&¶mName.getString("text")==null){
//当classname有值但text没有值时查找元素并返回
UiObject idButton2 = new UiObject(new UiSelector().className(paramName.getString("classname")));
if (idButton2.exists()) {
System.out.println(idButton2.getBounds());
}else {
System.out.println("Rect is()");
}
}
else if (paramName.getString("text")!=null&¶mName.getString("classname")==null) {
//当text有值但classname没有值时查找元素并返回
UiObject idButton3 = new UiObject(new UiSelector().textMatches(paramName.getString("text")));
if (idButton3.exists()) {
System.out.println(idButton3.getBounds());
}else {
System.out.println("Rect is()");
}
}
else if(paramName.getString("classname")!=null&¶mName.getString("text")!=null){
//当classname和text均不为空时,查找元素并返回
UiCollection collection = new UiCollection(new UiSelector().className(paramName.getString("classname")));
UiObject idButton4 = collection.getChild(new UiSelector().textMatches(paramName.getString("text")));
if (idButton4.exists()) {
System.out.println(idButton4.getBounds());
}else {
System.out.println("Rect is()");
}
}
}else {
System.out.println("Please input key");
}
}
}