软件测试丨app自动化测试之Andriod微信小程序的自动化测试
公众号搜索:TestingStudio 霍格沃兹测试开发的干货都很硬核
随着微信小程序的功能和生态日益完善,很多公司的小程序项目页面结构越来越多,业务逻辑也越来越复杂。如何做好小程序的自动化测试就成为测试同学普遍面临的一大痛点难题。微信小程序
小程序内嵌于微信内部,页面包含 Native 原生元素和 Web 元素,相当于混合应用。并且,小程序 Web 部分是基于腾讯的 X5 内核开发的,也是特殊的 WebView。那么,对微信小程序进行自动化测试,包括操作原生应用、WebView、基于微信官方自动化 SDK。WebView页面元素获取
使用元素定位工具:weditorweditor安装方式pip install weditor 复制代码
参考文档:https://github.com/alibaba/web-editor
使用 chrome inspect 定位时,解析元素是把页面解析为了 html 页面,使用 weditor,则会把页面解析为原生页面,而 Appium 在操作元素时,也是把页面解析成了原生去操作的(切换 webview 除外)
获取小程序进行号(打开小程序)
Mac:adb shell dumpsys activity top| grep ACTIVITY
Windows:adb shell dumpsys activity top| findstr ACTIVITY
获取androidProcess
实战源码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