Appium Appium 先开启一个 apk,再用 start_activity 开启另一个 apk 失败

honeybadger · 2015年07月07日 · 最后由 jiongdong 回复于 2016年11月17日 · 1492 次阅读

我先打开 Android 设置 apk, 然后使用 start_activity 打开另一个 apk:chrome 浏览器, 然后就卡在了初始页面,无法跳转到目标网页, 下面是我的代码:

#-*- coding: utf-8 -*-
from appium import webdriver
from time import sleep
desired_caps={
  'platformName': 'Android',
  'platformVersion': '4.4',
  'deviceName': 'bianbianmm',
  'browserName': 'Chrome',
  'udid': 'E561Lv'
}
desired_caps2={
  'platformName': 'Android',
  'platformVersion': '4.4',
  'deviceName': 'bianbianmm',
  'udid': 'E561Lv',
  'appPackage': 'com.android.settings',
  'appActivity': '.Settings'
}
driver = webdriver.Remote('http://localhost:4726/wd/hub',desired_caps2)
sleep(3)
driver.start_activity('com.android.chrome', 'com.google.android.apps.chrome.ChromeTabbedActivity')
driver.get('http://www.baidu.com')  #这步报错

出错的内容很奇怪:

Traceback (most recent call last):
File "C:\Users\AutoTest\Desktop\appium-python-chromev2.py", line 64, in
driver.get('http://www.baidu.com')
File "c:\python27\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 185, in get
self.execute(Command.GET, {'url': url})
File "c:\python27\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "build\bdist.win32\egg\appium\webdriver\errorhandler.py", line 29, in check_response
raise wde
WebDriverException: Message: Not yet implemented. Please help us: http://appium.io/get-involved.html

Appium 里面的日志:

