Appium Appium1.6.3 版本,在 bootstrap 源码中添加自定义监听器,将手动编译的 AppiumBootstrap.jar 放到 Appium 中,自定义监听没有生效

baxia · 2017年06月26日 · 最后由 bauul 回复于 2017年06月29日 · 1585 次阅读

1、版本:appium1.6.3
2、主要目的:想要在 bootstrap 的监听里自定义适合我测试应用的监听
3、遇到问题:
1)修改了 C:\Users\xxx\AppData\Roaming\npm\node_modules\appium\node_modules\appium-android-bootstrap\bootstrap 项目中的代码后,顺利使用 mvn clean package 生成 jar 包(期间编译添加 android 的相关依赖),将其放入 C:\Users\xxx\AppData\Roaming\npm\node_modules\appium\node_modules\appium-android-bootstrap\bootstrap\bin 下后,运行脚本,新的 AppiumBootstrap.jar 并没有起到监听作用,不知道为什么
2)尝试在 C:\Users\xxx\AppData\Roaming\npm\node_modules\appium\node_modules\appium-uiautomator\build\lib 下的 uiautomator.js 添加-e debug true 想远程调试 bootstrap,始终不能断点,获取到端口号,请问这是为什么呢?

在 carl 的指点下实现了目的,但是最后还是遗留了一堆问题,希望知道的大佬们来指点下~
1)为何在 C:\Users\xxx\AppData\Roaming\npm\node_modules\appium\node_modules\appium-uiautomator\build\lib 下的 uiautomator.js 添加-e debug true 没有生效?
2)appium 是在什么时候对 bootstrap 进行打包的?
3)为何使用 mvn 构建的 AppiumBootstrap.jar 包,修改的代码无效?
4)mvn install -P 4.4 失败,报错信息

共收到 8 条回复 时间 点赞
baxia 回复

1.把你的修改的代码贴出来,
2.把手机中 data/local/tmp 下面的 appiumbootstrap.jar 给 pull 出来并反编译,确定有包含你的代码
3.因为这是 ui1.0 的 jar 包,所以一般是使用 ant build 来打包的,不确定 mvn 打包是否 ok

bauul 回复

1、在 bootstrap.java->server.listenForever->dismissCrashAlerts->registerAnrAndCrashWatchers 中添加一个监听:

    UiDevice.getInstance().registerWatcher("WIFI", new UiWatcher() {
      @Override
      public boolean checkForCondition() {
        Logger.debug("WIFI 不稳定!");
//        UiObject window = new UiObject(new UiSelector().textContains("等一会"));
        UiObject window = new UiObject(new UiSelector().resourceId("com.xxx:id/tvAlert"));
        if (window.exists()){
          try {
            window.click();//点击等一会
          } catch (UiObjectNotFoundException e) {
            Log.e(LOG_TAG, "dialog gone?", e);
          }
          return true;
        }
        return false;
      }
    });
    Log.i(LOG_TAG, "Registed GUI Exception watchers");
  }

2、反编译出来后修改的内容时存在的

baxia 回复

看你写的没错,
你可以执行命令行把这个 server 起来,

uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap

然后在手机上操作把这个弹窗显示出来,验证一下有没有效果

bauul 回复

1、已经运行过了,还是不执行的。
2、通过观察运行的 log:

 runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap
INSTRUMENTATION_STATUS: numtests=1
INSTRUMENTATION_STATUS: stream=
io.appium.android.bootstrap.Bootstrap:
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: test=testRunServer
INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS_CODE: 1
[APPIUM-UIAUTO] [debug] Socket opened on port 4724[/APPIUM-UIAUTO]
[APPIUM-UIAUTO] [debug] Appium Socket Server Ready[/APPIUM-UIAUTO]
[APPIUM-UIAUTO] [debug] Loading json...[/APPIUM-UIAUTO]
[APPIUM-UIAUTO] [debug] Registered crash watchers.[/APPIUM-UIAUTO]

但是我在 bootstrap 中对这些字符打印也做过修改,实际根据控制台打印判断应该是没生效:

  public SocketServer(final int port) throws SocketServerException {
    keepListening = true;
    executor = new AndroidCommandExecutor();
    try {
      server = new ServerSocket(port);
      Logger.debug("**Socket opened on port " + port);
      ...
}
public void listenForever(boolean disableAndroidWatchers, boolean acceptSSLCerts) throws SocketServerException {
    Logger.debug("**Appium Socket Server Ready");
    ...
}

3、目前疑惑点:
1)根据 bootstrap 源码 git 项目中 readme 操作,但是执行 mvn install -P 4.4 出现报错,但是 bootstrap 可以正常导入 android.4.4.2_r4 了。项目也不报错了,不知道对于最终的 jar 包有没有影响?

[INFO] Android Add-Ons .................................... SUCCESS [  0.016 s]
[INFO] Android Add-On Google Platform 4.4 API 19 (Maps and USB) FAILURE [  0.078
s]
[INFO] Google Glass Development Kit (GDK) API 19 .......... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.501 s
[INFO] Finished at: 2017-06-28T09:16:38+08:00
[INFO] Final Memory: 22M/620M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0.0:re
ad-project-properties (default) on project google-apis-19: Properties could not b
e loaded from File: D:\tools\Android\newSDK\add-ons\addon-google_apis-google-19\s
ource.properties -> [Help 1]

2)还有就是 appium 自动打 bootstrap 的 jar 时机是在什么时候?想着是不是可以重新叫 appium 来打 jar 包

baxia 回复

你仔细检查一下 appium 执行日志,它会显示 push appiumbootstrap.jar 这个文件到手机的日志,另外打包是用 ant 的,
转到源码目录下 node_modules\appium-android-driver\node_modules\appium-android-bootstrap\bootstrap
执行 ant build 即可。
ui1.0 只依赖两个 jar 包,android.jar 和 uiautomator.jar,在 android sdk 的 platform 目录下

具体打包方法可参见百度:http://www.cnblogs.com/cologne/p/4726024.html

bauul 回复

最后试了一下,使用 ant 打包成功了,也最终实现了。(虽然不明白为何 mvn 打出的 jar 包不可以。。。),谢谢大佬指点~🤗

baxia 回复

👍
互相学习

baxia 修改 Bootstrap 源码,添加自定义监听代码实现 中提及了此贴 06月22日 14:51
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册