Running Webdriver Tests Using Maven in Eclipse IDE

Maven is a well-known tool for building large Java applications. In this tutorial, we’ll discover what is Maven and what advantages it has over other build tools like ANT? Finally, we’ll expose a detailed process of building Webdriver project in Maven. And later we’ll walk you through the best approach for running Webdriver tests using Maven in Eclipse IDE. But, if you wish to start with a simple approach of creating a Webdriver project, then read these six steps for a quick start. Let’s now unearth the Maven concept.

First of all, you must note that building a Test Automation framework using Selenium Webdriver isn’t just about downloading the Selenium API and start scripting. There is much moe to do. Like you first need an Integrated Development Environment (IDE) to create the test project and other dependencies to build the framework.

And Eclipse is one of the most widely used IDE in the Java world. It provides support for various plug-ins like Maven to ease up the build management tasks for Java projects.

Running Webdriver Tests Using Maven in Eclipse IDE.

Before we get through the procedure for running Webdriver tests using Maven, let’s begin by understanding the Maven.

1- What is Maven?

Apache Maven is a very powerful and widely used Java project management and build management tool. Here is a point-wise summary of its features.

1- It provides support for managing the entire lifecycle of a Java project.

2- Defining the project structure, dependencies, build, and test management are some of its magical traits.

3- Maven as a build tool allow setting up the execution environment for the project code to run independently.

4- It enables a unified platform where you can check out the source code from GIT/SVN, compile and package it into a JAR/WAR file.

5- You want a project management tool, Maven will do it for you. It has the project object model (POM) file to manage project’s build, dependency, and documentation.

6- Manage all project related dependencies using the POM.xml. It helps setting up all the configurations.

7- Last but not the least is its ability to download the project dependency jars automatically from the central repository.

 

2- Why to use Maven in Selenium Project?

1- Since Maven is a build automation tool,  so it can manage the Selenium Webdriver test project’s build compilation and documentation. It eases up the task of creating right project structure, adding and managing jar files in the projects build path.

2- Most important feature of Maven is managing the project dependencies using the POM.xml. Let’s take a simple example of upgrading a single jar file. Say, we were using Selenium version 2.53.1 which we later updated to a newer version. Such type of scenario is easy to handle using Maven as it requires updating the version in the POM file.

3- Now imagine the situation, when we have a large number of jar files with updates available. And if we have to update them manually, then it’ll turn out to be a cumbersome task. Also, there is a high probability of errors even if we’ve them updated manually.

4- Maven comes to the rescue in a situation like above. All we have to do is change the version of target jar files in the POM file. Maven will download the newer versions of all jar files automatically and store them in a local repository.

 

3- How does Maven handle the updates of a dependency?

1- Whenever you update a dependency version in the POM file, Maven first verifies the version of the jar file from the local repository.

2- If the version is available locally, then no action will take place. Else it’ll download the upgrade from the central repository.

3- If the Jars are not available in the central repository, then Maven will look for them in the remote repository.

4- We can configure the remote repository in the <POM.xml> to enable the automatic downloads of dependencies.

Consider the example which you can see below. It displays the configuration of a remote repository in the <POM.xml> file. You need to specify the <ID> and <URL> of the repository where the jar is available.

 

Here comes is a step by step tutorial, where we’ll first create a small project with Maven. And then, will create few tests with TestNG and Selenium 2.0.

Finally, you’ll get to see the steps running Webdriver tests using Maven.

4- Install Maven plug-in in Eclipse.

To add the Maven plug-in to Eclipse IDE, navigate to Help->Eclipse Marketplace.

Look up for Maven and install <Maven Integration for Eclipse> from the searched options.

Running Webdriver Tests Using Maven - Download and Install

Running Webdriver Tests Using Maven – Download and Install.

After installation, you have to restart the Eclipse IDE.

Then, go to the project, right-click on the <POM.xml> and verify that all the options are available as shown below.

Running Webdriver Tests Using Maven - Verify POM

Running Webdriver Tests Using Maven – Checking POM.

Note: For a quick note, you can also run Maven from the command line. To do so, you have to download the latest version of Maven package from Apache website.

5- Setup a Maven Project in Eclipse.

5.1- Step-1: Navigate to File ->New->Others->Maven->Maven Project.

