Integrating Selenium with Maven and Jenkins for CI/CD
๐ Integrating Selenium with Maven and Jenkins for CI/CD
If you're building automated browser tests with Selenium, you can use Maven to manage your test project and Jenkins to run tests automatically in your CI/CD pipeline.
๐งฑ Step 1: Set Up a Selenium Project with Maven
✅ 1. Create a Maven Project
You can create it from an IDE like IntelliJ or from the terminal:
bash
Copy
Edit
mvn archetype:generate -DgroupId=com.example -DartifactId=selenium-tests -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
✅ 2. Add Selenium Dependencies to pom.xml
xml
Copy
Edit
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.21.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
๐งช Step 2: Write a Simple Selenium Test
✅ Example LoginTest.java:
java
Copy
Edit
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginTest {
WebDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
}
@Test
public void testTitle() {
driver.get("https://example.com");
assert driver.getTitle().contains("Example");
}
@After
public void tearDown() {
driver.quit();
}
}
⚙️ Step 3: Run Tests with Maven
You can run your tests locally with:
bash
Copy
Edit
mvn test
๐งช Step 4: Set Up Jenkins for CI/CD
✅ 1. Install Jenkins
You can download and install Jenkins from: https://jenkins.io/download
Or run it with Docker:
bash
Copy
Edit
docker run -p 8080:8080 jenkins/jenkins:lts
✅ 2. Install Required Plugins
Maven Integration
Git Plugin
JUnit Plugin (optional for test reporting)
๐ Step 5: Create a Jenkins Job
Go to Jenkins Dashboard → New Item
Choose Freestyle Project
Give it a name (e.g., Selenium-Maven-Test)
Under Source Code Management, select Git
Add your Git repository URL
Under Build Triggers, select:
Poll SCM or GitHub webhook for automation
Under Build section:
Add a build step → Invoke top-level Maven targets
Goal: clean test
๐ Step 6: View Results
After running the job, Jenkins will:
Clone the project
Install dependencies using Maven
Run your Selenium tests
Show pass/fail results in the console output
✅ Optional: Add Test Reports to Jenkins
To visualize test results:
In your pom.xml, add the Surefire plugin:
xml
Copy
Edit
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>
In Jenkins:
Add Post-build Action → Publish JUnit test result report
Set path as: **/target/surefire-reports/*.xml
๐ Summary
Tool Role
Selenium Automates browser tests
Maven Manages dependencies and builds
Jenkins Automates testing in CI/CD pipelines
๐ง Final Tips
Use Headless browsers (like Chrome headless) in CI to run without a GUI.
Add screenshot capture on test failure.
Integrate with email or Slack for alerting failed builds.
Learn Selenium JAVA Training in Hyderabad
Read More
Creating a Selenium Framework from Scratch Using Java and TestNG
How to Take Screenshots in Selenium Automatically on Failure
Cross-Browser Testing with Selenium WebDriver
Implicit vs Explicit Waits in Selenium – What’s the Difference?
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment