一、前置说明
测试环境:
- appium版本:2.2.2
- Appium-Python-Client: 3.1.0
- 微信版本: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()