Introduction
Initially this post was going to contain instructions on running tests locally, and on SecureCI. Due to the lengthiness of the post, I decided to break this up into two different posts. This post will cover just setting up and running tests locally, building on the previous posts of creating some simple cucumber tests and building a sturdy selenium framework.
Running Locally
To run the tests locally, we will be using Ant to compile and run. We could also use the JDK to compile our file, but Ant will automagically find all of our includes, and perform an easy cleanup and compilation of results.
Install a JVM
From the previous blog posts, we should already have a JVM installed on the machine. If so, skip this step, and move onto Installing Ant.
Regardless of your operating system, you will need to install some Java virtual machine (JVM). You may either install a Java Runtime Environment (JRE), or a Java Development Kit (JDK), depending on what you want to do. If you intend to use Eclipse for Java development, then you should install a JDK (the JDK includes–among other useful things–the source code for the standard Java libraries). If you aren’t planning to use Eclipse for Java development and want to save some disk space, install a JRE. The suggestion is to have everyone running with a JDK, so a few useful ones are listed below:
Java will automatically be added to your system path, but it is useful to also create a system environment variable for ‘JAVA_HOME‘. Below are instructions for doing this in Windows 7:
- Right click on my computer
- Click properties
- Select advanced system settings
- Click new under system variables
- Add variable name of ‘JAVA_HOME‘ and variable value of ‘C:\Program Files (x86)\Java\jre7\‘ (or wherever java was installed)
- Click OK
Install Ant
Download the library files https://ant.apache.org/bindownload.cgi. Unzip the folder, and copy the binaries to the Program Files folder. Add the Ant bin folder to your system path. Below are instructions for doing this in Windows 7:
- Right click on my computer
- Click properties
- Select advanced system settings
- Click environment variables
- Select PATH and click edit
- Add ‘;C:\Program Files\apache-ant-1.8.4\bin‘ to the end of the file (or wherever Ant was copied to)
- Click OK
- Click new under system variables
- Add variable name of ‘ANT_HOME‘ and variable value of ‘C:\Program Files\apache-ant-1.8.4\‘ (or wherever Ant was copied to)
- Click OK
Setup Ant’s Build File
Ant operates by executing an xml file with instructions on how to compile, run, and handle errors. Without a build.xml file, Ant simply won’t run, and with a poorly written file, you’ll notice strange behavior in your application’s execution. The below file was written specifically for the setup we are utilizing, pointing to required jars, and formatting the results nicely.
In the main project directory, create a file titled ‘build.xml’. Some changes may be necessary based on your particular configuration, so take note of the comments explaining each line.
<!-- basic project setup: project name, relative directory, and default ant target --> <project name="cosmic comix" basedir="." default="runcukes"> <!-- the location of all the jar files we downloaded --> <property name="jars" value="lib"/> <target name="classpath"> <path id="classpath"> <!-- we can set multiple filesets if our jar files are distributed in multiple folders --> <fileset dir="${jars}"> <include name="**/*.jar"/> </fileset> <pathelement location="target/classes"/> </path> </target> <!-- the ant job for compiling our code --> <target name="compile" depends="classpath"> <mkdir dir="target/classes"/> <javac srcdir="src" destdir="target/classes" classpathref="classpath" includeantruntime="false"/> </target> <!-- the ant job for running our tests --> <target name="runcukes" depends="compile"> <mkdir dir="target/cucumber-junit-report"/> <java classname="cucumber.api.cli.Main" fork="true" failonerror="false" resultproperty="cucumber.exitstatus"> <classpath refid="classpath"/> <arg value="--format"/> <!-- creates our junit report --> <arg value="junit:target/cucumber-junit-report/allcukes.xml"/> <arg value="--format"/> <arg value="pretty"/> <arg value="--format"/> <!-- creates our cucumber html friendly report --> <arg value="html:target/cucumber-html-report"/> <arg value="--format"/> <!-- creates our cucumber json friendly report --> <arg value="json:target/cucumber.json"/> <arg value="--glue"/> <!-- identifies the package (folder) where the cucumber definitions live --> <arg value="comix.cosmic.definitions"/> <!-- identifies the folder where the feature files live (looks in all subfolders) --> <arg value="src"/> </java> <!-- writes out information to junit report --> <junitreport todir="target/cucumber-junit-report"> <fileset dir="target/cucumber-junit-report"> <include name="allcukes.xml"/> </fileset> <report format="frames" todir="target/cucumber-junit-report"/> </junitreport> <!-- checks our exit status, and determines success or failure --> <fail message="Cucumber failed"> <condition> <not> <equals arg1="${cucumber.exitstatus}" arg2="0"/> </not> </condition> </fail> </target> <!-- the ant job for cleaning up our environment --> <target name="clean"> <delete dir="target"/> </target> </project>
Execute Tests
Open up the command prompt. Navigate to the folder where the test project is checked out using the cd command. Once at the folder, if these tests have been before, it’s best to clean out the results folder. Run the command:
ant clean
Once that completes, run the following command to execute the tests:
ant
We can view the results by looking at the file ‘target/cucumber-html-report/index.html‘