robotium学习笔记

2016-07-12  谢满彬 

一、常见问题
1.运行test case时报java.lang.NoClassDefFoundError:com.jayway.android.robotium.solo.Solo
解决方法:右击项目->属性->Java Build Path->Libraries。
                 检查是否有robotium-solo-x.x.x.jar,如果没有请导入。
                 如果已经有切换到Order and Export,勾选robotium-solo-x.x.x.jar,点击确定。
                 最后clear一下项目在运行测试。

2.运行test case时报Permission Denial: starting instrumentation ComponentInfo{testpackname/android.test.InstrumentationTestRunner}  from pid=28108, uid=28108 not allowed because package does not have a signature matching the target com.xxx.xxx
原因:签名不一致导致的

关于重签名:

方式一:用resign.jar,下载地址: http://www.troido.de/re-sign.jar 直接把apk文件拖入即可

可能遇到的问题:
    问题1:ANDROID_HOME not set
    解决方法:右击我的电脑->属性->高级系统设置->高级->环境变量,新建名称为ANDROID_HOME环境变量,value值是SDK目录                         
    问题2:Error:Cannot run program "%ANDROID_HOME%/tools/zipalign": CreateProcess error=2, 系统找不到指定的文件
    解决方法:由于往后的SDK版本,把zipalign这个工具放到了 %ANDROID_HOME%/builds/xx.xx.xx/下,把zipalgin.exe复制到%ANDROID_HOME% /tools下即可
    方式二:手工方式,步骤如下:
        1.解压apk文件
        2.进入解压后的目录,把文件夹META-INF删除
        3.把删除META-INF后的文件重新压缩,并修改后缀名为apk
        4.打开cmd,运行jarsigner -keystore ~/.android/debug.keystore -storepass android -keypass android applicationName.apk androiddebugkey
             JDK7建议使用: jarsigner -keystore ~/.android/debug.keystore -storepass android -keypass android  -sigalg MD5withRSA -digestalg SHA1 applicationName.apk androiddebugkey
        5.使用工具zipalign优化 zipalign 4 applicationName.apk resignedApplicationName.apk

二、使用经验
view定位:一般使用uiautomator viewer来协助定位(注意眼见不一定为真实的)
                  定位个人使用习惯一般是id优化,下标、文字和相对坐标结合。
ID方式:白盒:solo.getView(R.id.name)
              黑盒:solo.getView(StringID)
下标:solo.getEditText(0)
          solo.getView(ImageView.class,0)
 文字: solo.getText('文字1')
          solo.getButton('文字1')

相对坐标:

    View topView = solo.getView("topId");
    View bottomView = solo.getView("bottomId");
    View targetView = null;
    Listviews = solo.getViews();
    int[] xy_top = new int[2];
    int[] xy_bottom = new int[2];
    topView.getLocationOnScreen(xy_top);
    bottomView.getLocationOnScreen(xy_bottom);
    for(View view : views){
        int[] xy = new int[2];
        view.getLocationOnScreen(xy);
        if(xy[1] < xy_top[1] && xy[1] >xy_bottom[1]){
            targetView =view;
            break;
        }
    }


遍历ListView:


 
 
public void testSameView(){
	Log.e(solo.getConfig().commandLoggingTag, solo.getCurrentActivity().getLocalClassName());
	ListView listView = solo.getView(ListView.class,0);
	int total = listView.getCount();
	Log.e(solo.getConfig().commandLoggingTag, "listview size:" + total);
	String tmp_text = null;
	for(int i = 0;i<total;i++){
		solo.scrollListToLine(listView, i);
		List<View> views = solo.getCurrentViews();
		int targetID = solo.getCurrentActivity().getResources().getIdentifier("crime_list_item_titleTextView", 
										"id", "com.bignerdranch.android.criminalintent");
		List<View> result = new ArrayList<View>();
		for(int tmp=0;tmp<views.size();tmp++){
			if(targetID == views.get(tmp).getId() && views.get(tmp).isShown() && views.get(tmp).getVisibility() == 0 && views.get(tmp).isEnabled()){
				result.add(views.get(tmp));
			}
		}
		TextView tagert_tv = null;
		int size = result.size();
		Log.e(solo.getConfig().commandLoggingTag, "all title size :" + size);
		if(total - i <= size){
			Log.e(solo.getConfig().commandLoggingTag, "i=" + i);
			tagert_tv = (TextView)result.get(i + size - total);
			//为了兼容屏幕不是显示完整的item
			if(tmp_text != null && tmp_text.equals(tagert_tv.getText().toString())){
				tagert_tv = (TextView)result.get(i + size - total + 1);
			}
		}
		else{
			tagert_tv = (TextView)solo.getView("crime_list_item_titleTextView");
		}
		Log.e(solo.getConfig().commandLoggingTag, "crime title is :" + tagert_tv.getText().toString());
		tmp_text = tagert_tv.getText().toString();
		solo.clickOnView(tagert_tv);
		solo.goBack();
		solo.waitForView(ListView.class);
		listView = solo.getView(ListView.class,0);
	}
}

有错误或建议,请留言指出,我会尽快回复,谢谢。



272°/2701 人阅读/2 条评论 发表评论

邓智群  2016-07-16

如果格式在美观一点就好了


谢满彬  2016-07-22

@邓智群 谢谢你的建议


登录 后发表评论