Testing JavaScript with JS Test Driver

No Comments

Some days ago I checked the Google Testing Blog for some interesting new articles when I saw the article about Super Fast JS Testing. Well, basically I have to say “again” as I had read it already some time back, but had no time to really dig into it. But this week luckily I had and so I wanted to give it a try. I would like to emphasize that I am not really an expert in JavaScript, but the testing side of this really got me.

Thus I first downloaded the JsTestDriver-1.0b.jar from the project homepage. And of course we need some small JavaScript Application that can be used for testing. My choice was the calculator that can be found from here. But I had to extract the JavaScript to some external file Calculator.js and some refactoring was required. The result can be seen in the following:

var calcValue = " ";
function clear()
	calcValue = " ";
function updatePrompt()
function updateValue(value)
  calcValue += value;
function executeClick(value)
function getResult()
	return eval(calcValue);

The JS Test Driver has some similarity to Selenium. It also uses a server that needs to be started first and that is then using some real browsers to perform the tests. Starting the server is very easy and can be performed with the following command given in the directory where the jsTestDriver-JAR is located:

java -jar JsTestDriver-1.0b.jar --port 4224

Connecting a browser to the server is solved very nicely. Simply “surf” to the corresponding server-URL, in this case http://localhost:4224. There you find a link with which the browser can be connected to the server. Really all established browsers are supported and they can run on any machine in the net as long as they are able to connect to the server.

For the test as such only two more files are needed. One YAML-file that defines the server to be used and where to find the required JavaScript files. It must be named jsTestDriver.conf.

The YAML-file could lead to some quite special problems. The line
– *.js
leads to the following not very descriptive error message :
error while scanning an alias expected alphabetic or numeric character, but found something else… org.jvyaml.ScannerException: ScannerException while scanning an alias we had this expected alphabetic or numeric character, but found something else…
In addition the following line must be present:
– src-test/*.js
I guess this is due to the “test” in the directory name. If this is missing simply no tests are executed. I started my tests with the following individual line
– *.js
and was thus facing both problems initially.

server: http://localhost:4224
  - src/*.js
  - src-test/*.js

Then we need the JavaScript testing class. This one is following the JUnit standards, thus testing-methods must be started with “test”. The following class checks that the basic arithmetic operations are working in our Calculator-application:

CalculatorTest = TestCase("CalculatorTest");
CalculatorTest.prototype.testAddition = function() {
	assertEquals("Addition Failed", "42", getResult());
CalculatorTest.prototype.testMinus = function() {
	assertEquals("Minus Failed", "42", getResult());
CalculatorTest.prototype.testDivide = function() {
	assertEquals("Divide Failed", "42", getResult());
CalculatorTest.prototype.testMultiply = function() {
	assertEquals("Multiply Failed", "42", getResult());

Now the tests can be easily started from the command line by changing to the directory where the jsTestDriver.conf-file is located. Tests are then executed as follows:

C:\...\Calculator>java -jar JsTestDriver-1.0b.jar --tests all

Total 4 tests (Passed: 4; Fails: 0; Errors: 0) (0,00 ms)
  Firefox Run 4 tests (Passed: 4; Fails: 0; Errors 0) (0,00 ms)

Not bad so far, but it gets even better. Because the Eclipse-PlugIn described here is really working very smoothly and thus those tests can be executed directly from Eclipse.


Developers using a lot of JavaScript that contain complex logic or functions should have a closer look at JS test Driver in my opinion.

Long-term experience in agile software projects using
Java enterprise technologies. Interested in test automation tools and concepts.


Your email address will not be published. Required fields are marked *