📜 ⬆️ ⬇️

Running Siesta tests from the console using PhantomJS

Here it will be described how to run Siesta tests from the console without using the paid (standard) version of the product (which costs $ 499).

Problem

The fact is that the free (light) version of the Siesta tool allows you to run tests only from a browser. And if you need to run tests for CI from the console, you will have to look at the standard version, which has a lot of goodies, including running from the console. The tool itself uses the well-known free engine PhantomJS to run its tests.

Decision

After reviewing the PhantomJS, Siesta documentation and ready-made examples for running the Jasmine and QUnit tests, I wrote some code to save our money with you.

Toolkit Overview

The Siesta tool is written on the ExtJS framework. This powerful tool is designed for testing javascript applications written using various popular frameworks such as ExtJS, jQuery, Dojo, etc. Testing can be carried out both modular and functional, up to a complete imitation of user actions in the browser.
')
The PhantomJS tool is used to launch web pages from the console and is based on the WebKit engine. It is widely covered in the Russian segment of the Internet, including on Habré. In contrast to the tool Siesta, on which information "the cat wept."

Actually, therefore I am writing this post.

The necessary conditions

  1. You already know how to run Siesta tests from a browser (if not, go here ). For example, we will run our tests at localhost/tests/index.html
  2. You have PhantomJS installed (if not, then here )


Required code

I wrote runner-siesta.js runner -script for PhantomJS to run the Siesta tests.

Run-siesta.js code
 var start = new Date().getTime(), system = require('system'), page = require('webpage').create(); console.log('\nStart tests...\n'); /** * . */ var globalLog = (function() { var store = []; return { /** *    . * * @param msg */ add: function(msg) { store.push(msg); }, /** *    . */ console: function() { var log = ''; for (var i= 0; i < store.length; i++) { log += store[i] + '\n'; } console.log(log); } }; }()); if (system.args.length !== 2) { globalLog.add('Usage: phantomjs run-siesta.js URL'); myExit(1); } /** *         . * * @param exitCode   (0 - ) */ function myExit(exitCode) { globalLog.add('Total time: ' + (new Date().getTime() - start) + ' ms'); globalLog.add('Exit code: ' + exitCode); globalLog.console(); phantom.exit(exitCode); } /** *     . * @param msg */ page.onConsoleMessage = function(msg) { if (msg.match(/END_TESTS/)) { var exitCode = page.evaluate( function() { var totalPass = document.getElementsByClassName('total-pass')[0].innerText; var totalFail = document.getElementsByClassName('total-fail')[0].innerText; if (totalFail !== '0') { console.log('\nFailed!'); } else { console.log('\nCompleted!'); } console.log('\nTotal pass: ' + totalPass); console.log('Total fail: ' + totalFail); return totalFail === '0' ? 0 : 1; } ); myExit(exitCode); } else if (!msg.match(/\[object Object\]/)) { console.log(msg); } }; /** *  . * @param {String} URL  */ page.open(system.args[1], function(status) { if (status !== "success") { globalLog.add("Unable to access network"); myExit(1); } } ); 


This code uses the PhantomJS API to run a page with tests and displays logs to the console, which are generated on the index.html page.

As you already know, index.html includes the index.js script, which describes the test run configuration. A simple example of index.js is provided on the official Siesta website.

In order for tests to run automatically, you need to add an option to the configuration:

autoRun: true

Thus, when PhantomJS accesses the page, tests will run automatically. If you need to launch tests through the browser, this is done via the page settings menu by selecting the “Auto launch” option.

In order for PhantomJS to receive information about each passed test, it is necessary to use the Siesta event - testfinalize , which is thrown every time at the end of the next test. A handler for this event is added to the configuration and displays information about the passed test in the page console (which PhantomJS, in turn, is picked up by using page.onsoleMessage ).

The completion of all tests is tracked using the testsuiteend event. The handler of this event outputs to the console a special code that PhantomJS knows. Due to this, he can complete his work and display the summary information in the console.

Handler code
  listeners: { testfinalize: function(event, test) { var fail = test.$failCount, pass = test.$passCount; var log = (fail ? '~~~~~~~~\n FAILED ' : '[PASSED] ') + test.url + ' [pass: ' + pass + ', fail: ' + fail + ']' + (fail ? '\n~~~~~~~~' : ''); console.log(log); }, testsuiteend: function(event, harness) { console.log('END_TESTS'); } } 



If you put it all together using a simple example from the office. Siesta, then index.js will look like this

Code index.js
 var Harness = Siesta.Harness.Browser.ExtJS; Harness.configure({ title : 'Awesome Test Suite', //   autoRun : true, preload : [ // version of ExtJS used by your application '../ext-4.1.1/resources/css/ext-all.css', '../resources/yourproject-css-all.css', // version of ExtJS used by your application '../ext-4.1.1/ext-all-debug.js', '../yourproject-all.js' ], listeners: { //        testfinalize: function(event, test) { var fail = test.$failCount, pass = test.$passCount; var log = (fail ? '~~~~~~~~\n FAILED ' : '[PASSED] ') + test.url + ' [pass: ' + pass + ', fail: ' + fail + ']' + (fail ? '\n~~~~~~~~' : ''); console.log(log); }, //         testsuiteend: function(event, harness) { console.log('END_TESTS'); } } }); Harness.start( '010_sanity.t.js', '020_basic.t.js' ); 



When tests are completed, run-siesta.js parses the test page for the total number of tests passed, displays the total elapsed time and returns the corresponding numeric identifier to the console (0 is success, 1 is error), which is important for CI.

An example of how it all looks in action.
screenshot


Todo

It is necessary to provide for the possibility of PhantomJS shutting down on a timeout in case it never receives an END_TESTS termination code.

Total

It took only two small files with simple code to save $ 499.

Profit?

Source: https://habr.com/ru/post/189144/


All Articles