文章目录
- 一、前言
- 二、环境准备:
- 三、通过 appium 实现对微信小程序进行对应的操作
- 简单代码实现
- 关键技术点
- 常见问题与解决方案
一、前言
微信小程序、微信公众号如何实现 app 自动化测试?
- 微信小程序、微信公众号其实就是混合app(原生控件+H5的页面)
- 原生控件元素与 H5 元素进行切换,需要切换上下文
driver.switch_to.context(’’)
微信小程序、微信公众号属于微信应用程序里面的,如何唯一操作被测的微信小程序/微信公众号?
通过 adb 命令 最终获取进程名称
- 命令01:adb shell dumpsys activity top | findstr ACTIVITY
- 命令02:adb shell ps 进程id
二、环境准备:
- 安装 Appium:
npm install -g appium@latest
- - 安装 Appium Python 客户端:
pip install Appium-Python-Client
- 确保 ADB 可用:
配置 Android SDK 的 platform-tools 到系统 PATH,确保 adb devices 能识别设备。 - 微信开发者工具:
启用微信的 WebView 调试(访问 http://debugxweb.qq.com,勾选 “开启 WebView 调试”)。 - 真机连接注意:
- 1、打开开发者选项-开启usb调试
- 2、真机必须运行接受adb命令 ---- 仅充电模式,允许ADB调试
- 3、选择USB配置----MTP 多媒体传输
三、通过 appium 实现对微信小程序进行对应的操作
简单代码实现
# -*- coding=utf-8 -*-
import time
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec
"""
h5 元素定位
"""
# 设置操作终端的配置参数
desired_caps = dict(
platformName='Android', # 指定操作系统
platformVersion='12',# 指定操作系统版本
automationName='Uiautomator2',# 默认框架
deviceName='4DV0225114004817',# 指定设备名称
appPackage='com.tencent.mm',# 被操作的应用程序包名
appActivity='com.tencent.mm.ui.LauncherUI',# 启动页面
noReset='true',# true--不重置 false--重置
skipServerInstallation= True, # 关键配置:跳过测试 APK 安装
uiautomator2ServerInstallTimeout= 30000, # 超时时间设为30秒
# 启用 X5 内核,而不是用 android.system.webview
recreateChromeDriverSessions=True,
chromedriverExecutable = "F:\Pycharm\AppAuto\Class\class07\chromedriver.exe", # chrome 浏览器驱动所在路径
# 切换webview,小程序是哪个webview->指定微信小程序的进程名称
chromeOptions={'androidProcess':"com.tencent.mm:appbrand1"}
)
# 发送命令给 appium server
driver = webdriver.Remote('http://127.0.0.1:4723', options=UiAutomator2Options().load_capabilities(desired_caps))
print("启动会话,打开微信")
time.sleep(5)
print("已打开微信,开始进入小程序")
# 实现下滑
def swipe_down():
# 获取整个app屏幕的大小
x = driver.get_window_size()["width"]
y = driver.get_window_size()["height"]
# 下滑
driver.swipe(start_x=x * 0.5, end_x=x * 0.5, start_y=y * 0.2, end_y=y * 0.9, duration=1000)
# 下滑
swipe_down()
print("下滑完成,开始进入小程序")
# 打开指定的某个小程序
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().text("若风里有诗")').click()
print("开始打开微信小程序-若风里有诗")
# 获取所有上下文
print(driver.contexts)
# 输出上下文的内容 ['NATIVE_APP', 'WEBVIEW_com.tencent.mm:toolsmp']
driver.switch_to.context('WEBVIEW_com.tencent.mm:toolsmp')
# [u'NATIVE_APP', u'WEBVIEW_com.tencent.mm:appbrand0', u'WEBVIEW_com.tencent.mm:support', u'WEBVIEW_com.tencent.mm:tools', u'WEBVIEW_com.tencent.mm:appbrand1']
# driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand0') #切换上下文为WEBVIEW_com.tencent.mm:appbrand0
# driver.find_element_by_xpath("/html/body/wx‐view/wx‐view[4]/wx‐fo
关键技术点
- WebView 上下文切换:
小程序在 WebView 中运行,需要通过 driver.contexts 获取所有上下文,然后切换到 WebView 上下文才能操作网页元素。 - ChromeDriver 版本匹配:
确保使用的 ChromeDriver 版本与微信内置的 Chrome 内核版本兼容。
可通过以下方式获取微信内核版本:versions = driver.execute_script("return window.navigator.userAgent")
- 元素定位策略:
在原生上下文使用 Android 定位方式(如 ID、XPath)。
在 WebView 上下文使用网页定位方式(如 CSS Selector、XPath)。 - 处理小程序加载延迟:
小程序首次加载较慢,建议添加足够的等待时间或显式等待。
常见问题与解决方案
- 找不到 WebView 上下文:
确保微信已开启 WebView 调试(访问 http://debugxweb.qq.com)。
添加更多等待时间,确保小程序完全加载。 - ChromeDriver 版本不匹配:
根据微信内核版本下载对应 ChromeDriver:ChromeDriver 下载。
通过 chromedriverExecutableDir 配置 ChromeDriver 自动下载路径。 - 元素定位失败:
使用 Appium Inspector 工具辅助定位元素。
注意上下文切换,确保在正确的上下文(原生或 WebView)中定位元素。 - 微信登录状态丢失:
使用 noReset=True 保持登录状态。
若需重新登录,可通过自动化方式输入账号密码或扫码。