前言
最近微信的小程序越来越多了,随之带来的问题是:小程序如何做自动化测试?
本篇以摩拜小程序为例,介绍如何定位小程序里面的元素
运行环境:
android 7.0
appium v1.7.1
webview进程
1.小程序和微信公众号还不太一样,基本思路差不多,先配置:chromeOptions
‘chromeOptions‘: {‘androidProcess‘: ‘com.tencent.mm:appbrand0‘}
2.androidProcess进程可以通过adb shell去查看,先点开摩拜小程序,然后进adb shell
C:Usersadmin>adb shell
HWBND-H:/ $ dumpsys activity top | grep ACTIVITY
ACTIVITY com.tencent.mm/.plugin.appbrand.ui.AppBrandUI d0f2ff4 pid=9104
HWBND-H:/ $ ps 9104
USER PID PPID VSIZE RSS WCHAN PC NAME
u0_a119 9104 495 2706272 283720 0 0000000000 S com.tencent.mm:appbrand0
HWBND-H:/ $
3.com.tencent.mm:appbrand0 这个就是我们要找到的了
摩拜小程序
1.先点开微信首页,下滑页面,出现小程序菜单后点击‘摩拜’
2.代码实现:
def swipeDown(driver, t=500, n=1):
‘‘‘向下滑动屏幕‘‘‘
l = driver.get_window_size()
x1 = l[‘width‘] * 0.5 # x坐标
y1 = l[‘height‘] * 0.25 # 起始y坐标
y2 = l[‘height‘] * 0.75 # 终点y坐标
for i in range(n):
driver.swipe(x1, y1, x1, y2,t)
# 向下滑动
swipeDown(driver)
小程序操作
1.进入小程序后点右下角的头像,这里可以通过坐标去点击
由于每个手机分辨率不一样,这里可以直接去计算下图标在屏幕的比例
2.接着点击“发红包赚赏金”
# 注意,这里是不需要切换的,别踩坑了!!!!!!
# driver.switch_to.context(‘WEBVIEW_com.tencent.mm:tools‘) # 不要加这行
time.sleep(3)
# tap触摸右下角那个菜单坐标 [873,1654], [1080,1861]
driver.tap([(873, 1654), (1080, 1861)], 500)
# 点发红包赚赏金
driver.find_element_by_accessibility_id("发红包赚赏金").click()
参考代码
# coding:utf-8
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
import time
desired_caps = {
‘platformName‘: ‘Android‘,
‘platformVersion‘: ‘7.0‘,
‘deviceName‘: ‘A5RNW18316011440‘,
‘appPackage‘: ‘com.tencent.mm‘,
‘appActivity‘: ‘.ui.LauncherUI‘,
‘automationName‘: ‘Appium‘,
# ‘unicodeKeyboard‘: True,
# ‘resetKeyboard‘: True,
‘noReset‘: True,
‘chromeOptions‘: {‘androidProcess‘: ‘com.tencent.mm:appbrand0‘}
}
driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps)
time.sleep(10)
def swipeDown(driver, t=500, n=1):
‘‘‘向下滑动屏幕‘‘‘
l = driver.get_window_size()
x1 = l[‘width‘] * 0.5 # x坐标
y1 = l[‘height‘] * 0.25 # 起始y坐标
y2 = l[‘height‘] * 0.75 # 终点y坐标
for i in range(n):
driver.swipe(x1, y1, x1, y2,t)
# 向下滑动
swipeDown(driver)
time.sleep(2)
# 点开小程序
driver.find_elements_by_id("com.tencent.mm:id/r9")[0].click()
time.sleep(4)
print(driver.contexts)
# 注意,这里是不需要切换的,别踩坑了!!!!!!
# driver.switch_to.context(‘WEBVIEW_com.tencent.mm:tools‘)
time.sleep(3)
# tap触摸右下角那个菜单坐标 [873,1654], [1080,1861]
driver.tap([(873, 1654), (1080, 1861)], 500)
# 点发红包赚赏金
driver.find_element_by_accessibility_id("发红包赚赏金").click()