小程序自动化测试--测试3
使用 Appium 進行微信小程序自動化測試
使用 node(wd)編寫 Appium 測試用例 介紹了使用 wd 編寫簡單的 Appium 測試用例
本文主要介紹使用 Appium 進行微信小程序自動化測試,涉及使用 wd 編寫復雜 Appium 測試用例以及微信 webview 自動化測試。
測試項目搭建及簡單前期準備參考使用 node(wd)編寫 Appium 測試用例
其實微信小程序就是webview,所以可以使用webview測試方式進行小程序的測試
打開微信調試功能
用微信打開debugx5.qq.com頁面,或者直接打開二維碼:
勾選【打開TBS內核Inspector調試功能】,如下:
設置好之后,就可以在chrome瀏覽器中打開chrome://inspect/頁面查看當前微信中打開的H5頁面了
在電腦中安裝chromedriver
安裝Appium時,會自動安裝chromedriver,但是在使用默認安裝的chromedriver時,對于一些老設備會存在一些問題,報類似下面這樣的錯誤:
An unknown server-side error occurred while processing the command. Original error: unknown error: Chrome version must be >= 55.0.2883.0 復制代碼解決辦法可以參考官方文檔
我使用的測試機chrome版本是59.0.3071.0,所以直接下載 v2.32 版本的 chromedriver 到 /usr/local/ 目錄下,在啟動Appium時,通過 --chromedriver-executable 指定使用此chromedriver,如下:
$ appium --chromedriver-executable /usr/local/chromedriver復制代碼編寫測試用例
以測試【美團酒店+】小程序為例,測試代碼如下:( setup.js、logging.js 參考使用 node(wd)編寫 Appium 測試用例)
weapp.js
require("../helpers/setup");const wd = require("wd");const serverConfig = {host: 'localhost',port: 4723 };describe("sample test", function () {this.timeout(300000);let driver;let allPassed = true;before(function () {driver = wd.promiseChainRemote(serverConfig);require("../helpers/logging").configure(driver);let desired = {platformName: 'Android',deviceName: 'U2TDU15904014013',appPackage: 'com.tencent.mm',appActivity: '.ui.LauncherUI',fullReset: false,fastReset: false,noReset: true,chromeOptions: {androidProcess: 'com.tencent.mm:appbrand0',}};return driver.init(desired).setImplicitWaitTimeout(8000);});after(function () {return driver.quit();});afterEach(function () {allPassed = allPassed && this.currentTest.state === 'passed';});it("enter 小程序", function () {return driver.elementByXPath("//*[@text='發現']").click().elementByXPath("//*[contains(@text, '朋友圈')]").then(function () {let action = new wd.TouchAction(driver);action.press({x: 20, y: 0}).moveTo({x: 20, y: 20}).wait(200).release().perform();return driver.performTouchAction(action);}).elementByXPath("//*[@text='小程序']").click().elementByXPath("//*[contains(@text, '美團酒店+')]").click().elementByXPath("//*[contains(@text, '美團酒店')]").should.eventually.exist.context('WEBVIEW_com.tencent.mm:appbrand0').sleep(5000).elementsByCssSelector('.cell', function (err, els) {els[0].click();}).sleep(5000);}); });復制代碼執行測試用例
在package.json中添加以下腳本:
{..."scripts": {"weapp": "mocha ./test/weapp.js"}... }復制代碼執行測試用例:
$ appium --chromedriver-executable /usr/local/chromedriver # 啟動Appium服務且指定chromedriver $ npm run weapp # 運行測試用例復制代碼執行結果如下:
以上就是使用 Appium 進行微信小程序自動化測試~
完整代碼:github.com/HuJiaoHJ/ap…
經過一系列的實踐之后,發現使用 Appium 進行微信小程序自動化測試依舊還存在以下幾個問題:
1、微信在6.5.23版本之后在使用 driver.context(WEBVIEW_com.tencent.mm:appbrand0) 時,獲取的 servicewechat.com/{appid}/{ve… 中body為空,而頁面內容都包含在 servicewechat.com/preload/pag… ,而在切換時,隨機獲取兩個html中一個,所以會存在獲取到空內容的情況,導致測試流程走不通(微信社區問題:developers.weixin.qq.com/blogdetail?… )
2、在小程序內部進行頁面跳轉之后,webview之間的相互切換暫時是存在問題的,原因還是上面提到的兩個html的原因,暫時沒有找到解決辦法。(社區問題:testerhome.com/topics/7769 )
所以,大概在17年12月更新的版本之后,還是不要再考慮使用 Appium 進行微信小程序的自動化測試了,網上很多教程都是在17年12月之前的,所以能走通整個流程,但是現在是存在問題的
歡迎找到解決辦法的小伙伴分享一下~~~
寫在最后:從調研到得出最終結論,花了差不多一周的時間,中間還經歷了一個春節。雖然最后此方案沒能用在實際開發中,有點遺憾,不過整個過程還是收獲不少~~~
原文地址:https://github.com/HuJiaoHJ/blog/issues/5
轉載于:https://juejin.im/post/5b2f263151882574c673b101
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的小程序自动化测试--测试3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Java connector消费AB
- 下一篇: proguard的简单配置说明