Integrating Java Selenium Automation in Azure DevOps CI Pipeline
Automation testing plays a crucial role in CI/CD pipelines to ensure software quality. If you are using Java Selenium for test automation and want to integrate it into an Azure DevOps pipeline, this guide will walk you through the step-by-step process.
Step 1: Prerequisites
Before proceeding, make sure you have:
If your organization qualifies for a free grant:
Within 48 hours, Microsoft will review your request and send an approval email. Once approved, you’ll be able to run jobs on Microsoft-hosted agents.
Even after submitting the free parallel job request, users can still use self-hosted agents as an alternative.
Step 2: Push Code to a Repository
Ensure your project is in a Git repository that Azure DevOps can access. If using Azure Repos:
If using GitHub/GitLab:
Step 3: Create an Azure Devops Pipeline
Recommended by LinkedIn
Step 4: Configure the YAML File for the Pipeline
System will generate a .yml file for your pipeline, now configure the .yml file properly. Below is a sample YAML file for Maven project :
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
# other options: 'macOS-latest', ' windows-latest
steps:
# Use a task to install Java 17
- task: JavaToolInstaller@1
displayName: Install Java
inputs:
versionSpec: '21'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
# Run Maven build and tests
- task: Maven@4
displayName: Configuring maven project
inputs:
mavenPomFile: 'pom.xml'
mavenOptions: '-Xmx3072m'
javaHomeOption: 'JDKVersion'
jdkVersionOption: '1.21'
jdkArchitectureOption: 'x64'
publishJUnitResults: true
testResultsFiles: '**/surefire-reports/TEST-*.xml'
goals: 'clean test'
Step 5: Setup Class for Selenium Tests
A proper Setup Class ensures browser initialization and cleanup. Example:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import java.time.Duration;
public class BaseTest {
public WebDriver driver;
@BeforeTest
public void setup() {
// Configure Chrome options
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
options.addArguments("--no-sandbox"); // Important for Linux
options.addArguments("--disable-dev-shm-usage"); // Prevents memory issues
options.addArguments("--headless"); // Run without GUI (useful for CI/CD pipelines)
options.addArguments("--disable-gpu"); // Disable GPU acceleration (Linux)
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
}
@AfterTest
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
Make sure chromedriver is installed or use WebDriverManager to manage drivers automatically.
Step 6: Run and Verify the Pipeline
Step 7: View Test Results in Azure DevOps
Once the pipeline runs successfully:
Conclusion
By following these steps, you have successfully integrated Java Selenium automation into an Azure DevOps CI pipeline. This ensures automated test execution and reporting, making your CI/CD process more efficient.
Let me know in the comments if you have any questions!
Nice Work 👍