因?yàn)橐业囊粋€(gè)前端的 Browser Testing 解決方案雏逾,看下來 PhantomJS 是比較合適的栖博,但是前端做 Browser Testing 確實(shí)比較麻煩,登錄態(tài)仇让、Seed、數(shù)據(jù)偽造等等
PhantomJS 的安裝:
npm -g install phantomjs-prebuilt
實(shí)現(xiàn)一個(gè)拖拽效果的測試用例秋柄,代碼如下(省略了為實(shí)現(xiàn)登錄蠢正,異常復(fù)雜的代碼):
var page = require('webpage').create();
page.viewportSize = { width: 1440, height: 1080 };
page.open('http://www.darwin.com/teach/course/1', function () {
setTimeout(function () {
page.sendEvent("mousedown", 180, 180);
page.sendEvent("mousemove", 180, 240);
page.sendEvent("mouseup", 180, 240);
page.render('finish.png');
phantom.exit();
}, 2000);
});
PhantomJS 支持的函數(shù):
page.childFramesCount
page.childFramesName
page.close
page.currentFrameName
page.deleteLater
page.destroyed
page.evaluate
page.initialized
page.injectJs
page.javaScriptAlertSent
page.javaScriptConsoleMessageSent
page.loadFinished
page.loadStarted
page.openUrl
page.release
page.render
page.resourceError
page.resourceReceived
page.resourceRequested
page.uploadFile
page.sendEvent
page.setContent
page.switchToChildFrame
page.switchToMainFrame
page.switchToParentFrame
page.addCookie
page.deleteCookie
page.clearCookies
PhantomJS 認(rèn)為自己并不是一個(gè)測試框架,只是為其他的測試框架提供支持而已笨触,在官網(wǎng)有羅列其支持的測試框架:
PhantomJS itself is not a test framework, it is only used to launch the tests via a suitable test runner.
The following table summarizes the list of various test frameworks and the corresponding test runners. If the framework does not need an external/third-party runner, it is marked as “built-in”.
目前 PhantomJS 還不能很好的支持 ES6雹舀,但是 2.5 Beta 版本是可以支持的,目前團(tuán)隊(duì)正在為這個(gè)目標(biāo)努力:
https://github.com/Medium/phantomjs/issues/668
下載 2.5 Beta 以后還需要一些額外的操作虚吟,后續(xù)版本應(yīng)該會(huì) fix 這些問題:
brew install webp
cd /usr/local/opt/webp/lib
cp libwebp.{7,6}.dylib
勉強(qiáng)可以用签财,但是因?yàn)?PhantomJS 的定位非常底層,真的要一個(gè)一個(gè)寫測試用例邦鲫,還需要自己來設(shè)計(jì)代碼結(jié)構(gòu)神汹,實(shí)際上會(huì)異常的復(fù)雜,看來 Karma 這樣的 Runner 真的是必不可少的