Appium启动微信失败的解决办法

简介: Appium启动微信失败的解决办法

一、前置说明


测试环境

  1. appium版本:2.2.2
  2. Appium-Python-Client: 3.1.0
  3. 微信版本:8.0.43


在使用下面代码测试启动微信时,一直不能把微信启动起来:

import time
import pytest
from driver.appium.manager import AppiumServersManager
from driver.appium.driver import WebDriver
from libs.cmd_util import cmd
def setup_module():
    import logging
    logging.basicConfig(level=logging.DEBUG)
    # 启动 appium server
    server_manager = AppiumServersManager(tail_log=True)
    server_manager.start()
@pytest.fixture(scope='module')
def driver():
    appium_server_url = 'http://localhost:4723'
    capabilities = {
        "platformName": "Android",
        "automationName": "uiautomator2",
        "deviceName": "9YS0220306003185",
        "appPackage": "com.tencent.mm",
        "appActivity": ".ui.LauncherUI",
        "noReset": "true"
    }
    driver = WebDriver(command_executor=appium_server_url, capabilities=capabilities)
    yield driver
    driver.quit()
def test_wechat(driver):
    time.sleep(2)
    driver.find_element('xpath', "//*[@text='通讯录']").click()

appium server中查找启动微信app的日志,如下:

2023-12-09 23:03:38:762 [AndroidDriver] Screen already unlocked, doing nothing
2023-12-09 23:03:38:762 [AndroidUiautomator2Driver@02b1 (42be6406)] Starting 'com.tencent.mm/.ui.LauncherUI and waiting for 'com.tencent.mm/.ui.LauncherUI'
2023-12-09 23:03:38:762 [ADB] Getting IDs of all 'com.tencent.mm' processes
2023-12-09 23:03:38:762 [ADB] Running 'D:\ProgramFiles\Android\android-sdk\platform-tools\adb.exe -P 5037 -s 9YS0220306003185 shell pgrep -f \(\[\[:blank:\]\]\|\^\)com\.tencent\.mm\(\[\[:blank:\]\]\|\$\)'
2023-12-09 23:03:38:857 [AndroidUiautomator2Driver@02b1 (42be6406)] 'com.tencent.mm' is already running and noReset is enabled. Set forceAppLaunch capability to true if the app must be forcefully restarted on session startup.
2023-12-09 23:03:38:858 [AppiumDriver@9348] New AndroidUiautomator2Driver session created successfully, session 42be6406-da3e-4c45-ab94-3803035e9d8a added to master session list
2023-12-09 23:03:38:858 [AppiumDriver@9348] Event 'newSessionStarted' logged at 1702134218858 (23:03:38 GMT+0800 (中国标准时间))
2023-12-09 23:03:38:858 [AndroidUiautomator2Driver@02b1 (42be6406)] Cached the protocol value 'W3C' for the new session 42be6406-da3e-4c45-ab94-3803035e9d8a
2023-12-09 23:03:38:858 [AndroidUiautomator2Driver@02b1 (42be6406)] Responding to client with driver.createSession() result: {"capabilities":{"platformName":"Android","automationName":"uiautomator2","deviceName":"9YS0220306003185","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI","noReset":true,"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformName":"Android","automationName":"uiautomator2","deviceName":"9YS0220306003185","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI","noReset":true},"deviceUDID":"9YS0220306003185","pixelRatio":"3","statBarHeight":99,"viewportRect":{"left":0,"top":99,"width":1176,"height":2202},"deviceApiLevel":31,"platformVersion":"12","deviceManufacturer":"HUAWEI","deviceModel":"LIO-AN00","deviceScreenSize":"1176x2400","deviceScreenDensity":480}}
2023-12-09 23:03:38:859 [HTTP] <-- POST /session 200 4267 ms - 900


其中有这么一句:


2023-12-09 23:03:38:857 [AndroidUiautomator2Driver@02b1 (42be6406)] ‘com.tencent.mm’ is already running and noReset is enabled. Set forceAppLaunch capability to true if the app must be forcefully restarted on session startup.


二、解决办法


根据日志提示,尝试在 capabilities 添加 "forceAppLaunch": "true" 之后,顺利启动微信。

import time
import pytest
from driver.appium.manager import AppiumServersManager
from driver.appium.driver import WebDriver
from libs.cmd_util import cmd
def setup_module():
    import logging
    logging.basicConfig(level=logging.DEBUG)
    # 启动 appium server
    server_manager = AppiumServersManager(tail_log=True)
    server_manager.start()
@pytest.fixture(scope='module')
def driver():
    appium_server_url = 'http://localhost:4723'
    capabilities = {
        "platformName": "Android",
        "automationName": "uiautomator2",
        "deviceName": "9YS0220306003185",
        "appPackage": "com.tencent.mm",
        "appActivity": ".ui.LauncherUI",
        "noReset": "true",
        "forceAppLaunch": "true",  # 注意:加上这个参数,可以顺利启动微信
    }
    driver = WebDriver(command_executor=appium_server_url, capabilities=capabilities)
    yield driver
    driver.quit()
def test_wechat(driver):
    time.sleep(2)
    driver.find_element('xpath', "//*[@text='通讯录']").click()
目录
相关文章
|
6月前
|
小程序
内网环境中ruoyi若依实现微信小程序授权登录解决办法
内网环境中ruoyi若依实现微信小程序授权登录解决办法
402 0
|
JSON Java 测试技术
Python + Appium 自动化操作微信入门
Python + Appium 自动化操作微信入门
239 0
微信分享报错 wxlog:Error:fail to load Keychain status:-25300 解决办法
微信分享报错 wxlog:Error:fail to load Keychain status:-25300 解决办法
2387 0
|
6月前
|
Web App开发 小程序 Android开发
Appium微信小程序自动化环境准备
Appium微信小程序自动化环境准备
219 1
|
6月前
|
小程序 Android开发
Appium微信小程序自动化之开启webview调试功能方法封装
Appium微信小程序自动化之开启webview调试功能方法封装
202 0
|
JSON Java 测试技术
Python + Appium 自动化操作微信入门看这一篇就够了
Python + Appium 自动化操作微信入门看这一篇就够了
863 0
Python + Appium 自动化操作微信入门看这一篇就够了
|
小程序 索引
微信小程序底部导航栏最多只能显示五个,解决办法
可以使用 scroll-view 实现类似 tabBar 的效果
346 0
|
JavaScript 前端开发
“此图片来自微信公众平台 未经允许不可引用“ 解决办法
“此图片来自微信公众平台 未经允许不可引用“ 解决办法
“此图片来自微信公众平台 未经允许不可引用“ 解决办法
|
Android开发 iOS开发
Android - 安卓设备在微信中播放视频结束后出现广告的解决办法
Android - 安卓设备在微信中播放视频结束后出现广告的解决办法
209 0
|
编解码 Windows
Windows 微信兼容性问题:微信截图放大解决办法。
Windows 微信兼容性问题:微信截图放大解决办法。
1045 0
Windows 微信兼容性问题:微信截图放大解决办法。