Better way to integrate maven/qunit/phantomjs?

Matt picture Matt · Dec 15, 2011 · Viewed 11k times · Source

I have been investigating the best way to do JS unit testing in our maven CI environment. What I currently have cobbled together is the following in my maven project:

  • qunit resources (JS/CSS files)
  • qunit test html files (one for each file under test) with html fixture if required
  • index html file which references the test html files as an ordered list of hyperlinks
  • PhantomJS runner file, which:
    • opens the index html file and parses out list of test files
    • opens each test file
    • takes a screenshot of the qunit test results for each file
    • If there are any failures, exit with a status of "1"
    • If there are no failures, exit with a status of "0"
  • shell file which will exit with "0" if phantomjs isn't installed, will call the phantomjs tests if it is installed
  • changes to pom.xml to run phantomjs tests during test phase of build:

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <id>PhantomJS Unit Testing</id>
                    <phase>test</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>${project.basedir}/src/main/webapp/unittest/phantomcheck</executable>
                <arguments>
                    <argument>${project.basedir}/src/main/webapp/unittest/qunit-runner.js</argument>
                    <argument>${project.basedir}/src/main/webapp/unittest/tests/index.html</argument>
                    <argument>${project.build.directory}/surefire-reports</argument>
                </arguments>
            </configuration>
        </plugin>
    </plugins>
    

So, this works nicely. It runs the qunit tests during builds on our dev and build machines (as long as PhantomJS is installed). The tests run in a headless browser environment with no restrictions on the qunit tests. Other maven/qunit integrations I've seen fall short due to running the tests in Rhino, or other JS environments which place restrictions on the type of tests we can write. Plus phantomjs gives us the ability to have the screenshots of the test runs, which are helpful in troubleshooting any failures.

The drawback to my approach is that a PhantomJS installation is required on the build/dev machine. I don't know how to bundle phantomJS into a dependency such that developers don't need to worry about installing PhantomJS. Can anyone give me a push in this direction? How do I get started?

Answer

Kyle picture Kyle · May 30, 2014

The phantomjs-maven-plugin provides an install goal for installing phantomjs so you don't need it pre-installed. After it installs phantomjs it sets a property with the path to the executable that other plugins can then use. It also has an exec goal for executing phantomjs scripts. Full disclosure: I wrote the plugin.