Starting Node Server
info: Welcome to Appium v1.3.4 (REV c8c79a85fbd6870cd6fc3d66d038a115ebe22efe)
info: Appium REST http interface listener started on 127.0.0.1:4726
info: [debug] Non-default server args: {"address":"127.0.0.1","port":4726,"bootstrapPort":2275,"logNoColors":true,"platformName":"Android","platformVersion":"19","automationName":"Appium","chromeDriverPort":7845}
info: Console LogLevel: debug
info: --> POST /wd/hub/session {"desiredCapabilities":{"deviceName":"bianbianmm","udid":"E561Lv","platformVersion":"4.4","appPackage":"com.android.settings","platformName":"Android","appActivity":".Settings"}}
info: Client User-Agent string: Python-urllib/2.7
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 855d1e11-a3d8-46c7-bafd-beac9fc796c0
info: Starting android appium
info: [debug] Getting Java version
info: Java version is: 1.8.0_25
info: [debug] Using fast reset? true
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: [debug] Checking whether adb is present
info: [debug] Using adb from E:\AndriodSDK\platform-tools\adb.exe
info: Retrieving device
info: [debug] Trying to find a connected android device
info: [debug] Getting connected devices...
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe devices
info: [debug] 1 device(s) connected
info: Found device E561Lv
info: [debug] Setting device id to E561Lv
info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv wait-for-device
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv shell "echo 'ready'"
info: [debug] Starting logcat capture
warn: No app capability, can't parse package/activity
info: [debug] Getting device API level
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv shell "getprop ro.build.version.sdk"
info: [debug] Device is at API Level 19
info: Device API level is: 19
info: [debug] Apk doesn't exist locally
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv 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:2275 to device:4724
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv forward tcp:2275 tcp:4724
info: [debug] Pushing appium bootstrap to device...
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv push "E:\AppiumGUI\Appium\node_modules\appium\build\android_bootstrap\AppiumBootstrap.jar" /data/local/tmp/
info: [debug] Pushing settings apk to device...
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv install "E:\AppiumGUI\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk"
info: [debug] Pushing unlock helper app to device...
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv install "E:\AppiumGUI\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: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv shell "ps 'uiautomator'"
info: [debug] No matching processes found
info: [debug] Running bootstrap
info: [debug] spawning: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap
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] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv shell "dumpsys window"
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"wake","params":{}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: wake
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
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] dataDir set to: /data
info: [debug] Pushing command to appium work queue: ["compressedLayoutHierarchy",{"compressLayout":false}]
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: getDataDir
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"\/data","status":0}
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] Getting device API level
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv shell "getprop ro.build.version.sdk"
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":false,"status":0}
info: [debug] Device is at API Level 19
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.android.settings/.Settings"
info: [debug] Waiting for pkg "com.android.settings" and activity ".Settings" to be focused
info: [debug] Getting focused package and activity
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv shell "dumpsys window windows"
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 855d1e11-a3d8-46c7-bafd-beac9fc796c0
info: <-- POST /wd/hub/session 303 7575.272 ms - 9
info: --> GET /wd/hub/session/855d1e11-a3d8-46c7-bafd-beac9fc796c0 {}
info: [debug] Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"4.4","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"deviceName":"bianbianmm","udid":"E561Lv","platformVersion":"4.4","appPackage":"com.android.settings","platformName":"Android","appActivity":".Settings"},"deviceName":"bianbianmm","udid":"E561Lv","appPackage":"com.android.settings","platformName":"Android","appActivity":".Settings"},"sessionId":"855d1e11-a3d8-46c7-bafd-beac9fc796c0"}
info: <-- GET /wd/hub/session/855d1e11-a3d8-46c7-bafd-beac9fc796c0 200 3.425 ms - 609 {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"4.4","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"deviceName":"bianbianmm","udid":"E561Lv","platformVersion":"4.4","appPackage":"com.android.settings","platformName":"Android","appActivity":".Settings"},"deviceName":"bianbianmm","udid":"E561Lv","appPackage":"com.android.settings","platformName":"Android","appActivity":".Settings"},"sessionId":"855d1e11-a3d8-46c7-bafd-beac9fc796c0"}
info: --> POST /wd/hub/session/855d1e11-a3d8-46c7-bafd-beac9fc796c0/appium/device/start_activity {"appPackage":"com.android.chrome","sessionId":"855d1e11-a3d8-46c7-bafd-beac9fc796c0","appActivity":"com.google.android.apps.chrome.ChromeTabbedActivity"}
info: [debug] Getting device API level
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv shell "getprop ro.build.version.sdk"
info: [debug] Device is at API Level 19
info: [debug] executing cmd: E:\AndriodSDK\platform-tools\adb.exe -s E561Lv shell "am start -S -n com.android.chrome/com.google.android.apps.chrome.ChromeTabbedActivity"
info: [debug] Responding to client with success: {"status":0,"value":"Successfully launched the app.","sessionId":"855d1e11-a3d8-46c7-bafd-beac9fc796c0"}
info: <-- POST /wd/hub/session/855d1e11-a3d8-46c7-bafd-beac9fc796c0/appium/device/start_activity 200 1723.267 ms - 104 {"status":0,"value":"Successfully launched the app.","sessionId":"855d1e11-a3d8-46c7-bafd-beac9fc796c0"}
info: --> POST /wd/hub/session/855d1e11-a3d8-46c7-bafd-beac9fc796c0/url {"url":"http://www.baidu.com,sessionId:855d1e11-a3d8-46c7-bafd-beac9fc796c0"}
info: [debug] Responding to client that a method is not implemented
info: <-- POST /wd/hub/session/855d1e11-a3d8-46c7-bafd-beac9fc796c0/url 501 2.480 ms - 158

共收到 9 条回复 时间 点赞

有 get 这个方法?我在 python 客户端的 webdriver.py 里面没搜到。。。

#1 楼 @eurekasaber 有的, 应该是从 selenium 的库里面继承过来的。
desired_caps={
'platformName': 'Android',
'platformVersion': '4.4',
'deviceName': 'bianbianmm',
'browserName': 'Chrome',
'udid': 'E561Lv',
}

driver = webdriver.Remote('http://localhost:4726/wd/hub',desired_caps)
driver.get('http://www.baidu.com')

经测试可行...

但是我本帖中的方法就不行,实在是想不明白

#2 楼 @honeybadger 我猜不能直接这样用吧。。。因为你是想在打开的浏览器里面打开网址

1、帖子格式需要编辑。
2、Chrome 已经打开了,日志里这步也没问题,所以 startActivity 没有失败,你自己也说了失败的是下一步driver.get()
3、Chrome 打开在 Native 部分,driver.get()需要在切换到 WebView 里再用。
4、https://testerhome.com/topics/2307

#4 楼 @sanlengjingvv 好的,谢谢提醒啊,我去看下,后面再修改一下格式。

#4 楼 @sanlengjingvv 终于明白了 webview 和 native_app 的区别,多谢指点啊。调试以后终于可以运行了。

#3 楼 @eurekasaber 其实 start_activity 已经起作用了,但是打印了一下 context, 显示这样开启的方式是 native_app, 而 driver.get() 适用于 webview, 所以报错了。 我自己没搞清楚 webview 和 native_app 的区别😭

#7 楼 @honeybadger 目前使用 start_activity 跨 app,第二个 app 无法元素定位,因为 driver 对应的还是第一个 app...目前没有特别好的解决方案,不知道你这边是怎么解决的

学习了👍

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册