测试环境:appium1.4.16+python3.5+android5.1
1.网上 api 中获取 activity 用法为:driver.current_activity(),结果使用时出现错误

try:
    current=driver.current_activity()
    print(current)
except Exception as e:
    print("[Error]: ",e)
driver.quit()

错误信息:

> info: --> GET /wd/hub/session/be5bd684-8ac8-442d-8e87-3b4f3817a727/appium/device/current_activity {}
> info: [debug] Getting focused package and activity
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "dumpsys window windows"
> info: [debug] Responding to client with success: {"status":0,"value":"com.android.camera.CameraLauncher","sessionId":"be5bd684-8ac8-442d-8e87-3b4f3817a727"}
> info: <-- GET /wd/hub/session/be5bd684-8ac8-442d-8e87-3b4f3817a727/appium/device/current_activity 200 210.211 ms - 107 {"status":0,"value":"com.android.camera.CameraLauncher","sessionId":"be5bd684-8ac8-442d-8e87-3b4f3817a727"}
> info: --> DELETE /wd/hub/session/be5bd684-8ac8-442d-8e87-3b4f3817a727 {}
> info: Shutting down appium session
> info: [debug] Pressing the HOME button
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "input keyevent 3"
> info: [debug] Stopping logcat capture
> info: [debug] Logcat terminated with code null, signal SIGTERM
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
> info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":"OK, shutting down"}
> info: [debug] [UIAUTOMATOR STDOUT] [CDS]close[4724]
> info: [debug] [BOOTSTRAP] [debug] Closed client connection

console 输出错误信息为:

[Error]:  'str' object is not callable

解决办法:目前群里有人帮我解决了,将括号去掉即可,就是 current=driver.current_activity
但是,为什么要去掉括号,有人能稍微解答一下吗

2、关闭 app 使用 driver.close_app() ,启动 app 使用 driver.launch_app()都是对的,但是使用重启 app 就出现错误了

#coding:utf-8 print(driver.current_activity())
from appium import webdriver
import time
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.touch_actions import TouchActions
desired_caps = {}
desired_caps['platformName'] = 'Android'  
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '0123456789ABCDEF'
desired_caps['appPackage'] = 'com.android.gallery3d'
desired_caps['appActivity'] = 'com.android.camera.CameraLauncher'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(2)
print('开始执行')
try:
    driver.reset()
except Exception as e:
    print("[Error]: ",e)
print('执行完成')
driver.quit()

错误信息:

