写点什么

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

  • 2022 年 9 月 23 日
    北京
  • 本文字数:1477 字

    阅读完需:约 5 分钟

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

微信小程序

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

WebView 页面元素获取

使用元素定位工具:

  • weditor

weditor 安装方式

pip install weditor
复制代码

参考文档:GitHub - alibaba/web-editor: web editor for atx 2 使用 chrome inspect 定位时,解析元素是把页面解析为了 html 页面,使用 weditor,则会把页面解析为原生页面,而 Appium 在操作元素时,也是把页面解析成了原生去操作的(切换 webview 除外)

Mac:

adb shell dumpsys activity top| grep ACTIVITY
复制代码

Windows:

adb shell dumpsys activity top| findstr ACTIVITY
复制代码




from time import sleepfrom 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
复制代码

小程序的测试方法有很多种,因为微信版本经常升级,webview 的设置也会发生变化,所以可用的测试方法可能会因为每个版本而不同。更多可用测试方法可参考测试人论坛:微信小程序自动化测试常用方法 14


更多学习资料戳下方!!!

用户头像

社区:ceshiren.com 2022.08.29 加入

微信公众号:霍格沃兹测试开发 提供性能测试、自动化测试、测试开发等资料、实事更新一线互联网大厂测试岗位内推需求,共享测试行业动态及资讯,更可零距离接触众多业内大佬

评论

发布
暂无评论
干货| app自动化测试之Andriod微信小程序的自动化测试_测试_测吧(北京)科技有限公司_InfoQ写作社区