Appium 关于 activity 的获取函数与 app 的 reset

付兰 · March 17, 2016 · Last by 付兰 replied at March 19, 2016 · 1454 hits

测试环境: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 用上了,谢谢!

共收到 13 条回复 时间 点赞

日志和代码尽量都适用 markdown,不要用图片。谢谢

将括号去掉即可,就是 current=driver.current_activity,但是,为什么要去掉括号,有人能稍微解答一下吗
建议你可以看下源码。因为这个方法 current_activity 是 webdriver 的自身属性。例如:

    @property
    def current_url(self):
        """
        Gets the URL of the current page.

        :Usage:
            driver.current_url
        """
        return self.execute(Command.GET_CURRENT_URL)['value']
# @property 装饰器可以将python定义的函数“当做”属性直接访问。

所以不需要带上 ()。直接调用就可以了。
ps:我是看的 selenium 的源码哦,你看下 appium 应该也是一样的 o(∩_∩) o 。

付兰 #3 · March 17, 2016 Author

#2 楼 @xie_0723 谢谢,这个是这样的:


@property
def current_activity(self):
    """Retrieves the current activity on the device.
    """
    return self.execute(Command.GET_CURRENT_ACTIVITY)['value']
付兰 #4 · March 17, 2016 Author

#1 楼 @monkey 好了

#3 楼 @a3096556718 原来群里是你问的啊,这是 property

付兰 #7 · March 18, 2016 Author

#6 楼 @neyo 是呀是呀

第二个问题,代码贴全

付兰 #9 · March 18, 2016 Author

#8 楼 @sanlengjingvv 贴完了,谢谢!

之前用过 selendroid 模式?现在手机上的 app 是 selendroid 模式下安装的?

付兰 #11 · March 18, 2016 Author

#10 楼 @sanlengjingvv 我的设置是 appium 啊

info: [debug] Running old fashion reset (reinstall)

貌似要重新安装,你加上这个试一试
desired_caps['app'] = '/abs/path/to/my.apk'

看下这个 issue 的评论
reset() breaks Appium if the App has been launched with "no_reset=true"

付兰 #13 · March 19, 2016 Author

#12 楼 @sanlengjingvv 是的,如果不设置路径的话,我 no reset 不勾选上也可以,我明天整理一下,谢谢

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