> info: Welcome to Appium v1.4.16 (REV ae6877eff263066b26328d457bd285c0cc62430d)
> info: Appium REST http interface listener started on 127.0.0.1:4723
> info: [debug] Non-default server args: {"address":"127.0.0.1","noReset":true,"logNoColors":true,"deviceName":"0123456789ABCDEF","platformName":"Android","platformVersion":"22","automationName":"Appium"}
> info: Console LogLevel: debug
> info: --> POST /wd/hub/session {"desiredCapabilities":{"platformName":"Android","appActivity":"com.android.camera.CameraLauncher","deviceName":"0123456789ABCDEF","platformVersion":"5.1","appPackage":"com.android.gallery3d"}}
> info: Client User-Agent string: Python-urllib/3.5
> info: [debug] Didn't get app but did get Android package, will attempt to launch it on the device
> info: [debug] Creating new appium session 9879fabd-8d7a-49bb-a5cf-4b49c7f286c8
> info: Starting android appium
> info: [debug] Getting Java version
> info: Java version is: 1.8.0_25
> info: [debug] Checking whether adb is present
> info: [debug] Using adb from D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe
> warn: No app capability, can't parse package/activity
> info: [debug] Using fast reset? false
> info: [debug] Preparing device for session
> info: [debug] Not checking whether app is present since we are assuming it's already on the device
> info: Retrieving device
> info: [debug] Trying to find a connected android device
> info: [debug] Getting connected devices...
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe devices
> info: [debug] 1 device(s) connected
> info: Found device 0123456789ABCDEF
> info: [debug] Setting device id to 0123456789ABCDEF
> info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF wait-for-device
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "echo 'ready'"
> info: [debug] Starting logcat capture
> info: [debug] Getting device API level
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "getprop ro.build.version.sdk"
> info: [debug] Device is at API Level 22
> info: Device API level is: 22
> info: [debug] Extracting strings for language: default
> info: [debug] Apk doesn't exist locally
> info: [debug] Could not get strings, but it looks like we had an old strings file anyway, so ignoring
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "rm -rf /data/local/tmp/strings.json"
> info: [debug] Not uninstalling app since server not started with --full-reset
> info: [debug] Skipping install since we launched with a package instead of an app path
> info: [debug] Forwarding system:4724 to device:4724
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF forward tcp:4724 tcp:4724
> info: [debug] Pushing appium bootstrap to device...
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF push "C:\\Program Files\\Appium\\node_modules\\appium\\build\\android_bootstrap\\AppiumBootstrap.jar" /data/local/tmp/
> info: [debug] Pushing settings apk to device...
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF install "C:\Program Files\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk"
> info: [debug] Pushing unlock helper app to device...
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF install "C:\Program Files\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
> info: Starting App
> info: [debug] Attempting to kill all 'uiautomator' processes
> info: [debug] Getting all processes with 'uiautomator'
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "ps 'uiautomator'"
> info: [debug] No matching processes found
> info: [debug] Running bootstrap
> info: [debug] spawning: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.android.gallery3d -e disableAndroidWatchers false
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
> info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
> info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
> info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
> info: [debug] [BOOTSTRAP] [debug] Loading json...
> info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.
> info: [debug] Waking up device if it's not alive
> info: [debug] Pushing command to appium work queue: ["wake",{}]
> info: [debug] [BOOTSTRAP] [debug] Client connected
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"wake","params":{}}
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "dumpsys window"
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: wake
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":true}
> info: [debug] Screen already unlocked, continuing.
> info: [debug] Pushing command to appium work queue: ["getDataDir",{}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"getDataDir","params":{}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getDataDir
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":"\/data\/local\/tmp"}
> info: [debug] dataDir set to: /data/local/tmp
> info: [debug] Pushing command to appium work queue: ["compressedLayoutHierarchy",{"compressLayout":false}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"compressedLayoutHierarchy","params":{"compressLayout":false}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: compressedLayoutHierarchy
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":false}
> info: [debug] Getting device API level
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "getprop ro.build.version.sdk"
> info: [debug] Device is at API Level 22
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.android.gallery3d/com.android.camera.CameraLauncher"
> info: [debug] Waiting for pkg "com.android.gallery3d" and activity "com.android.camera.CameraLauncher" to be focused
> info: [debug] Getting focused package and activity
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "dumpsys window windows"
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "getprop ro.build.version.release"
> info: [debug] Device is at release version 5.1
> info: [debug] Device launched! Ready for commands
> info: [debug] Setting command timeout to the default of 60 secs
> info: [debug] Appium session started with sessionId 9879fabd-8d7a-49bb-a5cf-4b49c7f286c8
> info: <-- POST /wd/hub/session 303 7613.497 ms - 74 
> info: --> GET /wd/hub/session/9879fabd-8d7a-49bb-a5cf-4b49c7f286c8 {}
> info: [debug] Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"5.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformName":"Android","appActivity":"com.android.camera.CameraLauncher","deviceName":"0123456789ABCDEF","platformVersion":"5.1","appPackage":"com.android.gallery3d"},"platformName":"Android","appActivity":"com.android.camera.CameraLauncher","deviceName":"0123456789ABCDEF","appPackage":"com.android.gallery3d"},"sessionId":"9879fabd-8d7a-49bb-a5cf-4b49c7f286c8"}
> info: <-- GET /wd/hub/session/9879fabd-8d7a-49bb-a5cf-4b49c7f286c8 200 21.139 ms - 639 {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"5.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformName":"Android","appActivity":"com.android.camera.CameraLauncher","deviceName":"0123456789ABCDEF","platformVersion":"5.1","appPackage":"com.android.gallery3d"},"platformName":"Android","appActivity":"com.android.camera.CameraLauncher","deviceName":"0123456789ABCDEF","appPackage":"com.android.gallery3d"},"sessionId":"9879fabd-8d7a-49bb-a5cf-4b49c7f286c8"}
> info: --> POST /wd/hub/session/9879fabd-8d7a-49bb-a5cf-4b49c7f286c8/appium/app/reset {"sessionId":"9879fabd-8d7a-49bb-a5cf-4b49c7f286c8"}
> error: Problem calculating md5: TypeError: Arguments to path.resolve must be strings
> info: [debug] Resetting app mid-session
> info: [debug] Running device specific reset
> info: [debug] Running old fashion reset (reinstall)
> info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue":"Arguments to path.resolve must be strings"},"sessionId":"9879fabd-8d7a-49bb-a5cf-4b49c7f286c8"}
> info: <-- POST /wd/hub/session/9879fabd-8d7a-49bb-a5cf-4b49c7f286c8/appium/app/reset 500 11.223 ms - 210 
> info: --> DELETE /wd/hub/session/9879fabd-8d7a-49bb-a5cf-4b49c7f286c8 {}
> info: Shutting down appium session
> info: [debug] Pressing the HOME button
> info: [debug] executing cmd: D:\android-sdk_r21-windows\android-sdk-windows\platform-tools\adb.exe -s 0123456789ABCDEF shell "input keyevent 3"
> info: [debug] Stopping logcat capture
> info: [debug] Logcat terminated with code null, signal SIGTERM
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
> info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":"OK, shutting down"}
> info: [debug] [UIAUTOMATOR STDOUT] [CDS]close[4724]
> info: [debug] [BOOTSTRAP] [debug] Closed client connection
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
> info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
> info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
> info: [debug] [UIAUTOMATOR STDOUT] Time: 5.636
> info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
> info: [debug] UiAutomator shut down normally
> info: [debug] Cleaning up android objects
> info: [debug] Cleaning up appium session
> info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"9879fabd-8d7a-49bb-a5cf-4b49c7f286c8"}
> info: <-- DELETE /wd/hub/session/9879fabd-8d7a-49bb-a5cf-4b49c7f286c8 200 1428.302 ms - 76 {"status":0,"value":null,"sessionId":"9879fabd-8d7a-49bb-a5cf-4b49c7f286c8"}

console 输出的:

[Error]:  Message: An unknown server-side error occurred while processing the command.

3、有没有什么方法可以在运行代码出错后可以快速的关掉 appium 的所谓的 session 吗(因为改了代码想马上运行时会提示有一个 sesion 正在运行),由于没办法,把昨晚刚学到的 try except 用上了,谢谢!


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