Selenium Project setup using Maven

Selenium Project Using Maven

In the last post we saw How to setup the selenium WebDriver using Eclipse. Now here we will see How to setup the Selenium WebDriver using the Maven project.

Maven is a Build tool for java just as Ant is. [http://maven.apache.org/what-is-maven.html]. Offcourse definitely you would ask what is a build tool ?To answer that question, consider what is required to build a software system. Typically, there is much more to building software than just typing in and then compiling the source code. There is a number of steps required to transform the source into a deployable and useable software solution.

In general, a build tool provides a mechanism to describe the operations and structure of the build process.It determine the steps that need to be executed and in which order, and then manages the execution of those steps

http://maven.apache.org/what-is-maven.htmlMore about Maven here

Setting up Selenium 2 in a Maven project is easy. Maven will download the java bindings and Selenium 2 core libraries plus all of the drivers and will create the project for you, using a maven pom.xml (project configuration) file.

This is how the selenium 2 dependency will look
[sourcecode language=”xml” wraplines=”false” collapse=”false”]
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium</artifactId>
<version>2.25.0</version>
</dependency>
[/sourcecode]

First we will have to create a folder at our local computer to contain our selenium files, as adding the dependency will automatically download all the files

If you know, that you will only use a certain WebDriver implementation, e.g. the FirefoxDriver or SafariDriver, you don’t need to depend on the selenium artifact (which will have dependencies to all WebDriver implementations as well as the support classes). Instead we can add the dependency to just the artifact you need, as below

[sourcecode language=”xml” wraplines=”false” collapse=”false”]
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.0a5</version>
</dependency>
[/sourcecode]

In the WebDriver implementation, there is no necessity for starting the selenium server. But if you use DefaultSelenium RemoteDriver implementation, then you will need to start the server manually or even you can call the sockets method that will connect to the selenium server through your Java code. We will cover this in later posts.

Selenium server dependency will look like this

[sourcecode language=”xml” wraplines=”false” collapse=”false”]
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.25.0</version>
</dependency>
[/sourcecode]

The Whole pom.xml file will look into like the below, (as in seleniumhq website)

[sourcecode language=”xml” wraplines=”false” collapse=”false”]
<?xml version="1.0" encoding="UTF-8"?>
<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>MySeleniumProject</groupId>
<artifactId>MySeleniumProject</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.25.0</version>
</dependency>
<dependency>
<groupId>com.opera</groupId>
<artifactId>SafaiDriver</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.opera</groupId>
<artifactId>SafariDriver</artifactId>
<version>0.16</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
</project>
[/sourcecode]

The pom.xml can be just created with a simple text editor.

Now after this is done. In the command Line or Terminal(Mac) cd to the path where you had created folders for this project and also where you can find the pom.xml and run the following command

mvn clean install

This will download Selenium and all its dependencies and will add them to the project. Now that you had done all the above you can import to your favourite IDE like Eclipse or IntelliJ IDEA.

<a href="http://seleniumhq.org/docs/appendix_installing_java_driver_Sel20_via_maven.html#importing-sel2-0-project-into-intellij-using-maven"Selenium WEbDriver documentation link where it shows the project import steps for a maven proejct
I hope, I covered everything and hope this helps some one.

Leave your comment if any. I will try to reply and get back to you at the earliest

Leave a Comment

*