Skip to content

JavaScript testing with Maven

oleh-maikovych edited this page Sep 25, 2014 · 9 revisions

This is a step-by-step instruction for integrating Jasmine testing framework with maven.

Requirements

  • JDK 1.6+
  • Maven 3.0.4+

Step 1: Configure POM file

Augment plgins section of the project's pom file with the following instructions:

		<plugins>
			<plugin>
				<groupId>com.github.searls</groupId>
				<artifactId>jasmine-maven-plugin</artifactId>
				<version>1.3.1.5</version>
				<executions>
					<execution>
						<goals>
							<goal>test</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<webDriverClassName>org.openqa.selenium.phantomjs.PhantomJSDriver</webDriverClassName>
					<specRunnerTemplate>REQUIRE_JS</specRunnerTemplate>
					<jsSrcDir>src/main/web/ua/com/fielden/platform/web</jsSrcDir>
					<jsTestSrcDir>src/test/web/ua/com/fielden/platform/web</jsTestSrcDir>
					<customRunnerConfiguration>src/test/web/ua/com/fielden/platform/web/app/testspike/jasmine.txt</customRunnerConfiguration>
					<preloadSources>
						<source>vendor/require.js</source>
					</preloadSources>
				</configuration>
			</plugin>
		</plugins>

This instructions tells Maven to use maven-jasmin-plugin for testing JavaScript code. The most interesting part of these instructions is plugin configurations. The following is an explanation of the configuration options used in this example:

  • <webDriverClassName>...</webDriverClassName> - tells plugin which web driver (web emulator) to use for running test cases. In our example org.openqa.selenium.phantomjs.PhantomJSDriver (PhantomJS web driver) is used. Next step shows where to download and how to install that web driver.
  • <specRunnerTemplate>...</specRunnerTemplate> - tells plugin which html template to use to run jasmine unit tests. Fortunately maven-jasmine-plugin has two predefined templates: DEFAULT and REQUIRE_JS. If your JavaScript code and test are wrapped in RequireJS module then REQUIRE_JS template should be used otherwise DEFAULT template can be used.
  • <jsSrcDir>...</jsSrcDir> - tells plugin where all JavaScript sources to test can be found. In our case all sources are in src/main/web/ua/com/fielden/platform/web directory.
  • <jsTestSrcDir>...</jsTestSrcDir> - tells plugin where all Jasmine tests can be found. In our case all tests are in src/test/web/ua/com/fielden/platform/web directory.
  • <customRunnerConfiguration>...</customRunnerConfiguration> - this option allows to specify file path that contains additional instructions for script loader (mostly file paths, a URL, or a classpaths). When testing RequireJS modules then this option must point to shim file. In our example shim file is located at src/test/web/ua/com/fielden/platform/web/app/testspike/jasmine.txt.
  • <preloadSources><source>...</source></preloadSources> - defines the list of JavaScript source files that must be loaded before test run (it might be some third party JavaScript libraries). In our example only RequireJS library must be preloaded.

Other maven-jasmine-plugin configuration options can be found at http://searls.github.io/jasmine-maven-plugin/test-mojo.html

Step 2: Download and install PhantomJS

Download PhantomJS for preferred operating system from http://phantomjs.org/download.html and install it. After the installation please make sure that phantomjs executable is in your system's PATH by running phantomjs command in the terminal.

Clone this wiki locally