Import the testing dependencies

The central and most essential part of a Maven project is its’ pom.xml file. Among other information (like the project’s defining artifactID and groupID), it stores the list of dependencies your project has and the plugins the project will use. Dependencies that are declared within the pom.xml file will be downloaded from the Maven repository configured for the project, into the project, as external libraries or dependencies. The default repository that is configured is the Maven central repository (http://search.maven.org/#search|ga|1|), and except for the situation where you explicitly configure a local repository, this is the place to look for the latest versions of the libraries you will import into your project.

How this works is that upon project compilation (when you run the ‘mvn clean install’ command), Maven will search in the repository it has configured for the libraries you declared in the dependency section. If this is the first time you compiled the project after putting in a new dependency, Maven will look at the project version. If it is not already downloaded to your computer, it will download the library with the specified version from the repository and store it locally. Every subsequent building of the project, where the library does not change its’ version, will not download the project from the repository, as it is already stored locally. On your computer, Maven creates a specific folder where all the dependencies are downloaded, so that it knows where to access them from. This folder is called ‘.m2’.

The libraries that you need to import to test with Selenium and TestNG can be found by searching within the Maven repository. Just search for the following libraries, and inspect the latest available version for them:

  • selenium-firefox-driver : to run tests on Firefox
  • selenium-chrome-driver : to run tests on Chrome
  • other selenium-name-driver types of libraries you would like to run tests on (IE, Safari, Android, etc)
  • selenium-java
  • testng : to use the TestNG library’s capabilities

When you find the libraries, with the latest version, search for the link named ‘pom’. It will open the pom.xml file for that particular project, a similar file to the one you have in your project.

In your own project’s pom.xml file, under <dependencies>, put in, for each of the desire dependency, under a <dependency> tag, the artifactID and groupID of the libraries.

To make versioning of dependencies easily manageable, right below your own project’s declaration, create a section called ‘properties’, where you will declare all the libraries versions. They will have a specific format, namely : ‘projectname.version’. This will be a parameter, that you can reference from within the ‘dependencies’ section of the pom.xml file. Consider multiple libraries that have the same groupID, but different artifactID (they are part of the same organization). It is common for these libraries’ versions to be coordinated, so that a a new release brings with itself the release of multiple libraries. This is the case for most of the Selenium drivers. If you inspect the Maven repository at any time, you will see that these driver libraries have the same latest version. This is a situation where you don’t want to explicitly write the version for each of these libraries in the pom file, but would prefer to reference a variable. This way, when the libraries get a new release, you will only update one place in the pom file, and all the dependencies will be up to date.

For example, for the selenium-chrome-driver:

  • The section holding an example of version would look like:
<properties>
    <selenium.version>2.40.0</selenium.version>
</properties>
  • put in the dependencies’s artifactID, groupID, and reference the project’s version declared inside the ‘properties’ section:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>${selenium.version}</version>
</dependency>

The file with all the necessary dependency declarations can be found below:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>your.group.id</groupId> <artifactId>yourproject</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<selenium.version>latest_version</selenium.version>
<testng.version>latest_version</testng.version>
</properties>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
</dependencies>
</project>

Now, from the command line run the following command again:

mvn clean install

If you’re using Eclipse, you will additionally need to run the following command, from the command line:

mvn eclipse:clean eclipse:eclipse -DdownloadSources -DdownloadJavadocs

This will download your external libraries sources and javadocs into your project, so that you can inspect them directly from Eclipse, to see their code. For IntelliJ this command is not needed. Just refresh the project in your IDE and it’s all setup with the latest dependencies.

The next step is to create the Maven profile for running tests.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s