最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Mac中初始化搭建appium的Python的运行环境

Mac crifan 453浏览 0评论
折腾:
【记录】跑通运行代码:win中的app微信公众号采集
期间,看到代码中,用到:appium
from appium import webdriver
所以先去安装库:
【已解决】Mac中安装appium的Python库
然后调试发现代码:
from appium import webdriver

server = 'http://localhost:4723/wd/hub'

    driver = webdriver.Remote(server, desired_caps)
需要去运行appium的服务
所以再去搞清楚,如何启动appium的server:
【已解决】Mac中下载安装和启动appium的server服务
再去运行代码,看看能否跑通:
【已解决】Appium报错:WebDriverException Original error Could not find a connected Android device
此处,第一次跑通的完整配置:
from appium import webdriver
server = 'http://localhost:4723/wd/hub’

Android_Redmi10X_DeviceName = "crifan Redmi10X"
Android_Redmi10X_UDID = "orga4pmzee4ts47t"

    desired_caps = {
        "platformName": "Android",
        "deviceName": Android_Redmi10X_DeviceName,
        "udid": Android_Redmi10X_UDID,
        "appPackage": "com.tencent.mm",
        "appActivity": ".ui.LauncherUI",
        "noReset": True
    }

    desired_caps['chromeOptions'] = {'androidProcess': 'com.tencent.mm:tools'}
    desired_caps['noReset'] = True
    driver = webdriver.Remote(server, desired_caps)
结果:
可以正常运行了:
先是:
Appium触发了 此处的安卓手机,弹框安装一些必要的APP:
  • Appium Settings
  • ios.appium.uiautomator2.server
    •  
  • io.appium.uiautomator2.server.test
