關(guān)于函數(shù)測(cè)試,比如有一些固定的輸入輸出,可以使用mocha來(lái)進(jìn)行測(cè)試
關(guān)于頁(yè)面功能的測(cè)試,推薦nightmare。
var Nightmare = require('nightmare');var nightmare = Nightmare({ show: true }); nightmare .goto('https://www.taobao.com/') //待測(cè)試鏈接 .type('#q', '電視機(jī)') //輸入框選中,輸入值 .click('form[action*="/search"] [type=submit]')//form表單提交 .wait(3000) .exists('#spulist-grid') .evaluate(function () { return document.querySelector('#spulist-grid .grid-item .info-cont') //獲取需返回的值 .textContent.trim(); }) .end() .then(function (result) { //即為return中的數(shù)據(jù) console.log(result); }) .catch(function (error) { //錯(cuò)誤捕捉 console.error('Search failed:', error); });
可以結(jié)合mocha使用。
var Nightmare = require('nightmare');var expect = require('chai').expect;var fork = require('child_process').fork; describe('test index.html', function() { var child; before(function (done) { //鉤子函數(shù),測(cè)試之前調(diào)用 child = fork('./server.js'); child.on('message', function (msg) { if (msg === 'listening') { done(); } }); }); after(function () { //鉤子函數(shù),測(cè)試之后調(diào)用 child.kill(); }); it('點(diǎn)擊后標(biāo)題改變', function (done) { var nightmare = Nightmare({ show: true }); nightmare .goto('http://127.0.0.1:8080/index.html') .click('h1') .wait(1000) .evaluate(function () { return document.querySelector('h1').style.color; }) .end() .then(function(color) { console.log(color) expect(color).to.equal('red'); done(); }) }); });
不知道為什么,總感覺(jué)前端的自動(dòng)化測(cè)試從某種程度上來(lái)講,還是比較吃力的。求大神普及……
相信看了本文案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注Gxl網(wǎng)其它相關(guān)文章!
推薦閱讀:
javascript中call詳解
javascript中call與apply以及bind有哪些不同
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com