Apache Maven
π Apache Maven β Complete Guide for Selenium Automation
πΉ 1. What is Maven?
Buil**d Automation & Project M**anagement Tool for Java projects.
Handles:
Depe**ndency management** β JARs via
pom.xml(no manual downloads).Standard project structure β Consistency across teams.
Build lifecycle β Compile β Test β Package β Deploy.
Plugins β Extend functionality (TestNG, Surefire, Reporting).
CI/CD Integratio**n** β Works with Jenkins, GitHub Actions, etc.
π‘ In automation frameworks, Maven ensures you donβt manually manage Selenium/TestNG JARs.
πΉ 2. Maven Installation & Setup
Download Maven from Apache Maven.
Extract and set environment variables:
MAVEN_HOME = C:\``apache-mavenAdd
%MAVEN_HOME%``\bintoPATH.
Verify:
mvn -v
πΉ 3. Standard Maven Project Structure
selenium-framework/
βββ pom.xml # Project configuration
βββ src/
β βββ main/
β β βββ java/ # Utility classes, Page Objects
β β βββ resources/ # Config files (e.g., log4j.properties)
β βββ test/
β βββ java/ # Selenium TestNG test classes
β βββ resources/ # testng.xml, test data
βββ target/ # Compiled code, reports, JAR/WAR files
π‘ Interview Point β Maven enforces this convention over configuration, so projects are uniform.
πΉ 4. POM (Project Object Model)
The heart of Mav**en** is pom.xm``l.
Minimal Selenium Project POM:
<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.automation</groupId>
<artifactId>selenium-framework</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.23.0</version>
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<scope>test</scope>
</dependency>
<!-- WebDriverManager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<!-- Surefire Plugin (for TestNG execution) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
πΉ 5. Maven Lifecycle
Maven defines ph**ases** in the build lifecycle:
validate β Validate project structure
compile β Compile source code
test β Run unit/automation tests (via TestNG/JUnit)
package β Package code into JAR/WAR
install β Install into local repository (
~/.m2/repository)deploy β Deploy to remote repository (e.g., Nexus, Artifactory)
π‘ For automation:
mvn cl``ean testis most common.
πΉ 6. Dependency Scope
co**mpile** (default) β Available everywhere
test β Only in
s``rc/test/java(e.g., TestNG)provided β Provided by container (e.g., servlet-api)
runtime β Needed only at runtime
π‘ Selenium = compile, TestNG = test.
πΉ 7. Running Tests with Maven
- Run all tests:
mvn test
- Clean build:
mvn clean test
- Run specific test class:
mvn -Dtest=GoogleTest test
- Run TestNG groups:
mvn test -Dgroups=smoke
- Pass parameters:
mvn test -Dbrowser=chrome
πΉ 8. Profiles (Multi-Environment Setup)
<profiles>
<profile>
<id>qa</id>
<properties>
<env>qa</env>
<browser>chrome</browser>
</properties>
</profile>
<profile>
<id>staging</id>
<properties>
<env>staging</env>
<browser>firefox</browser>
</properties>
</profile>
</profiles>
Run:
mvn test -Pqa
π‘ Helps in running the same suite against QA / UAT / Prod.
πΉ 9. Reports in Maven
Su**refire Reports** β default in
target/surefire-reportsExtentReports / Allure β add dependency + listener
Generate HTML report:
mvn surefire-report:report
πΉ 10. Maven with Jenkins (CI/CD)
Install Maven in Jenkins (Global Tool Configuration).
Create a job β B**uild Step β Invoke top-level Maven targets** β enter
clean test.Publish TestNG results:
Path =**/target/surefire-reports/testng-results.xml.Run builds automatically on GitHub push.
πΉ 11. Advanced Maven Usage
Multi-Module Pro**ject** β separate
frameworkandtestsmodules.Dependency Tree:
mvn dependency:tree
- Effective POM:
mvn help:effective-pom
- Check latest ver**sions**:
mvn versions:display-dependency-updates
πΉ 12. Interview-Focused Quick Points
What is Maven?
β Build automation & dependency management tool.Difference between
install&deploy?
βinstallβ local repo;deployβ remote repo.How to run specific TestNG suite with Maven?
β Configure insurefire-plugin, thenmvn test.How to handle multiple environments?
β Use Maven profiles (-Pqa,-Pstaging).How does Maven integrate with Jenkins?
β Runmvn clean test, publish surefire/testng results.
1. What is a Maven Profile?
A Maven profile is like a configuration switch inside your pom.xml that lets you:
Change test environment (QA, UAT, Staging, Prod).
Change browser type (Chrome, Firefox, Edge).
Change data/config paths (DB connection strings, API endpoints, file paths).
Include/exclude certain test suites.
Benefit:
You donβt need to touch Java code for these changes β you just pick a profile at runtime using:
bashCopyEditmvn clean test -Pqa
2. Why We Use Profiles in Selenium Projects
| Reason | Example |
| Environment Switching | QA β https://qa.app.com, UAT β https://uat.app.com |
| Browser Switching | chrome, firefox, edge |
| Different Data Sets | Run with staging DB vs local DB |
| Selective Execution | Run only smoke tests or full regression |
| Jenkins Integration | Pass profile as build parameter in Jenkins job |
3. Example pom.xml with Profiles
Hereβs a real-world Maven profile setup for Selenium + TestNG:
xmlCopyEdit<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>selenium-automation</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<profiles>
<!-- QA Environment -->
<profile>
<id>qa</id>
<properties>
<base.url>https://qa.myapp.com</base.url>
<browser>chrome</browser>
</properties>
</profile>
<!-- UAT Environment -->
<profile>
<id>uat</id>
<properties>
<base.url>https://uat.myapp.com</base.url>
<browser>firefox</browser>
</properties>
</profile>
<!-- Regression on Chrome -->
<profile>
<id>regression</id>
<properties>
<base.url>https://qa.myapp.com</base.url>
<browser>chrome</browser>
<suiteXmlFile>testng-regression.xml</suiteXmlFile>
</properties>
</profile>
</profiles>
<build>
<plugins>
<!-- Surefire Plugin to run TestNG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
<systemPropertyVariables>
<base.url>${base.url}</base.url>
<browser>${browser}</browser>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
4. Using These Profiles in Selenium Code
javaCopyEditimport org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestBase {
public static WebDriver driver;
public void setUp() {
String baseUrl = System.getProperty("base.url");
String browser = System.getProperty("browser");
if (browser.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
}
driver.manage().window().maximize();
driver.get(baseUrl);
}
}
5. Running with Maven
bashCopyEditmvn clean test -Pqa
This will:
Use
https://qa.myapp.comas the base URL.Use Chrome browser.
bashCopyEditmvn clean test -Puat
This will:
Use
https://uat.myapp.comas the base URL.Use Firefox browser.
6. Jenkins Integration
Hereβs how to connect Maven profiles with Jenkins:
Jenkins Job Configuration
Add "String Parameter" called
envProfilein Jenkins job.In the Build Step, run:
bashCopyEditmvn clean test -P${envProfile}Now, when you run the Jenkins job:
Enter
qaβ Runs in QA env.Enter
uatβ Runs in UAT env.
7. Advantages in Automation
Single codebase runs on all environments.
Quick switch without modifying code.
Easy to parameterize in Jenkins.
Supports parallel execution with different configs.
If you want, I can prepare a ready-to-run Maven + TestNG + Selenium project with profiles, pom.xml, and sample tests so you can just clone and run it both locally and from Jenkins. That will give you a complete working example from scratch.
You said:
ok
ChatGPT said:
Great β Iβll prepare a ready-to-run Maven + Selenium + TestNG project structure with profiles so you can directly use it.
Hereβs what Iβll include:
pom.xmlwith profiles for QA & UATTestBase class that reads profile properties (
base.url,browser)A sample TestNG test that opens the environment URL and checks page title
TestNG suite XMLs for regression/smoke execution
Do you want me to make it
Basic (just one test + profile switching)
orFull-featured (includes regression/smoke suites, reporting, and Jenkins-ready commands)?
You said:
Full-featured
ChatGPT said:
Alright β
Iβll give you a full-featured Maven + Selenium + TestNG project thatβs:
Profile-based (QA, UAT, Regression, Smoke)
Supports multiple browsers (Chrome, Firefox)
Jenkins-ready (just pass
-Pqaor-Puat)Generates TestNG HTML reports
Has separate suite XMLs for smoke & regression
1. Project Structure
pgsqlCopyEditselenium-automation/
βββ pom.xml
βββ src
β βββ main
β β βββ java
β β βββ base
β β βββ TestBase.java
β βββ test
β βββ java
β β βββ tests
β β βββ LoginTest.java
β β βββ HomePageTest.java
β βββ resources
β βββ testng-smoke.xml
β βββ testng-regression.xml
2. pom.xml
xmlCopyEdit<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>selenium-automation</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.22.0</version>
</dependency>
<!-- WebDriverManager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.9.2</version>
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<!-- QA -->
<profile>
<id>qa</id>
<properties>
<base.url>https://qa.myapp.com</base.url>
<browser>chrome</browser>
<suiteXmlFile>src/test/resources/testng-regression.xml</suiteXmlFile>
</properties>
</profile>
<!-- UAT -->
<profile>
<id>uat</id>
<properties>
<base.url>https://uat.myapp.com</base.url>
<browser>firefox</browser>
<suiteXmlFile>src/test/resources/testng-regression.xml</suiteXmlFile>
</properties>
</profile>
<!-- Smoke -->
<profile>
<id>smoke</id>
<properties>
<base.url>https://qa.myapp.com</base.url>
<browser>chrome</browser>
<suiteXmlFile>src/test/resources/testng-smoke.xml</suiteXmlFile>
</properties>
</profile>
</profiles>
<build>
<plugins>
<!-- Surefire Plugin to run TestNG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
<systemPropertyVariables>
<base.url>${base.url}</base.url>
<browser>${browser}</browser>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. Base Class (TestBase.java)
javaCopyEditpackage base;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class TestBase {
protected static WebDriver driver;
public void setUp() {
String browser = System.getProperty("browser");
String baseUrl = System.getProperty("base.url");
if (browser.equalsIgnoreCase("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else {
throw new RuntimeException("Invalid browser: " + browser);
}
driver.manage().window().maximize();
driver.get(baseUrl);
}
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
4. Sample Test (LoginTest.java)
javaCopyEditpackage tests;
import base.TestBase;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class LoginTest extends TestBase {
@BeforeMethod
public void beforeTest() {
setUp();
}
@Test
public void verifyTitle() {
String title = driver.getTitle();
System.out.println("Page Title: " + title);
Assert.assertTrue(title.contains("MyApp"), "Title verification failed!");
}
@AfterMethod
public void afterTest() {
tearDown();
}
}
5. TestNG Suite Files
testng-smoke.xml
xmlCopyEdit<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Smoke Suite">
<test name="Smoke Tests">
<classes>
<class name="tests.LoginTest"/>
</classes>
</test>
</suite>
testng-regression.xml
xmlCopyEdit<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Regression Suite">
<test name="Regression Tests">
<classes>
<class name="tests.LoginTest"/>
<class name="tests.HomePageTest"/>
</classes>
</test>
</suite>
6. Run Commands
bashCopyEdit# Run QA Regression
mvn clean test -Pqa
# Run UAT Regression
mvn clean test -Puat
# Run Smoke Tests in QA
mvn clean test -Psmoke
7. Jenkins Integration
Create a String Parameter in Jenkins called
envProfile.Build command in Jenkins:
bashCopyEditmvn clean test -P${envProfile}Now, in Jenkins:
Enter
qaβ Runs regression in QA.Enter
smokeβ Runs smoke in QA.Enter
uatβ Runs regression in UAT.