Integrating Java Selenium Automation in Azure DevOps CI Pipeline

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:

  • A Java Selenium Automation project (Maven/Gradle based).
  • Azure DevOps account. Create azure devops account for free. After creating account make sure you're an administrator of the Azure Devops project that you want to use.
  • To run pipelines on Microsoft-hosted agents, your Azure DevOps organization must have Microsoft-hosted parallel jobs enabled. You have two options:
  • Purchase a Parallel Job: If your organization requires multiple concurrent pipelines, you can buy parallel jobs.
  • Request a Free Grant – If eligible, you can request one free parallel job from Microsoft.
  • How to Request a Free Parallel Job

If your organization qualifies for a free grant:

  1. Visit this link: Azure DevOps Parallelism Request
  2. Fill out the Azure DevOps Parallelism Request Form
  3. Submit the request

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.

  • A Git repository (Azure Repos/GitHub/GitLab/Bitbucket)
  • Installed Maven (if using a Maven-based project)
  • A proper test framework like TestNG or JUnit

Step 2: Push Code to a Repository

Ensure your project is in a Git repository that Azure DevOps can access. If using Azure Repos:

  • Go to Azure DevOps -> Repos
  • Clone the repository and push your Selenium automation code

If using GitHub/GitLab:

  • Link your repository to Azure DevOps using Service Connections.

Step 3: Create an Azure Devops Pipeline

  • SIgn in to Azure Devops and go to Azure Devops -> Pipeline
  • Select New pipeline or Create pipeline if creating your first pipeline.

Article content

  • Select your code repository (Azure Repos/GitHub/GitLab)

Article content

  • If you select Github then it might be redirected to Github to sign in. If so, enter github credentials. Then, a list of repositories will show, select your repository.

Article content

  • Then you might be redirected to Github to install the Azure Pipeline app. If so, select Approve & install.
  • If you select Azure Repos Git, then a list of repositories will show from your Azure repository, select your repository.

Article content

  • Azure Pipelines will analyze your repository and recommend the Maven/Gradle pipeline template. Choose the template or configure manually.

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

  • Commit and push your .yml file
  • Go to Azure DevOps -> Pipelines
  • Click on Run Pipeline
  • Monitor the execution and check logs

Step 7: View Test Results in Azure DevOps

Once the pipeline runs successfully:

  • Navigate to Pipelines -> Runs -> Tests
  • View the JUnit/TestNG test results and logs


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!



To view or add a comment, sign in

More articles by Md. Tanvir Hossain Mitul

Others also viewed

Explore content categories