| app自动化测试之Andriod微信小程序的自动化测试

获取更多技术文章分享

随着微信小程序的功能和生态日益完善,很多公司的小程序项目页面结构越来越多,业务逻辑也越来越复杂。如何做好小程序的自动化测试就成为测试同学普遍面临的一大痛点难题。

微信小程序

小程序内嵌于微信内部,页面包含 Native 原生元素和 Web 元素,相当于混合应用。并且,小程序 Web 部分是基于腾讯的 X5 内核开发的,也是特殊的 WebView。那么,对微信小程序进行自动化测试,包括操作原生应用、WebView、基于微信官方自动化 SDK。

WebView页面元素获取

使用元素定位工具:

  • weditor
  • weditor安装方式

  • pip install weditor

参考文档:https://github.com/alibaba/web-editor
使用 chrome inspect 定位时,解析元素是把页面解析为了 html 页面,使用 weditor,则会把页面解析为原生页面,而 Appium 在操作元素时,也是把页面解析成了原生去操作的(切换 webview 除外)
![](https://img-blog.csdnimg.cn/img_convert/a5171783f8a562898251fbe787ae64af.png)


Mac:

adb shell dumpsys activity top| grep ACTIVITY


Windows:

adb shell dumpsys activity top| findstr ACTIVITY


![](https://img-blog.csdnimg.cn/img_convert/f0d39e440d16813bcc8a17ab3420b19b.png)

![](https://img-blog.csdnimg.cn/img_convert/622980b1c434ed795e8ef82ffe15b432.png)

from time import sleep
from appium import webdriver

class TestDemo:
def setup(self):
self.desire_cap= {
‘automationName’: “uiautomator2”,
“platformName”: “Android”,
“deviceName”: “wechat”,
“appPackage”: “com.tencent.mm”,
“appActivity”: “.ui.LauncherUI”,
“noReset”: “true”,
‘chromeOptions’: {‘androidProcess’: ‘com.tencent.mm:appbrand0’}
}
# androidProcess:webview是独立进程的,导致无法获取,需要在 chromeOptions 添加 androidProcess 即可
self.driver=webdriver.Remote(“http://127.0.0.1:4723/wd/hub”,self.desire_cap)
self.driver.implicitly_wait(20)

def teardown(self):
        self.driver.quit()
def test_demo(self):
        # 刚打开微信时,页面加载时间未知,
                # 需要通过find_element触发隐式等待,防止后续操作失败
                        self.driver.find_element_by_xpath("//*[@text='通讯录']")
                                size = self.driver.get_window_size()
                                        # 获取当前屏幕的宽、高
                                                width = size.get("width")
                                                        height = size.get("height")
                                                                # 滑动打开小程序页面
                                                                        self.driver.swipe((width / 2), int((height * 0.2)), (width / 2), (height * 0.8), 2000)
                                                                                self.driver.find_element_by_xpath("//*[@resource-id='com.tencent.mm:id/gam' and @text='雪球']").click()
                                                                                        sleep(5)
                                                                                                print(self.driver.contexts,'第一次打印')
                                                                                                        self.driver.find_element_by_xpath("//*[@content-desc='搜索股票信息/代码']/..").click()
                                                                                                                self.driver.find_element_by_xpath('//*[@text="请输入股票名称/代码"]').send_keys("阿里巴巴")
                                                                                                                        text = self.driver.find_element_by_xpath('//*[@content-desc="阿里巴巴"]')
                                                                                                                                assert text

- 模拟器:Genymotion
- - 系统版本:8.1
- - 微信版本:7.0.15
- - 小程序:雪球
- - http://appium.io/docs/en/writing-running-appium/caps/
- - https://sites.google.com/a/chromium.org/chromedriver/capabilities
- - https://sites.google.com/a/chromium.org/chromedriver/getting-started/getting-started---android
小程序的测试方法有很多种,因为微信版本经常升级,webview的设置也会发生变化,所以可用的测试方法可能会因为每个版本而不同。更多可用测试方法可参考测试人论坛:https://ceshiren.com/t/topic/12403


内容全面升级,4 个月 20+ 项目实战强化训练,资深测试架构师、开源项目作者亲授 BAT 大厂前沿最佳实践,带你一站式掌握测试开发必备核心技能(对标阿里P6+,年薪50W+)!直推 BAT 名企测试经理,普遍涨薪 50%+!

### 
⬇️ 点击“阅读原文”,提升测试核心竞争力!
[原文链接](https://mp.weixin.qq.com/s?__biz=MzU3NDM4ODEzMg==&mid=2247496673&idx=1&sn=70f3dd374768de645f98a1ce28e1465b&chksm=fd31912aca46183c01b580f04fdf64ee6061941870efeaeef6fe0c43852b57627f4bbd7880e5#rd) 

[获取更多技术文章分享](https://qrcode.testing-studio.com/f?from=CSDN&url=https://ceshiren.com/t/topic/16586)


版权声明:本文为hogwarts_2022原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。