直接使用adb shell命令是可以实现一些步骤,在无法调用appium的api实现某些操作时我们可以通过执行adb命令实现,比如某些点击事件,打开指定的文件等。但是若在appium服务器内穿插使用adb shell命令,adb命令可正常执行,但是后续的appium的api调用会出现问题,具体报错如下:
1 2 3 4 5 | [debug] [WD Proxy] Proxying [GET /source] to [GET http://127.0.0.1:8200/wd/hub/session/e8d74fb1-890f-4f66-8484-23941f00cf1b/source] with body: {} [WD Proxy] Got an unexpected response with status undefined: {"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":8200} [debug] [W3C (1919ed43)] Encountered internal error running command: UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server. Original error: Error: connect ECONNREFUSED 127.0.0.1:8200 [debug] [W3C (1919ed43)] at JWProxy.command (E:\node_modules\[email protected]@appium-base-driver\lib\jsonwp-proxy\proxy.js:270:13) [HTTP] <-- POST /wd/hub/session/1919ed43-ef90-4a9d-ab7f-52c4eef0e6b5/element 500 4101 ms - 571 |
原因:
新版本的appium服务器在默认情况下禁用了adb shell 等shell命令执行,必须在启动服务器时提供命令行参数
1 | appium --session-override --relaxed-security |
在被测设备上执行给定的shell命令,并返回其stdout或两者stdout,stderr如果将其includeStderr设置为true。如果命令的返回码不为零,则将引发异常。该命令的作用方式与adb shell在主机上执行该命令的方式相同。
支持的参数
command:远程命令的名称。例如,它也可以是可执行文件的完整路径/bin/ls。(必须指定)
args:以字符串表示的命令参数列表。如果提供单个字符串,则它将自动转换为单项数组。可选参数。
includeStderr:将此参数设置为true,以便将stderr输出与stdout一起包括到返回的结果中。如果启用,则返回的结果将地图stdout和stderr包含相应的字符串键,否则它只是一个简单的字符串。false默认。
timeout:shell命令超时(以毫秒为单位)。如果该命令需要更多时间来完成执行,则将引发异常。默认为20000毫秒。
示例代码:
1 2 3 4 5 6 7 | result = driver.execute_script('mobile: shell', { 'command': 'echo', 'args': ['arg1', 'arg2'], 'includeStderr': True, 'timeout': 5000 }) assert result['stdout'] == 'arg1 arg2' |
实现代码:
1 2 3 4 5 6 | opts = { 'command':'am start', 'args':['-n','cn.wps.moffice_eng/' 'cn.wps.moffice.documentmanager.PreStartActivity2','-d','file://mnt/sdcard/documents/test.doc'] } result = self.page.execute_script('mobile:shell',opts) |
注:command内就不用写adb shell 了,AppiumDriver执行时会自带adb shell。