robotium 获取控件ID方法

本文介绍了一个测试工程中的自动化脚本,用于获取并分析应用内部信息,每5秒执行一次,通过Logcat窗口筛选特定标签,实现对当前活动页面的详细信息收集,包括视图元素的位置、ID、资源名称、可点击性和启用状态。
摘要由CSDN通过智能技术生成

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值