本文爲霍格沃茲測試學院測試大咖公開課《微信小程序自動化測試》圖文整理精華版。
隨着微信小程序的功能和生態日益完善,不少公司的產品業務形態逐漸從 App 延升到微信小程序、微信公衆號等。小程序項目頁面愈來愈多,業務邏輯也愈來愈複雜,全手工測試已沒法知足快速增加的業務需求。css
然而,因爲小程序自己的一些特性,致使業界目前缺少成熟完善的解決方案,總會出現各類問題(包括騰訊微信官方提供的自動化工具)。如何作好小程序的自動化測試就成爲測試同窗當下廣泛面臨的一個痛點難題。python
本節課就主要分享下微信小程序自動化測試的一些最佳實踐心得,包括微信小程序的基本測試技術和操做方法,以及如何利用 Appium 的 WebView 測試技術 + adb proxy 完成微信小程序的自動化測試(多是目前最實用的小程序自動化測試技術),並附上 Python 版源碼。android
平臺差別:儘管各運行環境是十分類似的,可是仍是有些許區別:web
JavaScript 語法和 API 支持不一致:語法上開發者能夠經過開啓 ES6 轉 ES5 的功能來規避(詳情);此外,小程序基礎庫內置了必要的Polyfill,來彌補API的差別。chrome
WXSS 渲染表現不一致:儘管能夠經過開啓樣式補全來規避大部分的問題,仍是建議開發者須要在 iOS 和 Android 上分別檢查小程序的真實表現。json
chrome://inspect/#devices
爲何仍然有不少人搞不定?小程序
解決方案:如何 fix it?微信小程序
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()
以上,更多內容(ChromeDriver 的資料與 WebView 自動化關鍵代碼,Appium 配置,mapping.json,常見錯誤等),請點擊下方連接入羣獲取。瀏覽器