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

微信小程序

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

WebView页面元素获取

应用元素定位工具:

  • weditor
  • weditor装置形式

    pip install weditor

参考文档:https://github.com/alibaba/we...
应用 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 webdriverclass 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/writ...
  • https://sites.google.com/a/ch...
  • https://sites.google.com/a/ch...

小程序的测试方法有很多种,因为微信版本常常降级,webview的设置也会发生变化,所以可用的测试方法可能会因为每个版本而不同。更多可用测试方法可参考测试人论坛:https://ceshiren.com/t/topic/...