先贴上年前的两个帖子:
@qinggchu http://testerhome.com/topics/1826
@yangchengtest http://testerhome.com/topics/1876
Adb-For-Test
https://github.com/gb112211/Adb-For-Test
用在robotium中去处理跨进程的时候还有坑没填上,主要就是权限问题,这里可以看下@qinggchu的这篇博客http://blog.csdn.net/qingchunjun/article/details/43343735
robotium中使用uiautomator命令获取uidump.xml的时候是需要root权限才能获取到的,所以在跨进程时,要通过解析uidump.xml去定位元素位置的时候,就需要使用root过的真机才行,关键需要使用su命令。
参考@qinggchu修改的例子,年后回来就花了点时间修改了下Adb-For-Test,Adb-For-Robotium,简单测试了下,基本上可以满足需求,例子中有一个执行robotium脚本时录制的视频。
主要修改的地方就是获取uidump.xml,原谅我去解析的时候先是用的cat命令
// 获取设备当前界面的控件信息,并解析uidump.xml文件 private void uidump() { ShellUtils.suShell("uiautomator dump /data/local/tmp/uidump.xml"); sleep(2000); ShellUtils.suShell("chmod 777 /data/local/tmp/uidump.xml"); sleep(2000); try { xml = ShellUtils.StringTOInputStream(ShellUtils .getShellOut(ShellUtils .shell("cat /data/local/tmp/uidump.xml"))); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } dumps = new UiDumpService().getDumps(xml); } //需要root权限执行命令时使用该方法 public static void suShell(String cmd) { Process ps = null; DataOutputStream os; try { ps = Runtime.getRuntime().exec("su"); os = new DataOutputStream(ps.getOutputStream()); os.writeBytes(cmd + "\n"); os.writeBytes("exit\n"); os.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static InputStream StringTOInputStream(String in) throws Exception { ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1")); return is; } |