不说其他,直接上代码:
可以直接放在测试工程里用,记得改下包名。
5s 刷新一次,需要自行在 logcat 窗口中设置筛选条件 tag=getCurrentInfo 然后就~自行发挥吧
public class GetInfoTest extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "xxxxx.xxxxxxxx.xxxxxxxxxx.xxxxxxxxxx";
private static Class launcherActivityClass;
static {
try {
launcherActivityClass = Class
.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public GetInfoTest() throws ClassNotFoundException {
super(launcherActivityClass);
}
private Solo solo;
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void tearDown() throws Exception {
}
public void testGetInfo(){
while(true){
solo.sleep(5000);
getCurrentInfo();
}
}
public void getCurrentInfo(){
Log.v("getCurrentInfo", "current activity is "
+ solo.getCurrentActivity().getClass().getSimpleName());
ArrayList<View> av = solo.getCurrentViews();
Log.v("getCurrentInfo", "begin get view info");
for (View view : av) {
Log.v("getCurrentInfo", "* * * * * * *");
int[] location = { 0, 0 };
view.getLocationInWindow(location);
Log.v("getCurrentInfo", "location:" + location[0] + ","
+ location[1]);
Log.v("getCurrentInfo", "name:" + view.getClass().getName());
int id = view.getId();
if (view instanceof TextView) {
Log.v("getCurrentInfo", "text:" + ((TextView) view).getText().toString());
}
if (view instanceof ViewGroup) {
Log.v("getCurrentInfo", "this view is a viewgroup");
}
if (id == -1)
continue;
if (null != view.getResources()
&& null != view.getResources().getResourceEntryName(id))
Log.v("getCurrentInfo", "id:"+view.getResources().getResourceEntryName(id)
+ ",resourse:" + Integer.toHexString(id));
Log.v("getCurrentInfo", "clickable:"+view.isClickable());
Log.v("getCurrentInfo", "Enable:"+view.isEnabled());
}
ArrayList<WebElement> WebElementsList = solo.getCurrentWebElements();
Log.v("getCurrentInfo", "begin get webelements");
for (WebElement we : WebElementsList) {
Log.v("getCurrentInfo", "classname is " + we.getClassName());
Log.v("getCurrentInfo", "id is " + we.getId());
Log.v("getCurrentInfo", "x is " + we.getLocationX());
Log.v("getCurrentInfo", "y is " + we.getLocationY());
Log.v("getCurrentInfo", "name is " + we.getName());
Log.v("getCurrentInfo", "tag name is " + we.getTagName());
Log.v("getCurrentInfo", "text is " + we.getText());
}
}
}