robotium 获取控件ID方法

来源:互联网 发布:mysql delete in语句 编辑:程序博客网 时间:2024/06/06 08:40

直接放在测试工程里用,改下包名。
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());        }    }}
0 0