javascript - How to assert the page after press a button which does a slow ajax call, with zombiejs? -
i'm using zombiejs test html page, has button, when clicks on it, issue ajax call, , update page after several seconds.
/static/ajax-button.html
<html> <head> <script src="./js/jquery-1.11.2.js"></script> <script> function fetchresponse() { $.get('/delay/8', function(data) { $('#response').text(data); }); } </script> </head> <body> <button id="mybutton" onclick="fetchresponse()">click me fetch response</button> <div id="response">please wait while ...</div> </body> </html>
app/app.js
using expressjs:
app.get('/delay/:seconds', function(req, res) { const seconds = req.param("seconds") || 1; settimeout(function() { res.send('delayed hello world in ' + seconds + 's!') }, seconds * 1000); });
test/press-button-spec.js
const browser = require('zombie'); const moment = require('moment'); const expect = require('expectations'); // we're going make requests http://example.com/??? // routed our test server localhost:3000 browser.localhost('example.com', 3000); describe('browser.pressbutton', function() { const browser = new browser(); this.timeout(10000); before(function(done) { console.log("######## browser.visit"); browser.visit('/static/ajax-button.html', done); }); before(function(done) { console.log("######## browser.pressbutton"); browser.pressbutton('#mybutton', done); }); it('should find "delayed hello world!" in page after while', function(done) { console.log("######## testing"); console.log(browser.html()); browser.assert.status(200); browser.assert.text('#response', "delayed hello world in 8s!"); }); });
but when use mocha run as:
mocha test/press-button-spec.js
it reports:
➜ zombiejs-test git:(master) ✗ mocha test/press-button-spec.js browser.pressbutton ######## browser.visit ######## browser.pressbutton 1) "before all" hook 0 passing (5s) 1 failing 1) browser.pressbutton "before all" hook: timeout: did not load resources on page
seems browser.pressbutton('#mybutton', done);
timeout, because default wait time zombiejs 5000ms
, ajax call need 8s
finish.
how fix it?
you can find code https://github.com/freewind/zombiejs-test
after cloned it, run:
node app/app.js mocha test/press-button-spec.js
update:
i can add browser.waitduration = '10s'
set global wait duration, make test pass, i'm not sure if it's best way.
we can call browser.pressbutton
without passing callback, , check them in browser.wait
:
it('should find "delayed hello world!" in page after while', function(done) { browser.pressbutton('#mybutton'); browser.wait(10000).then(function() { browser.assert.status(200); browser.assert.text('#response', "delayed hello world in 8s!"); }).then(done, done) });
https://github.com/freewind/zombiejs-test/blob/master/test/wait-spec.js#l28
Comments
Post a Comment