The 5,000 Foot View
There are three components involved when using JsTestDriver:
- The JsTestDriver server
- The Web Browser
- The JsTestDriver runner
If you have Homebrew on your machine, you can run the command:
Setting up your Environment
Installing via HomeBrew will install the JAR file in the directory:
/usr/local/Cellar/js-test-driver/1.3.5/libexec/. You need to set the environment variable
JSTESTDRIVER_HOME in your
Before you run JsTestDriver, you need to specify a configuration file. The default filename that JsTestDriver recognizes is
jstestdriver.conf. Here is what my configuration file looks like:
1 2 3 4 5 6 7
This is a file written in YAML syntax. I specified a random port 9876 for our server. By default, JsTestDriver will listen on port 4224. Because I specified the
* character, JsTestDriver will load all the source files from the
src folder and test files from the
test directory with a
.js extension. Be sure to only use spaces in your configuration file; don’t use tabs.
Running the JsTestDriver Server
To run JsTestDriver, you need to type this command in the Terminal:
We pass the JAR file to the
-jar argument. In our case, the JsTestDriver file is located inside the directory we specified earlier. If you downloaded the JAR file elsewhere, you’ll have to write out the correct path here.
Next, you have to specify the same port you specified in your configuration file.
Selecting a Browser To Capture
In the web browser or browsers on which you want to run your tests, you need to type
http://localhost:9876 or whatever port number you decided to use. A page will load with two links as shown below. Choose the link to “Capture This Browser”.
If all goes well, you should see a green band informing you of the state of the JsTestDriver server.
By the way, if you quit running the JsTestDriver server, the browser will inform you of its connection to the server. It will retry connecting to the server…
… before it finally acknowledges that it is dead:
One more thing: if you don’t need to run your tests in the browser for whatever reason, you can skip this step of capturing the browser.
A Simple Test File
We are going to reuse the test spec shown on the JsTestDriver project page. We will call this file
1 2 3 4 5 6 7 8
We also could have written it this way:
1 2 3 4 5 6 7 8
Running the JsTestDriver Runner
At this point, we are ready to run the JsTestDriver runner and see the feedback we get. Our test should fail, and JsTestDriver confirms this when we run the command
java -jar $JSTESTDRIVER_HOME/JsTestDriver-1.3.5.jar --tests all:
1 2 3 4 5 6 7 8
Then we write the code to make this test pass:
1 2 3 4 5 6 7 8 9
Now when you run the java command to run JsTestDriver, you will see your test passing:
1 2 3 4 5
Jstdutil, a Ruby Wrapper
Tired of running the same Java command over and over? Christian Johansen wrote a Ruby wrapper to automate that task. You can find it on Github under the gem name jstdutil.
Let’s include the gem in our Gemfile:
1 2 3 4 5
bundle install to have bundler grab the gem.
Remember to repeat the same process as when we ran the Java command by hand:
- Run the server in the background
- Capture the browser
- Run the runner
To run the server in the background, we issue the command in the Terminal:
Assuming you’ve captured the browser, you’ll then want to run the runner:
The neat thing about using jstdutil is the colored output on the Terminal. The gem also comes with an autotesting feature. If you want to run the JsTestDriver runner every time the files change, use
I hope you now have a better understanding of how to use JsTestDriver. If you found it useful, share it on Twitter or your social networking platform of choice. I’d also love to hear your feedback. Write it below in the comments.
I included a Pitfalls section below to address some common problems when trying to use JsTestDriver.
Make sure you have written valid YAML. You will get parsing errors if you use tabs in your YAML file. The error below occurred because I used tabs to indent before the
- character when specifying the directories to load.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Also make sure that you use the same port in the
--port argument when you start the JsTestDriver server as the one you specified in your
.conf file, otherwise you’ll get an error about the server not being able to start. The error below occurred because I specified port 6666 in my configuration file but ran JsTestDriver with a different port.
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 26 27 28 29 30 31
- Getting Started, https://code.google.com/p/js-test-driver/wiki/GettingStarted
- Need Help with Error Message: java.lang.RuntimeException: Could not start browser CommandLineBrowserRunner https://groups.google.com/group/js-test-driver/browse_frm/month/2012-05?fwc=1&pli=1