缘由

最近在做 Appium 升级(从 1.4.13 到 1.6.3),升级完成后,发现在启动应用时,是先安装 AppiumSettings 和 Unlock 应用,再启动 AppiumBootstrap,
这就导致在部分机器上安装前面两个应用时,无法自动点击 “安装” 的 button。
日志如下:

[debug] [AndroidDriver] Pushing settings apk to device...
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","install","E:\\up\\autodependency\\mobile\\appium\\node_modules\\appium-android-driver\\node_modules\\io.appium.settings\\bin\\settings_apk-debug.apk"]
[debug] [AndroidDriver] Pushing unlock helper app to device...
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","install","E:\\up\\autodependency\\mobile\\appium\\node_modules\\appium-android-driver\\node_modules\\appium-unlock\\bin\\unlock_apk-debug.apk"]
[ADB] Getting device platform version
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","shell","getprop","ro.build.version.release"]
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","shell","dumpsys","window"]
[AndroidDriver] Screen already unlocked, doing nothing
[AndroidDriver] Remote apk path is /data/local/tmp/9024285e01047110b9f18f73cacee078.apk
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
......
[debug] [ADB] Forwarding system: 4724 to device: 4724
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","forward","tcp:4724","tcp:4724"]
[debug] [UiAutomator] Starting UiAutomator
[debug] [UiAutomator] Moving to state 'starting'
[debug] [UiAutomator] Parsing uiautomator jar
[debug] [UiAutomator] Found jar name: 'AppiumBootstrap.jar'
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","push","E:\\up\\autodependency\\mobile\\appium\\node_modules\\appium-android-driver\\node_modules\\appium-android-bootstrap\\bootstrap\\bin\\AppiumBootstrap.jar","/data/local/tmp/"]
[debug] [ADB] Attempting to kill all uiautomator processes
[debug] [ADB] Getting all processes with uiautomator
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","shell","ps"]
[ADB] No uiautomator process found to kill, continuing...
[debug] [UiAutomator] Starting UIAutomator
[debug] [ADB] Creating ADB subprocess with args: ["-P",5037,"-s","562beeee","shell","uiautomator","runtest","AppiumBootstrap.jar","-c","io.appium.android.bootstrap.Bootstrap","-e","pkg","com.XXX","-e","disableAndroidWatchers",false,"-e","acceptSslCerts",false]

WebStorm 修改 Appium 源码

  1. 打开以下两个文件并修改:
//更新 appium\node_modules\appium-android-driver\build\lib\android-helpers.js
case 11:
context$1$0.next = 13;
//注释下面这句,并添加return
//return _regeneratorRuntime.awrap(helpers.pushSettingsApp(adb));
return;

case 13:
context$1$0.next = 15;
//注释下面这句,并添加return
// return _regeneratorRuntime.awrap(helpers.pushUnlock(adb));
return;
 //更新 appium\node_modules\appium-android-driver\build\lib\driver.js
 case 11:
  if (!this.opts.autoLaunch) {
    context$2$0.next = 14;
    break;
  }

  context$2$0.next = 14;
  // start UiAutomator
  this.bootstrap = new _androidHelpers2['default'].bootstrap(this.adb, this.bootstrapPort, this.opts.websocket);
  return _regeneratorRuntime.awrap(this.bootstrap.start(this.opts.appPackage, this.opts.disableAndroidWatchers));

case 14:
  context$2$0.next = 15;
  return _regeneratorRuntime.awrap(this.initAUT());

case 15:
  context$2$0.next = 16;
  return _regeneratorRuntime.awrap(_androidHelpers2['default'].pushSettingsApp(this.adb));

case 16:
  context$2$0.next = 17;
  return _regeneratorRuntime.awrap(_androidHelpers2['default'].pushUnlock(this.adb));

case 17:

注意点

  1. 记得测试一下在锁屏状态下,创建 driver 并启动应用是否会自动解锁

测试检查

[debug] [UiAutomator] Starting UiAutomator
[debug] [UiAutomator] Moving to state 'starting'
[debug] [UiAutomator] Parsing uiautomator jar
[debug] [UiAutomator] Found jar name: 'AppiumBootstrap.jar'
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","push","E:\\up\\autodependency\\mobile\\appium\\node_modules\\appium-android-driver\\node_modules\\appium-android-bootstrap\\bootstrap\\bin\\AppiumBootstrap.jar","/data/local/tmp/"]
[debug] [ADB] Attempting to kill all uiautomator processes
[debug] [ADB] Getting all processes with uiautomator
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","shell","ps"]
[ADB] No uiautomator process found to kill, continuing...
[debug] [UiAutomator] Starting UIAutomator
[debug] [ADB] Creating ADB subprocess with args: ["-P",5037,"-s","562beeee","shell","uiautomator","runtest","AppiumBootstrap.jar","-c","io.appium.android.bootstrap.Bootstrap","-e","pkg","com.suning.snmessenger","-e","disableAndroidWatchers",false,"-e","acceptSslCerts",false]
[debug] [UiAutomator] Moving to state 'online'
[AndroidBootstrap] Android bootstrap socket is now connected
[debug] [AndroidDriver] Pushing settings apk to device...
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","install","E:\\up\\autodependency\\mobile\\appium\\node_modules\\appium-android-driver\\node_modules\\io.appium.settings\\bin\\settings_apk-debug.apk"]
[debug] [AndroidDriver] Pushing unlock helper app to device...
[debug] [ADB] Running 'E:\DevTool\Android\SDK\platform-tools\adb.exe' with args: ["-P",5037,"-s","562beeee","install","E:\\up\\autodependency\\mobile\\appium\\node_modules\\appium-android-driver\\node_modules\\appium-unlock\\bin\\unlock_apk-debug.apk"]

感慨一下

因为没有看过 appium 的源码,这里纯粹是为了解决某个问题,强撸了代码,从开始一步一步的 debug 过来的,TMD 按 F7 按的手都麻了,才找到这个要修改的地方。
这里修改的其实是 build 后的源码,没 build 之前的,我也不知道怎么弄,望求知道的兄弟指点。


↙↙↙阅读原文可查看相关链接,并与作者交流