使用 node(wd)編寫 Appium 測試用例 介紹了使用 wd 編寫簡單的 Appium 測試用例html
本文主要介紹使用 Appium 進行微信小程序自動化測試,涉及使用 wd 編寫複雜 Appium 測試用例以及微信 webview 自動化測試。node
測試項目搭建及簡單前期準備參考使用 node(wd)編寫 Appium 測試用例android
其實微信小程序就是webview,因此可使用webview測試方式進行小程序的測試git
用微信打開debugx5.qq.com頁面,或者直接打開二維碼:github
勾選【打開TBS內核Inspector調試功能】,以下:web
設置好以後,就能夠在chrome瀏覽器中打開chrome://inspect/頁面查看當前微信中打開的H5頁面了chrome
安裝Appium時,會自動安裝chromedriver,可是在使用默認安裝的chromedriver時,對於一些老設備會存在一些問題,報相似下面這樣的錯誤:npm
An unknown server-side error occurred while processing the command.
Original error: unknown error: Chrome version must be >= 55.0.2883.0
複製代碼
解決辦法能夠參考官方文檔json
我使用的測試機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 進行微信小程序自動化測試~
一、微信在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?… )
二、在小程序內部進行頁面跳轉以後,webview之間的相互切換暫時是存在問題的,緣由仍是上面提到的兩個html的緣由,暫時沒有找到解決辦法。(社區問題:testerhome.com/topics/7769 )
因此,大概在17年12月更新的版本以後,仍是不要再考慮使用 Appium 進行微信小程序的自動化測試了,網上不少教程都是在17年12月以前的,因此能走通整個流程,可是如今是存在問題的
歡迎找到解決辦法的小夥伴分享一下~~~
寫在最後:從調研到得出最終結論,花了差很少一週的時間,中間還經歷了一個春節。雖然最後此方案沒能用在實際開發中,有點遺憾,不過整個過程仍是收穫很多~~~
原文地址:https://github.com/HuJiaoHJ/blog/issues/5