Understanding Maven For Automation Testing
Understanding Maven For Automation Testing
Maven is a build automation tool used primarily for Java projects. It simplifies project
management by automating the process of downloading dependencies, compiling code,
running tests, and packaging the application. In automation testing, Maven ensures
consistent builds and dependency management.
Key Benefits:
Example:
Let’s say you’re working on a Selenium project. Without Maven, you’d need to download
Selenium JAR files manually. With Maven, you just specify the dependencies in the
pom.xml file, and Maven downloads them for you.
The pom.xml file (Project Object Model) is the heart of a Maven project. It contains
configuration details about the project, including:
Example:
<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>
● The <project> tag is the root of the POM file, defining it as a Maven project.
● xmlns and xsi:schemaLocation define XML namespace and schema details for
Maven.
<groupId>com.example</groupId>
<artifactId>selenium-project</artifactId>
<version>1.0-SNAPSHOT</version>
● <groupId>: Unique identifier for your project, typically your organization or domain
name.
● <artifactId>: Name of the project.
● <version>: Version of your project (e.g., 1.0-SNAPSHOT for a development version).
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.25.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>
● <dependencies>: A list of external libraries your project needs.
● Each <dependency> specifies the groupId, artifactId, and version for the library.
How it works:
Dependencies in Maven are managed through the pom.xml file. Maven retrieves these
dependencies from a central repository (Maven Central) or other specified repositories.
Steps:
Example:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
● This adds Apache Commons Lang library to your project for common string
operations.
Dependency Hierarchy:
Maven resolves conflicts by using the dependency closest to your project in the
dependency tree. Use the mvn dependency:tree command to view it.
Example:
mvn -version
You should see the Maven version and Java version details.
my-project
|-- src/main/java # Application source code
|-- src/test/java # Test code
|-- pom.xml # Project configuration
Here’s a fully functional Selenium framework using Java, Maven, and JUnit:
Project Structure:
selenium-framework
|-- src/main/java
|-- src/test/java
| |-- tests
| | |-- LoginTest.java
| |-- utils
| |-- WebDriverManager.java
|-- pom.xml
1. pom.xml:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.25.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
2. WebDriverManager:
package utils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
3. LoginTest:
package tests;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import utils.WebDriverManager;
@Before
public void setUp() {
driver = WebDriverManager.getDriver();
driver.get("https://example.com/login");
}
@Test
public void testLogin() {
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("password123");
driver.findElement(By.id("loginButton")).click();
@After
public void tearDown() {
driver.quit();
}
}
● Sets up, tests login functionality, and cleans up after the test.
By following these steps, you can set up a professional Selenium framework and manage
it efficiently using Maven. Share your experience on LinkedIn to inspire others in their
automation journey!