本文為霍格沃茲測試學院測試大咖公開課《微信小程式自動化測試》圖文整理精華版。
随着微信小程式的功能和生态日益完善,很多公司的産品業務形态逐漸從 App 延升到微信小程式、微信公衆号等。小程式項目頁面越來越多,業務邏輯也越來越複雜,全手工測試已無法滿足快速增長的業務需求。
然而,由于小程式本身的一些特性,導緻業界目前缺乏成熟完善的解決方案,總會出現各種問題(包括騰訊微信官方提供的自動化工具)。如何做好小程式的自動化測試就成為測試同學當下普遍面臨的一個痛點難題。
本節課就主要分享下微信小程式自動化測試的一些最佳實踐心得,包括微信小程式的基本測試技術和操作方法,以及如何利用 Appium 的 WebView 測試技術 + adb proxy 完成微信小程式的自動化測試(可能是目前最實用的小程式自動化測試技術),并附上 Python 版源碼。
小程式運作環境

平台差異:盡管各運作環境是十分相似的,但是還是有些許差別:
JavaScript 文法和 API 支援不一緻:文法上開發者可以通過開啟 ES6 轉 ES5 的功能來規避(詳情);此外,小程式基礎庫内置了必要的Polyfill,來彌補API的差異。
WXSS 渲染表現不一緻:盡管可以通過開啟樣式補全來規避大部分的問題,還是建議開發者需要在 iOS 和 Android 上分别檢查小程式的真實表現。
微信小程式技術架構
- 微信小程式技術架構如下圖所示:
微信小程式自動化測試最佳實踐(附 Python 源碼)
使用 Chrome 調試小程式
- 用 Chrome 浏覽器提供的 inspect 分析工具,在浏覽器中輸入如下位址:
chrome://inspect/#devices
- 使用 Chrome 浏覽器檢視手機上打開的 WebView 程序與基本資訊:
微信小程式自動化測試最佳實踐(附 Python 源碼) - 可以使用 chrome inspect 分析微信小程式的控件結構與布局:
微信小程式自動化測試最佳實踐(附 Python 源碼) - 使用 console 執行自己的 JavaScript 代碼:
微信小程式自動化測試最佳實踐(附 Python 源碼)
小程式的性能測試
- 這裡附一張小程式性能測試圖:
微信小程式自動化測試最佳實踐(附 Python 源碼)
微信小程式的自動化測試
微信小程式自動化測試最佳實踐(附 Python 源碼)
- 微信小程式自動化測試的關鍵步驟
- 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,常見錯誤等),請點選下方連結入群擷取。