Today will introduce an automated test suite called nightmare, a PHANTOMJS-based test framework, a set of high-level APIs based on PHANTOMJS for the test application. Its API to Goto, refresh, click, type ... such as simple common e2e test action package, make its semantics clear, concise. Its official in http://www.nightmarejs.org/.
If your project test does not need to be understood by the needs and testers, then the nightmare-based test may be a good choice, your cost of reducing the test code, and the deployment of the test suite. We can choose to integrate as a test suite based on Jasmine-node.
Install Nightmare:
NPM Install Nightmare
Below we compare the comparison with the remote PHANTOMJS:
The original PHANTOMJS code:
Phantom.create (function (ph) { ph.createpage (function (page) { page.open (' http://yahoo.com '), function ( Status) { page.evaluate (function () { var el = document.queryselector (' input[title= "Search"] "); El.value = ' GitHub nightmare '; }, function (result) { page.evaluate (function () { var el = Document.querysele ctor ('. Searchsubmit '); var event = document.createevent (' MouseEvent '); Event.initevent (' Click ', True, false); El.dispatchevent (event); }, function (result) { ph.exit (); }) ;}); });
Nightmare code:
New Nightmare () . Goto (' http://yahoo.com ') . Type (' input[title= "Search"] ', ' GitHub nightmare ') . Click ('. Searchsubmit ') . Run ();
Everything is obvious, don't say much.
Nightmare also supports plug-in methods for extracting common logic for reuse and for improving test code semantics, as in the following example:
/** * Login to a swiftly account. * * @param {string} email * @param {string} password */exports.login = function (email, password) { return function (nigh Tmare) { nightmare . Viewport (+). Goto (' Https://swiftly.com/login ') . Type (' #username ', email). type (' #password ', password). Click ('. Button--primary '). wait (); };};
Using the code is simple:
var swiftly = require (' nightmare-swiftly '); new Nightmare () . Use (Swiftly.login (e-mail, password)) . Use ( Swiftly.task (instructions, uploads, path)) . Run ();
Nightmare PHANTOMJS-based automated test suite