//

Testing JavaScript with JS Test Driver

8.9.2009 | 3 minutes of reading time

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:

1var calcValue = " ";
2 
3function clear()
4{
5    calcValue = " ";
6}
7 
8function updatePrompt()
9{
10    document.calculator.input.value=calcValue;
11}
12 
13function updateValue(value)
14{
15  calcValue += value;
16}
17 
18function executeClick(value)
19{
20    updateValue(value);
21    updatePrompt();
22}
23 
24function getResult()
25{
26    return eval(calcValue);
27}
28

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.

1server: http://localhost:4224
2 
3load:
4  - src/*.js
5  - src-test/*.js
6

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:

1CalculatorTest = TestCase("CalculatorTest");
2 
3CalculatorTest.prototype.testAddition = function() {
4    clear();
5    updateValue("10");
6    updateValue("+");
7    updateValue("32");
8    assertEquals("Addition Failed", "42", getResult());
9};
10 
11CalculatorTest.prototype.testMinus = function() {
12    clear();
13    updateValue("50");
14    updateValue("-");
15    updateValue("8");
16    assertEquals("Minus Failed", "42", getResult());
17};
18 
19CalculatorTest.prototype.testDivide = function() {
20    clear();
21    updateValue("1764");
22    updateValue("/");
23    updateValue("42");
24    assertEquals("Divide Failed", "42", getResult());
25};
26 
27CalculatorTest.prototype.testMultiply = function() {
28    clear();
29    updateValue("1");
30    updateValue("*");
31    updateValue("42");
32    assertEquals("Multiply Failed", "42", getResult());
33};
34

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 1.9.1.2: 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.

share post

Likes

0

//

More articles in this subject area\n

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen

Wir helfen Deinem Unternehmen

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.