Create a test project with Selenium2, TestNG and Maven

Test automation is a blast! It’s something everyone should be doing at their companies, either big or small, since it will save you TONS of work.

In this tutorial we’ll create a small project with Maven, and then create some tests with TestNG that use Selenium2 with WebDriver to execute your tests in a web browser.

Create the Maven Project

We will create a brand new Maven project for this.  No needs for a specific archetype (web, db access, etc).  Just run this command in a terminal window:

This will create the following file and directory structure (typical maven project):

Tweak the pom.xml file

In order to support Selenium2 with WebDriver and TestNG, we need to tweak the pom.xml file generated.


Change the class

Now, update the file, to remove all references to JUnit and add the TestNG and Selenium2 wiring

Run the tests

We’re all set!  Now to run the tests you can execute the following command in a terminal window:

The good thing about this is that if you keep adding more test classes, they will be grabbed by maven and be executed, without extra tweaking in the project.

If in the future you have multiple tests, but need to run only one, you can always use the -Dtest flag from Maven:

This is all for now.  With this, you can begin writing your automated tests with any IDE that supports Maven (IntelliJ, Netbeans, Eclipse), and since we use  Maven, it can be easily hooked up to a CI server, just like Jenkins.