Appium uiautomatorviewer 获取不到动态页面解决办法

hello · March 22, 2018 · Last by canjieChen replied at April 17, 2019 · 8822 hits

以前玩 PC 最近刚搞 app 测试,费好大劲环境装好,高兴的点击 uiautomatorviewer.bat 想去抓取页面元素,尴尬的是首页就直接报错。

Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't exist! >

百度发现按照 Q 博士大神的提供的方法可以解决 ### @DoctorQ 博客地址.原理就是调用 UiDevice.dumpWindowHierarchy() 方法去获取 dumpxml。
现把完整解决全过程贴一下,方便像我一样这样小白复制粘贴。
1,建一个测试工程

工程结构如上图,代码极其简单,我这是顺便把页面截图也搞定了。
代码贴出,方便需要的朋友复制粘贴。

package com.lvmama.uidump;
import java.io.File;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class DumpXml extends UiAutomatorTestCase {
public void testRun() throws Exception {
UiDevice uiDevice = getUiDevice();
File pic = new File("/data/local/tmp/uishot.png");
uiDevice.takeScreenshot(pic,1.0f,100);
uiDevice.dumpWindowHierarchy("uidump.xml");

}
}
这里说一下更改工程为 uitest-project 的方法
1,打开 Cmd,进入 SDK 的 tools 目录如我的 D:\android\android-sdk-windows\tools>
执行命令:android create uitest-project -n LvmamaXmlKit -t 1 -p D:\my_java\LvmamaXmlKit
执行完后工程即为 uitest-project 工程

2,打开 build.xml,把改成
---打开工程下面的 build.xml,改 default 的值改成"build"
3,run as Ant build
---工程下,右键点击 build.xml,run as Ant build

jar 即打包成功了。
2,执行 push 命令推送 jar 包到手机中

3,验证截图是否成功
打命令:adb shell uiautomator runtest LvmamaXmlKit.jar -c com.lvmama.uidump.DumpXml

去手机里看看截图是否生成。

完事后修改一下 uiautomatorviewer 源码,调用自己的 jar,截图抓 xml 很爽的!

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 17 条回复 时间 点赞

请问源码怎么修改呢?

hello #3 · June 11, 2018 Author
maguoze 回复

这介绍还不够明白?代码都贴上了

hello #4 · June 11, 2018 Author
maguoze 回复
hello 回复

这个 jar 已经做出来了,也推到手机了,就是不知道怎么修改 uiautomatorviewer 源码,调用自己的 jar

hello 回复

下载了你链接的 uiautomatorviewer.jar,替换了原来的,但是截图的时候报错

hello #7 · June 11, 2018 Author
maguoze 回复

夜神模拟器?

hello 回复

真机的,要在真机测试

hello #9 · June 11, 2018 Author
maguoze 回复

大哥,定位元素 的环境 和运行环境没有任何关系的啊,你可以用夜神去定位元素,用真机去执行测试啊,我这个 bug 还没修改,最近忙,真机还不支持,实在抱歉啊。

hello 回复

不用抱歉,有方法已经不错了。那修改 uiautomatorviewer 源码,调用自己的 jar 这个有方法吗?现在运行了自己的 jar 包,只能自己打开 XML 文件看,没法在 uiautomatorviewer 直接用,新手一枚,还在学习中

hello #11 · June 11, 2018 Author
maguoze 回复

修改 uiautomatorviewer 源码,调用自己的 jar 这个有方法,我这个就是这么干的。有兴趣的话,你研究一下源码,改代码吧。

hello 回复

uiautomatorviewer 源码在哪个位置呢?还没找到😢 ,有修改的说明书就最好了,现在还在慢慢摸索😄

有个暂时解决的办法,关闭 uiautomatorviewer,在 cmd 中输入 adb reboot,重新打开 uiautomatorviewer,就可以了

hello #14 · June 13, 2018 Author
strong 回复

哦,这个是没试过,回头试试看

strong 回复

这个办法还是不行呢

hello 回复

大神,有修改 uiautomatorviewer 源码,调用自己的 jar 的方法吗?

hello #17 · June 25, 2018 Author
maguoze 回复

现在已经支持真机了,你下载一下试试看。另外,你 app 下载链接方便发一下么。这边调查一下,看为啥截图不行。

halohalo,我想问一下,我看你项目上面写着兼容 android 和 ios,为什么 iOS 我无法获取呢,是我哪里没配置好么?

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up