本文为霍格沃兹测试学院测试大咖公开课《微信小程序自动化测试》图文整顿精华版。
随着微信小程序的性能和生态日益完善,很多公司的产品业务状态逐步从 App 延升到微信小程序、微信公众号等。小程序我的项目页面越来越多,业务逻辑也越来越简单,全手工测试已无奈满足快速增长的业务需要。
然而,因为小程序自身的一些个性,导致业界目前不足成熟欠缺的解决方案,总会呈现各种问题(包含腾讯微信官网提供的自动化工具)。如何做好小程序的自动化测试就成为测试同学当下广泛面临的一个痛点难题。
本节课就次要分享下微信小程序自动化测试的一些最佳实际心得,包含微信小程序的根本测试技术和操作方法,以及如何利用 Appium 的 WebView 测试技术 + adb proxy 实现微信小程序的自动化测试(可能是目前最实用的小程序自动化测试技术),并附上 Python 版源码。
小程序运行环境
平台差别:只管各运行环境是十分相似的,然而还是有些许区别:
JavaScript 语法和 API 反对不统一:语法上开发者能够通过开启 ES6 转 ES5 的性能来躲避(详情);此外,小程序根底库内置了必要的Polyfill,来补救API的差别。
WXSS 渲染体现不统一:只管能够通过开启款式补全来躲避大部分的问题,还是倡议开发者须要在 iOS 和 Android 上别离查看小程序的实在体现。
微信小程序技术架构
- 微信小程序技术架构如下图所示:
应用 Chrome 调试小程序
- 用 Chrome 浏览器提供的 inspect 剖析工具,在浏览器中输出如下地址:
chrome://inspect/#devices
- 应用 Chrome 浏览器查看手机上关上的 WebView 过程与根本信息:
- 能够应用 chrome inspect 剖析微信小程序的控件构造与布局:
- 应用 console 执行本人的 JavaScript 代码:
小程序的性能测试
- 这里附一张小程序性能测试图:
微信小程序的自动化测试
- 微信小程序自动化测试的关键步骤
- Native 原生自动化形式。
- 应用 Appium 即可实现,毛病就是控件定位不够精确,无奈深刻小程序外部;
- Webview 自动化形式:能够获取更多小程序外部品质数据。
- 设置 chromedriver 正确版本
- 设置 chrome option 传递给 chromedriver
- 应用 adb proxy 解决 fix chromedriver 的 bug
为什么依然有很多人搞不定?
- 低版本的 chromedriver 在高版本的手机上有 bug
- chromedriver 与微信定制的 chrome 内核对接实现上有问题
解决方案:如何 fix it?
- chromedriver 没有应用 adb 命令,而是应用了 adb 协定
- 参考课程中提到的 adb proxy 源代码
源码-微信小程序自动化测试 Python 版代码示例
class TestWXMicroWebView: # 为了演示不便,未应用page object模式 def setup(self): caps = {} caps["platformName"] = "android" caps["deviceName"] = "测试人社区 ceshiren.com" caps["appPackage"] = "com.tencent.mm" caps["appActivity"] = "com.tencent.mm.ui.LauncherUI" caps["noReset"] = True caps['unicodeKeyboard'] = True caps['resetKeyboard'] = True caps['chromedriverExecutable'] = \ '/Users/seveniruby/projects/chromedriver/chromedrivers/chromedriver_78.0.3904.11' # options = ChromeOptions() # options.add_experimental_option('androidProcess', 'com.tencent.mm:appbrand0') caps['chromeOptions'] = { 'androidProcess': 'com.tencent.mm:appbrand0' } caps['adbPort'] = 5038 self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self.driver.implicitly_wait(30) self.driver.find_element(By.XPATH, "//*[@text='通讯录']") self.driver.implicitly_wait(10) self.enter_micro_program() print(self.driver.contexts) def enter_micro_program(self): # 原生自动化测试 size = self.driver.get_window_size() self.driver.swipe(size['width'] * 0.5, size['height'] * 0.4, size['width'] * 0.5, size['height'] * 0.9) self.driver.find_element(By.CLASS_NAME, 'android.widget.EditText').click() self.driver.find_element(By.XPATH, "//*[@text='勾销']") self.driver.find_element(By.CLASS_NAME, "android.widget.EditText").send_keys("雪球") self.driver.find_element(By.CLASS_NAME, 'android.widget.Button') self.driver.find_element(By.CLASS_NAME, 'android.widget.Button').click() self.driver.find_element(By.XPATH, "//*[@text='自选']") def find_top_window(self): for window in self.driver.window_handles: print(window) if ":VISIBLE" in self.driver.title: print(self.driver.title) else: self.driver.switch_to.window(window) def test_search_webview(self): # 进入webview self.driver.switch_to.context('WEBVIEW_xweb') self.driver.implicitly_wait(10) self.find_top_window() # css定位 self.driver.find_element(By.CSS_SELECTOR, "[src*=stock_add]").click() # 期待新窗口 WebDriverWait(self.driver, 30).until(lambda x: len(self.driver.window_handles) > 2) self.find_top_window() self.driver.find_element(By.CSS_SELECTOR, "._input").click() # 输出 self.driver.switch_to.context("NATIVE_APP") ActionChains(self.driver).send_keys("alibaba").perform() # 点击 self.driver.switch_to.context('WEBVIEW_xweb') self.driver.find_element(By.CSS_SELECTOR, ".stock__item") self.driver.find_element(By.CSS_SELECTOR, ".stock__item").click()
小程序自动化测试须要跨过的几个坎
- WebView 开关 /x5 内核调试开关
- ChromeOption 选项须要填写
- WebView 版本和 ChromeDriver 版本对应问题
- 低版本 ChromeDriver 须要修复 ps 命令的 bug
- Context API 有肯定的提早须要期待
以上,更多内容(ChromeDriver 的材料与 WebView 自动化要害代码,Appium 配置,mapping.json,常见谬误等),请点击下方链接入群获取。