Tuesday, September 27, 2011

Automated Javascript Test Framework - JSTestDriver-Remote JS console - maven


There are number of factors that goes into deciding the Framework that would help automate the tests involved in testing the product again newly arriving build . As it is very important to make sure that we have all the test cases in place which can be executed against every new build and make sure that the product is stable.
1 ) Asynchronous Test
                The test framework should allow rather support Asynchronous tests .
2) Run against build tools.
                This feature will make sure that before the final release build is prepared all the test are executed and the result are in hand .
3) Browser support
                This feature will remove the manual intervention for starting the browser for executing the test (Applicable to only those projects which needs browsers to test the product ).

We now have a lot of test of test frameworks available which can perform most of the above task involved in Unit Testing the product.
1)      JSTestFDriver
2)      YUI
3)      JSUnit
And many more. This article will focus on JSTestDriver

How JSTestDriver Works?
It starts of by launching a server (Java process ) which holds the responsibility of loading the test runner code into the browser(acts as slave) . Once this happens the browser can be controlled by the server which sends across command to the browser to load the test resources . JSTestDriver has a jsTestDriver.conf' file (which can be easily configured)  which let the developer configure the files that the browser needs to load to run the tests.
Configuration File

server: http://localhost:9876

load:
  - src/*.js
  - src-test/*.js

The very first line in this conf file is URL that the browser needs to use to contact the server
Load directive tells the browser on the resources that needs to be loaded , one need to note that the resources loaded will be in the same sequence in which they are specified in the conf file.
Out here by resources it means to include the files that has the test written which are being implemented as a part of automation of tests(Unit tests).
9876 is the port on which JSTest server runs.


Configuring JSTestDriver in Maven

1)     pom.xml
Pom file needs to be configured with the jstestdriver details
  <dependencies>
        <dependency>
            <groupId>com.google.jstestdriver</groupId>
            <artifactId>maven-jstestdriver-plugin</artifactId>
            <version>1.3.2.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

<plugins>
<plugin>
          <groupId>com.google.jstestdriver</groupId>
          <artifactId>maven-jstestdriver-plugin</artifactId>
          <version>1.3.2.1</version>
          <executions>
                <execution>
                                <id>run-tests</id>
                <phase>integration-test</phase>
                                <goals>
                <goal>test</goal>
                </goals>
              <configuration>
                                                <debug>true</debug>
                <config>${project.basedir}/test/conf/jsTestDriver.conf</config>
                <browser>${browser.command}</browser>
                <port>9876</port>
                <basePath>${project.basedir}/src</basePath>
              </configuration>
            </execution>
          </executions>
 </plugin>
      </plugins>
2)     ./src/test/conf/jsTestDriver.conf
Define the server URL , resources that needs to be loaded by JSTestDriver
3)     Maven settings.xml
<profile>
           <id>jstests</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <browser.command>c:/Program Files/Mozilla Firefox/firefox.exe</browser.command>
            </properties>
</profile>
            This configuration defines the browsers that will be loaded while the JSTestDriver is started.
There is a very nice WIKI page created on JSTestDriver which would be worth taking a look at Getting Started.