類似的,nightmare也是一個模擬還原瀏覽器上業務操作的強大工具,而且更易于使用。同時可以使用chrome的插件daydreem自動錄制生成用戶行為操作的事件序列,更加方便我們進行實際的測試。
yield Nightmare() .goto('http://yahoo.com') .type('input[title="Search"]', 'github nightmare') .click('.searchsubmit'); |
Nightmare也支持異步操作,并支持多種斷言庫,這里結合chai.js就可以這樣來使用。
var Nightmare = require('nightmare'); var expect = require('chai').expect; // jshint ignore:line describe('test yahoo search results', function() { it('should find the nightmare github link first', function(done) { var nightmare = Nightmare() nightmare .goto('http://yahoo.com') .type('form[action*="/search"] [name=p]', 'github nightmare') .click('form[action*="/search"] [type=submit]') .wait('#main') .evaluate(function () { return document.querySelector('#main .searchCenterMiddle li a').href }) .end() .then(function(link) { expect(link).to.equal('https://github.com/segmentio/nightmare'); done(); }) }); }); |
原文轉自:http://jixianqianduan.com/frontend-javascript/2016/11/22/front-end-auto-test.html