One of the overarching goals of the Selenified testing framework is to provide testers with a tool that reduces spin-up time on projects and enables them to very quickly get up to speed with writing tests. As such, it was very important that the process of setting it up is just as simplified.

For this reason, Selenified is designed to integrate with all of the popular Java build tools including Maven, Ant, and Gradle.  This means you can incorporate it into your new or existing development project just like you would any other software library ( i.e by pulling it down from Maven Central).

In this post, we will focus on setting up Selenified in a new or existing Maven project.

Getting started with Selenified with Maven is extremely easy, and involves 3 steps:

  1. Add the Selenified dependency to your POM file
  2. Configure your POM to run your tests
  3. Write and execute your tests

Adding the Selenified dependency to your POM

Ideally, your test code should be located within the Application Under Test’s (AUT) code base to facilitate execution (and CI integration if applicable), so my recommendation is to include them in your development project. Integrating tests with application code also helps simplify version control and more importantly increases the likelihood that developers will actually run them.

One way to accomplish this is to create a new module within the project, in this case, I’m using IntelliJ, so I right-clicked the root folder then selected New->Module. 

The newly created sub-module will contain its own POM file, which will specify all the dependencies and execution configurations for our tests. This provides us the flexibility to either execute tests independently or as a part of the overall project build (you’ll  note that your development project is listed as a “parent” in the POM file.)

...
<parent>
 <artifactId>hello_world5</artifactId>
 <groupId>hello_world5</groupId>
 <version>1.0-SNAPSHOT</version>
</parent>
...

I realize of course that adding test code to your dev project may not always be feasible, so if you have to create a new project to develop your tests, then you can do so using File->New->Project->Maven, then name your project accordingly.

Once this project/module has been created, we need to add the Selenified dependency to our POM file.

...
<dependency>
 <groupId>com.coveros</groupId>
 <artifactId>selenified</artifactId>
 <version>3.0.1</version>
 <scope>test</scope>
</dependency>
...

This will retrieve the Selenified jar file from Maven Central, along with all other libraries required to run your automated functional tests. This is an excellent feature as it absolutely simplifies the dependency management process, meaning you no longer need to chase down Selenium related jar files. The framework takes care of this for you!

Configure your POM to run your tests

Now that we’ve specified Selenified as our dependency, we will configure our POM file to execute the tests, and there are a few ways to do this depending on your project.

Selenified uses TestNG as a runner, so one approach could be to specify our run configs using a combination of the POM file and TestNG.xml file. This file contains a number of execution criteria including which test groups, classes, and packages to run. You can find a very detailed guide for that here

Another option is to bypass the TestNG.xml file altogether and integrate your execution configuration directly into the POM file, (again highlighting the amount of flexibility the framework provides). To do this, we will need to declare a number of attributes as variables in the POM itself, then set default values or override them via the command line during execution.

First, let’s add the following properties to the POM, we will be using the Maven Failsafe plugin to execute our tests:

...
<properties>
 <failsafe.groups.include>sampleInclude</failsafe.groups.include>
 <failsafe.groups.exclude>sampleExclude</failsafe.groups.exclude>
 <failsafe.files.include>**/*Sample.java</failsafe.files.include>
 <failsafe.files.exclude></failsafe.files.exclude>
</properties>
...

As you can see from above, we’re executing tests from the “sampleInclude” group and excluding any tests in the “sampleExclude” group. Again, these values can be overridden by passing them in via the command line, but these defaults will be used otherwise. We are also set up to run any tests in class names ending in “*Sample.java

One of the really neat features Selenified offers is the ability to execute tests in multiple browsers by adding a list of browsers to our execution command, this way we can verify all of our supported browsers simultaneously. To take advantage of this feature, we must add a listener to our Failsafe configuration.

...
<property>
 <name>listener</name>
 <value>com.coveros.selenified.utilities.Transformer</value>
 </property>
</property>
...

Did I mention that the framework also enables parallel test execution which can significantly reduce your regression phase?  In order to do this, you’ll need to increase the number of threads. You can also set the number of verbosity of the failsafe plugin, in order to debug your tests.

Both of these can be added to the properties section of your POM as well.

...
<properties>
 <failsafe.threads>5</failsafe.threads>
 <failsafe.verbosity>3</failsafe.verbosity>
<properties>
...

There are of course quite a few more options that can be passed in when we execute our tests and for more information, please refer to the TestNG and Maven Failsafe plugin reference pages.

We will now set our build configurations by adding the following to the “build” section of our POM file:

...
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-failsafe-plugin</artifactId>
 <version>2.21.0</version>
 <configuration>
 <groups>${failsafe.groups.include}</groups>
 <excludedGroups>${failsafe.groups.exclude}</excludedGroups>
 <includes>
 <include>${failsafe.files.include}</include>
 </includes>
 <excludes>
 <exclude>${failsafe.files.exclude}</exclude>
 </excludes>
 </configuration>
 <executions>
 <execution>
 <id>verify</id>
 <goals>
 <goal>verify</goal>
 </goals>
 </execution>
 </executions>
</plugin>
.....

Once the POM file has been configured, it is time to write our tests. For reference, you can view an example of a complete POM file here

Write and execute your tests

There’s already an excellent guide in the “Writing Tests” section of the Selenified page, so I won’t cover it here. There are also some great resources here including blog posts and video blogs to help you get started writing tests.

Once your tests have been written, you can execute them using the standard Maven commands, for example, to run a suite of tests with the group name “regression” run the following command:

 mvn verify -Dfailsafe.groups.include = regression 

This will run all tests in the “regression group using HtmlUnit as a default, if you’d prefer to run using a different browser such as Chrome or Firefox, then you can pass those arguments as well.

 mvn verify -Dfailsafe.groups.include = regression -Dbrowser=Firefox

To execute the tests in both Chome and Firefox, use the following command:

 mvn verify -Dfailsafe.groups.include = regression -Dbrowser=Chrome,Firefox

And that’s it! Hopefully, this has provided you with some options to get started with your testing project, please feel free to post your thoughts and questions below.

Leave a comment

Your email address will not be published. Required fields are marked *

X