And then click Next. See the below screenshot.

Running Webdriver Tests Using Maven - Create a Project

Running Webdriver Tests Using Maven – Create a Project.

 

5.2- Step-2: Select the Checkbox “Create a simple project” (skip archetype selection) and click Next.

Refer the embed screenshot.

Running Webdriver Tests Using Maven - Save Project

Running Webdriver Tests Using Maven – Save Project.

 

5.3- Step-3: Provide Group Id and Artifact Id in the window and Click Finish.

Running Webdriver Tests Using Maven - Set Group Id

Running Webdriver Tests Using Maven – Set Group Id.

 

5.4- Step-4: After finishing, you will find the project structure created like below. POM.xml created is like that which is used to download all dependencies.

Running Webdriver Tests Using Maven - Folder Structure

Running Webdriver Tests Using Maven – Folder Structure.

 

By default, the contents of POM.xml file is as follows:.

Running Webdriver Tests Using Maven - POM Overview

Running Webdriver Tests Using Maven – View POM file.

 

5.5.- Step-5: Now add the dependencies for Selenium ll.

Selenium Maven artifacts are available in below central repository.

Download Selenium Maven Artifacts.

 

 

For the Selenium library, add the following dependencies in <POM.xml>.

 

And following is the dependency for JUnit.

In a similar manner, if we want to add any other third party jar, then include them in the same fashion.

5.6- Step-6: Final POM.xml contents are as follows.

 

5.7- Step-7: Maven will download all the dependency jars into a local repository called <.m2>.

M2 folder is located at <C:\Users\username\.m2\repository>.

All the jars get downloaded to a folder called as the repository. It resides in .m2 folder. Maven will create separate folders for the different version and different group id.

Running Webdriver Tests Using Maven - .M2 Snapshot

Running Webdriver Tests Using Maven – .M2 folder view.

 

5.8- Step-8: If the <m2> folder doesn’t populate in Maven dependencies, then you can add those jars manually.

  • Open the Eclipse Windows ->Preference.
  • Navigate to Java->Build Path->Classpath Variables.
Running Webdriver Tests Using Maven - Set ClassPath

Running Webdriver Tests Using Maven – Set ClassPath.

 

5.9- Step-9: Upon successful setup, you will find Maven Dependencies folder like below which will have the required dependency jar for the project.

Running Webdriver Tests Using Maven - Check Dependencies

Running Webdriver Tests Using Maven – Check Dependencies.

 

6-  Writing Selenium Webdriver Test and Build Using Maven.

First of all, let’s create a simple example using Selenium and TestNG. Then, we’ll be running Webdriver tests using Maven.

6.1- Step-1: Create a new test class under the package <com.techbeamers.test>.

In this, we’ll add code to verify the title of Google home page. Right-click on the package and Select New->Class and name the class as <GoogleHomePageTitle>.

Running Webdriver Tests Using Maven - Create a Test Class

Running Webdriver Tests Using Maven – Create a Test Class.

 

6.2- Step-2: Now add the following code in the test class.

 

6.3- Step-3: Running Webdriver test cases using maven (Surefire plug-in).

1- Add the following snippet in <POM.xml> to set the Surefire Plug-in for running the tests.

2- This plug-in will come into picture during the testing phase of the software build lifecycle to execute tests.

3- We will also add the TestNG dependency to the <POM.xml> file.

4- We have a test <verifyGooglePageTitle()> in our test class. We ‘ll also include it in the <POM.xml> file.

Finally, the <POM.xml> would look something like this.

6.3.1- Final POM XML.

 

6.4- Step-4: Test Execution.

We are now ready as we’ve completed the required setup. And only the following steps are remaining before running the Webdriver tests using Maven.

  • Right-click on the <POM.xml> and Select Run As->Maven clean.
  • And after that Select Run As-> Maven install.

Finally, you would see the following as output on the console.

Running Webdriver Tests Using Maven - Execution Summary

Running Webdriver Tests Using Maven – Execution Summary.

 

Final Thought.

It was a comprehensive tutorial on What is Maven, Why is it useful and how to use it for running the Webdriver tests.

And hopefully, you all would like it and able to use the concept in your projects.

 

All the Best,

TechBeamers.

Leave a Reply


*