Testing interaction with your site in an automated fashion can be hard, but fortunately there are tools out there to make it easier. I didn’t realize they existed until recently so I have been playing with them. One I am really liking is Zombie.js. To be honest I haven’t tested anything too difficult, but the ajaxy/dynamic content stuff done through JavaScript I have done, and I like it.
How about we do something very basic, that shows what and how to test.
Test HTML Page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| <!DOCTYPE html> < html > < head > < title >Hello World</ title > < script type = "text/javascript" > $(document).ready(function(){ $('footer').click(function(){ $("#content").append("< div class = 'my-data' >hello world</ div >"); }); }); </ script > </ head > < body > < div id = "content" > < footer > < Copyright >Now Buddy Lindsey</ Copyright > </ footer > </ div > </ body > </ html > |
What You Need to Start Testing
Zombie.js allows you to interact with a page, but by itself does not allow you to do assertions. For that we need to use a test runner of some sort. The one I have been using lately has been Mocha since this is node.js we can just install mocha and zombie with npm.Dependencies
1
2
| npm install -g mocha npm install zombie |
Testing Our Div is There
The first test we want to do is to make sure our content div is there for us to use. Mocha uses a describe block to bundle up like tests, but it uses it functions for the actual tests. So describe is there for organization. Here is our first test which checks for our div.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| var assert = require( 'assert' ); var Browser = require( 'zombie' ); var browser = new Browser(); describe( "Index Page" , function (){ it( "contains #content div" , function (done){ assert.ok(browser.success); assert.ok(browser.query( "#content" )) done(); }); }); }); |
1
2
3
4
| var assert = require( 'assert' ); var Browser = require( 'zombie' ); var browser = new Browser(); |
1
2
| describe( "Index Page" , function (){ }); |
1
2
| it( "contains #content div" , function (done){ }); |
1
2
3
4
5
| assert.ok(browser.success); assert.ok(browser.query( "#content" )) done(); }); |
- We are opening the page and rendering everything out
- The entire response is stored in the browser
- Once the visit is finished we run our assertions
- The success is making sure that we have a 200 response
- The query function selects the dom elements and returns it back
- Finally we end out with done(), and all is well
1
|
Testing Our JavaScript
This next code snippet is very similar to what we have covered so we are only going to cover the new stuff. Here is the entire code snippet.
1
2
3
4
5
6
7
8
9
10
| it( "add div to page" , function (){ assert.ok(browser.success) var footer = browser.query( 'footer' ); browser.fire( "click" , footer, function (){ assert.ok(browser.query( ".my-data" )); done(); }); }); }); |
1
| var footer = browser.query( 'footer' ); |
1
2
3
4
| browser.fire( "click" , footer, function (){ assert.ok(browser.query( ".my-data" )); done(); }); |
- First parameter is the event to fire
- The second is the element to which you want to fire event on
- Finally the callback
Finally, we just do an assertion of some sort to make sure things worked. In this case we added a div to the page so we need to test it exists.
Final Step
Now that we have our code we are ready to run our tests. Make sure your index.html is hosted somewhere for our headless tests to get to. Make sure you have mocha and zombie installed. Then take all the code and put it in a javascript file in your test folder and run the following command:
1
| mocha -R spec |
1
2
3
4
5
6
| Index Page ✓ contains #content div (307ms) ✓ add div to page (324ms) ✔ 2 tests complete (370 ms) |
All the Test Code
Here is all the test code in its glory for you to look at.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| var assert = require( 'assert' ); var Browser = require( 'zombie' ); var browser = new Browser(); describe( "Index Page" , function (){ it( "contains #content div" , function ( done ){ assert.ok(browser.success); assert.ok(browser.query( "#content" )) done (); }); }); it( "add div to page" , function (){ assert.ok(browser.success) var footer = browser.query( 'footer' ); browser.fire( "click" , footer, function (){ assert.ok(browser.query( ".my-data" )); done (); }); }); }); }); |
Conclusion
This was a basic example. Hopefully you have gained a starting point at doing headless browser tests. This is really good for doing front end acceptance testing for your UI. UI functionality testing is usually hard to do, but this really helps us get a step closer to automating it so we know, at least, when our features break for our users.Sign up here with your email
ConversionConversion EmoticonEmoticon