然后就打开了微信app:
安装完毕后,代码正常能继续运行,而不报错了:
也注意到了,appium中显示正常连接设备的log了:
[debug] [35m[WD Proxy][39m Proxying [POST /session] to [POST http://127.0.0.1:8200/wd/hub/session] with body: {"capabilities":{"firstMatch":[{"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformName":"Android","deviceName":"crifan Redmi10X","udid":"orga4pmzee4ts47t","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI","noReset":true,"chromeOptions":{"androidProcess":"com.tencent.mm:tools"}},"platformName":"Android","deviceName":"orga4pmzee4ts47t","udid":"orga4pmzee4ts47t","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI","noReset":true,"chromeOptions":{"androidProcess":"com.tencent.mm:tools"},"deviceUDID":"orga4pmzee4ts47t"}],"alwaysMatch":{}}}
[debug] [35m[WD Proxy][39m Got response with status 200: {"sessionId":"2544ad20-d73b-4673-9cd0-e287ccd7453e","value":{"capabilities":{"firstMatch":[{"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformName":"Android","deviceName":"crifan Redmi10X","udid":"orga4pmzee4ts47t","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI","noReset":true,"chromeOptions":{"androidProcess":"com.tencent.mm:tools"}},"platformName":"Android","deviceName":"orga4pmzee4ts47t","udid":"orga4pmzee4ts47t","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI","noReset":true,"chromeOptions":{"androidProcess":"com.tencent.mm:tools"},"deviceUDID":"orga4pmzee4ts47t"}],"alwaysMatch":{}},"sessionId":"2544ad20-d73b-4673-9cd0-e287ccd7453e"}}
[info] [35m[WD Proxy][39m Determined the downstream protocol as 'W3C'
[debug] [35m[WD Proxy][39m Proxying [GET /appium/device/info] to [GET http://127.0.0.1:8200/wd/hub/session/2544ad20-d73b-4673-9cd0-e287ccd7453e/appium/device/info] with no body
[debug] [35m[WD Proxy][39m Got response with status 200: {"sessionId":"2544ad20-d73b-4673-9cd0-e287ccd7453e","value":{"androidId":"edca4ce87a26685a","apiVersion":"29","bluetooth":{"state":"ON"},"brand":"Redmi","carrierName":"","displayDensity":440,"locale":"zh_CN","manufacturer":"Xiaomi","model":"M2004J7AC","networks":[{"capabilities":{"SSID":null,"linkDownBandwidthKbps":1048576,"linkUpstreamBandwidthKbps":1048576,"networkCapabilities":"NET_CAPABILITY_NOT_METERED,NET_CAPABILITY_INTERNET,NET_CAPABILITY_NOT_RESTRICTED,NET_CAPABILITY_TRUSTED,NET_CAPABILITY_NOT_VPN,NET_CAPABILITY_VALIDATED,NET_CAPABILITY_NOT_ROAMING,NET_CAPABILITY_FOREGROUND,NET_CAPABILITY_NOT_CONGESTED,NET_CAPABILITY_NOT_SUSPENDED","signalStrength":-19,"transportTypes":"TRANSPORT_WIFI"},"detailedState":"CONNECTED","extraInfo":null,"isAvailable":true,"isConnected":true,"isFailover":false,"isRoaming":false,"state":"CONNECTED","subtype":0,"subtypeName":"","type":1,"typeName":"WIFI"}],"platformVersion":"10","realDisplaySize":"1080x2400","timeZone":"Asia/Shanghai"}}
[debug] [35m[ADB][39m Running '/Users/xxx/Library/Android/sdk/platform-tools/adb -P 5037 -s orga4pmzee4ts47t shell dumpsys window'
[info] [35m[AndroidDriver][39m Screen already unlocked, doing nothing
[info] [35m[UiAutomator2][39m Starting 'com.tencent.mm/.ui.LauncherUI and waiting for 'com.tencent.mm/.ui.LauncherUI'
[debug] [35m[ADB][39m Running '/Users/xxx/Library/Android/sdk/platform-tools/adb -P 5037 -s orga4pmzee4ts47t shell am start -W -n com.tencent.mm/.ui.LauncherUI -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000'[debug] [35m[WD Proxy][39m Proxying [GET /appium/device/pixel_ratio] to [GET http://127.0.0.1:8200/wd/hub/session/2544ad20-d73b-4673-9cd0-e287ccd7453e/appium/device/pixel_ratio] with no body
[debug] [35m[WD Proxy][39m Got response with status 200: {"sessionId":"2544ad20-d73b-4673-9cd0-e287ccd7453e","value":2.75}
[debug] [35m[WD Proxy][39m Matched '/appium/device/system_bars' to command name 'getSystemBars'
[debug] [35m[WD Proxy][39m Proxying [GET /appium/device/system_bars] to [GET http://127.0.0.1:8200/wd/hub/session/2544ad20-d73b-4673-9cd0-e287ccd7453e/appium/device/system_bars] with no body
[debug] [35m[WD Proxy][39m Got response with status 200: {"sessionId":"2544ad20-d73b-4673-9cd0-e287ccd7453e","value":{"statusBar":69}}
[debug] [35m[WD Proxy][39m Matched '/window/current/size' to command name 'getWindowSize'
[debug] [35m[WD Proxy][39m Proxying [GET /window/current/size] to [GET http://127.0.0.1:8200/wd/hub/session/2544ad20-d73b-4673-9cd0-e287ccd7453e/window/current/size] with no body
[debug] [35m[WD Proxy][39m Got response with status 200: {"sessionId":"2544ad20-d73b-4673-9cd0-e287ccd7453e","value":{"height":2201,"width":1080}}
[info] [35m[Appium][39m New AndroidUiautomator2Driver session created successfully, session c70a00ea-47ee-4f24-8c28-cd37f5afedc1 added to master session list
[debug] [35m[BaseDriver][39m Event 'newSessionStarted' logged at 1624587287741 (10:14:47 GMT+0800 (中国标准时间))
[debug] [35m[W3C (c70a00ea)][39m Cached the protocol value 'W3C' for the new session c70a00ea-47ee-4f24-8c28-cd37f5afedc1
[debug] [35m[W3C (c70a00ea)][39m Responding to client with driver.createSession() result: {"capabilities":{"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformName":"Android","deviceName":"crifan Redmi10X","udid":"orga4pmzee4ts47t","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI","noReset":true,"chromeOptions":{"androidProcess":"com.tencent.mm:tools"}},"platformName":"Android","deviceName":"orga4pmzee4ts47t","udid":"orga4pmzee4ts47t","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI","noReset":true,"chromeOptions":{"androidProcess":"com.tencent.mm:tools"},"deviceUDID":"orga4pmzee4ts47t","deviceApiLevel":29,"platformVersion":"10","deviceScreenSize":"1080x2400","deviceScreenDensity":440,"deviceModel":"M2004J7AC","deviceManufacturer":"Xiaomi","pixelRatio":2.75,"statBarHeight":69,"viewportRect":{"left":0,"top":69,"width":1080,"height":2132}}}
[info] [35m[HTTP][39m [37m<-- POST /wd/hub/session [39m[32m200[39m [90m17543 ms - 1005[39m
[info] [35m[HTTP][39m [90m[39m[warn] [35m[BaseDriver][39m Shutting down because we waited 60 seconds for a command
[warn] [35m[Appium][39m Closing session, cause was 'New Command Timeout of 60 seconds expired. Try customizing the timeout using the 'newCommandTimeout' desired capability'
[info] [35m[Appium][39m Removing session 'c70a00ea-47ee-4f24-8c28-cd37f5afedc1' from our master session list
[debug] [35m[UiAutomator2][39m Deleting UiAutomator2 session
[debug] [35m[UiAutomator2][39m Deleting UiAutomator2 server session
[debug] [35m[WD Proxy][39m Matched '/' to command name 'deleteSession'
[debug] [35m[WD Proxy][39m Proxying [DELETE /] to [DELETE http://127.0.0.1:8200/wd/hub/session/2544ad20-d73b-4673-9cd0-e287ccd7453e] with no body
[info] [35m[WD Proxy][39m socket hang up
[warn] [35m[UiAutomator2][39m Did not get confirmation UiAutomator2 deleteSession worked; Error was: UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: socket hang up
[debug] [35m[ADB][39m Running '/Users/xxx/Library/Android/sdk/platform-tools/adb -P 5037 -s orga4pmzee4ts47t shell am force-stop com.tencent.mm'
[debug] [35m[Logcat][39m Stopping logcat capture
[debug] [35m[ADB][39m Removing forwarded port socket connection: 8200
[debug] [35m[ADB][39m Running '/Users/xxx/Library/Android/sdk/platform-tools/adb -P 5037 -s orga4pmzee4ts47t forward --remove tcp:8200'
[info] [35m[UiAutomator2][39m Restoring hidden api policy to the device default configuration
[debug] [35m[ADB][39m Running '/Users/xxx/Library/Android/sdk/platform-tools/adb -P 5037 -s orga4pmzee4ts47t shell 'settings delete global hidden_api_policy_pre_p_apps;settings delete global hidden_api_policy_p_apps;settings delete global hidden_api_policy''
如此,即可正常,第一次,初始化appium的Python的开发环境了。
【总结】
此处Mac中,搭建Appium的Python环境的步骤:
(1)appium的server:Mac的appium桌面客户端
去appium的官网,下载appium的(Mac)客户端
地址:
http://appium.io/
-》
此处是:
Appium-1.21.0-mac.zip
下载后,解压,得到 Appium.app
安装:拖动到 应用程序目录
双击打开
点击 Start Server
看到log显示:
Appium REST http interface listener started on 0.0.0.0:4723
说明Appium的server端,启动成功
(2)appium的client端:第一次初始化
先安装python库:
pip install Appium-Python-Client
再去写(测试)代码:
from appium import webdriver
server = 'http://localhost:4723/wd/hub'
Android_Redmi10X_DeviceName = "crifan Redmi10X"
Android_Redmi10X_UDID = "orga4pmzee4ts47t"

。。。

    desired_caps = {
        # 如果换手机,需要改 deviceName、udid
        "platformName": "Android",
        "deviceName": Android_Redmi10X_DeviceName,
        "udid": Android_Redmi10X_UDID,
        "appPackage": "com.tencent.mm",
        "appActivity": ".ui.LauncherUI",
        "noReset": True
    }
    desired_caps['chromeOptions'] = {'androidProcess': 'com.tencent.mm:tools'}
    desired_caps['noReset'] = True
    driver = webdriver.Remote(server, desired_caps)
其中:
  • Capability的参数
    • 最核心的三个
      • platformName:Android
        • 表示安卓平台
      • udid:安卓设备的序列号,设备唯一编号
        • 可以通过 adb devices 找到
          • 比如:
            • # adb devices
              • List of devices attached
              • orga4pmzee4ts47t    device
          • 中的 orga4pmzee4ts47t
      • deviceName:随便填个值(当然最好是见名知意用户看得懂的值)
        • 比如
          • “deviceName”: “crifan Redmi10X”,
    • 详见
第一次初始化时,会在手机端(弹框,要点击继续)安装三个app:
  • Appium Settings
  • ios.appium.uiautomator2.server
  • io.appium.uiautomator2.server.test
然后即可正常工作:
此处打开了微信app

转载请注明:在路上 » 【已解决】Mac中初始化搭建appium的Python的运行环境

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.190 seconds, using 23.33MB memory