TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Use Maven and Eclipse to Run Webdriver Tests
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Tutorial

Use Maven and Eclipse to Run Webdriver Tests

Discover step-by-step instructions to run WebDriver tests using Maven in Eclipse.
Last updated: Sep 12, 2023 2:25 am
By Meenakshi Agarwal
Share
12 Min Read
running webdriver tests using maven
running webdriver tests using maven
SHARE

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 the Webdriver project in Maven. And later we’ll walk you through the best approach for running Webdriver tests using Maven in Eclipse IDE. But, you can start with a simple approach to creating a Webdriver project in six steps. Let’s now unearth the Maven concept.

Contents
What is Maven?Why use Maven in the Selenium Project?How does Maven handle the updates of a dependency?Install the Maven plug-in in Eclipse.Set up a Maven Project in Eclipse.Step-1: Navigate to File ->New->Others->Maven->Maven Project.Step-2: Select the Checkbox “Create a simple project” (skip archetype selection) and click Next.Step-3: Provide Group Id and Artifact Id in the window and Click Finish.Step-4: After finishing, you will find the project structure created below. POM.xml created is like that which is used to download all dependencies.Step-5: Now add the dependencies for Selenium ll.Step-6: Final POM.xml contents are as follows.Step-7: Maven will download all the dependency jars into a local repository called <.m2>.Step-8: If the <m2> folder doesn’t populate in Maven dependencies, then you can add those jars manually.Step-9: Upon successful setup, you will find the Maven Dependencies folder below which will have the required dependency jar for the project.Writing Selenium Webdriver Test and Build Using Maven.Step-1: Create a new test class under the package <com.techbeamers.test>.Step-2: Now add the following code to the test class.Step-3: Running Webdriver test cases using maven (Surefire plug-in).Step-4: Test Execution.

First of all, you must note that building a Test Automation framework using Selenium Webdriver isn’t just about downloading the Selenium API and starting scripting. There is much more to do. 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.

How to Run Webdriver Tests Using Maven in Eclipse

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

What is Maven?

Apache Maven is a very powerful and widely used Java project management and builds 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 is some of its magical traits.

3- Maven as a build tool allows 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, and 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 the project’s build, dependency, and documentation.

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

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

Why use Maven in the Selenium Project?

1- Since Maven is a build automation tool, it can manage the Selenium Webdriver test project’s build compilation and documentation. It eases up the task of creating the right project structure and adding and managing jar files in the project’s 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 the above. All we have to do is change the version of the 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.

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.

<repositories>
     <repository>
         <id>libraryId</id>
         <url>http://comanyrepositryId</url>
     </repository>
</repositories>

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

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

Install the Maven plug-in in Eclipse.

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

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

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.

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 the Maven package from the Apache website.

Set up a Maven Project in Eclipse.

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

And then click Next. See the below screenshot.

Create a Project
Running Webdriver Tests Using Maven – Create a Project.

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

Refer to the embedded screenshot.

Save Project
Running Webdriver Tests Using Maven – Save Project.

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.

Step-4: After finishing, you will find the project structure created 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 the POM.xml file are as follows:

Running Webdriver Tests Using Maven - POM Overview
Running Webdriver Tests Using Maven – View POM file.

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

Selenium Maven artifacts are available from the following central repository.

  • https://repo1.maven.org/maven2/org/seleniumhq/selenium/

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

<dependency>
       <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-java</artifactId>
       <version>2.53.1</version>
</dependency>

And following is the dependency for JUnit.

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.9</version>
</dependency>

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

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

<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>com.techbeamers.test</groupId>
  <artifactId>com.techbeamers.selenium</artifactId>
  <version>0.0.1-SNAPSHOT</version>
<dependencies>  
<dependency>
       <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-java</artifactId>
       <version>2.53.1</version>
</dependency>
</dependencies>
</project>

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 the repository. It resides in the .m2 folder. Maven will create separate folders for the different versions and different group ids.

Running Webdriver Tests Using Maven - .M2 Snapshot
Running Webdriver Tests Using Maven – .M2 folder view.

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.

Step-9: Upon successful setup, you will find the Maven Dependencies folder 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.

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.

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

In this, we’ll add code to verify the title of the Google home page. Right-click on the package 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.

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

package com.techbeamers.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


public class GoogleHomePageTitle {
	
	private WebDriver driver; 
	String URL = "http://google.com";

	@BeforeClass
	public void testSetUp() {
		
		driver = new FirefoxDriver();
	}
	
	@Test
	public void verifyGooglePageTittle() {
		driver.navigate().to(URL);
		String getTitle = driver.getTitle();
		Assert.assertEquals(getTitle, "Google");
	}
	
	@AfterClass
	public void tearDown() {
		driver.quit();
	}
}

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.

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>

2- This plug-in will come into the 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.

 <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8.8</version>
 </dependency>

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test">
    <classes>
      <class name="com.techbeamers.test.GoogleHomePageTitle"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

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

Final POM XML.
<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>com.techbeamers.test</groupId>
  <artifactId>com.techbeamers.selenium</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
		<jre.level>1.8</jre.level>
		<jdk.level>1.8</jdk.level>
  </properties>
  	<build>
		<plugins>
			<!-- Compiler plug-in -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${jdk.level}</source>
					<target>${jdk.level}</target>
				</configuration>
			</plugin>
			<!-- Below plug-in is used to execute tests -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.19.1</version>
				<configuration>
					<suiteXmlFiles>
						<!-- TestNG suite XML files -->
						<suiteXmlFile>testng.xml</suiteXmlFile>
					</suiteXmlFiles>
				</configuration>
			</plugin>
		</plugins>
	</build>
 <dependencies>  
 <dependency>
       <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-java</artifactId>
       <version>2.53.1</version>
 </dependency>
 <dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.8.8</version>
 </dependency>
 </dependencies>
</project>

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 – Run Webdriver Tests Using Maven

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

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

All the Best,

TechBeamers.

You Might Also Like

20 Demo Sites for Automation Testing

Page Object Model (POM) and Page Factory Guide in Selenium Java

Selenium 4 Relative Locators Guide

Selenium Version 4 Features – What’s New?

How to Inspect Element in Safari, Android, and iPhone

TAGGED:Advanced Selenium Tutorial
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article How to Use Chrome for Running Webdriver Test Cases How to Use Chrome for Running Webdriver Test Cases
Next Article Writing Data-Driven Tests in Selenium 3.0 How to Write Data-Driven Tests in Selenium

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x