Jenkins pipeline integration in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration; public class LoginTest { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { // Set path to chromedriver executable if needed // System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.manage().window().maximize(); } @Test public void testLogin() { driver.get("https://example.com/login"); WebElement usernameInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); usernameInput.sendKeys("testuser"); WebElement passwordInput = driver.findElement(By.id("password")); passwordInput.sendKeys("Test@1234"); WebElement loginButton = driver.findElement(By.id("loginBtn")); loginButton.click(); // Wait for URL to change to dashboard wait.until(ExpectedConditions.urlToBe("https://example.com/dashboard")); Assert.assertEquals(driver.getCurrentUrl(), "https://example.com/dashboard", "URL after login should be dashboard"); WebElement welcomeMessage = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("welcomeMessage"))); Assert.assertTrue(welcomeMessage.isDisplayed(), "Welcome message should be visible on dashboard"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } } // Jenkins pipeline snippet (Jenkinsfile) to run this test: // pipeline { // agent any // stages { // stage('Checkout') { // steps { // checkout scm // } // } // stage('Build and Test') { // steps { // sh './mvnw clean test' // } // } // } // }
This Java test uses Selenium WebDriver with TestNG to automate the login test.
setUp() initializes the Chrome browser and sets an explicit wait.
testLogin() opens the login page, enters username and password, clicks login, then waits for the dashboard URL and welcome message to appear. Assertions verify the URL and element visibility.
tearDown() closes the browser after tests.
The Jenkins pipeline snippet shows how to run the Maven test command automatically on Jenkins after code checkout.
This structure follows best practices: explicit waits avoid flaky tests, TestNG manages test lifecycle, and the pipeline automates running tests on code changes.
Now add data-driven testing with 3 different username and password combinations
Practice
Solution
Step 1: Understand Jenkins pipeline role
Jenkins pipelines automate tasks like building and testing software continuously.Step 2: Identify Selenium test integration benefit
Integrating Selenium tests in Jenkins runs tests automatically to find problems early.Final Answer:
To automate running tests and catch issues early -> Option AQuick Check:
Automation + early detection = A [OK]
- Thinking Jenkins replaces Selenium tests
- Believing tests run only manually
- Confusing Jenkins with test writing tool
Solution
Step 1: Recall Jenkins declarative syntax
Stages use the format stage('name') { steps { ... } } to define actions.Step 2: Check each option's syntax
stage('Test') { steps { sh 'mvn test' } } matches correct syntax; others have wrong punctuation or keywords.Final Answer:
stage('Test') { steps { sh 'mvn test' } } -> Option CQuick Check:
Correct stage block syntax = B [OK]
- Using colons instead of braces
- Omitting steps block
- Using 'run' instead of 'sh' for shell commands
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean compile'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
}
}Solution
Step 1: Understand default Jenkins pipeline behavior
By default, if a shell command fails (non-zero exit), Jenkins stops and marks build failed.Step 2: Apply this to Maven test failure
Since 'mvn test' fails, the pipeline stops and build is marked failed.Final Answer:
Pipeline stops and marks build as failed -> Option BQuick Check:
Failing shell command stops pipeline = A [OK]
- Assuming pipeline continues after failure
- Thinking Jenkins retries automatically
- Believing build is marked successful on failure
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean compile'
}
}
stage('Test') {
sh 'mvn test'
}
}
}
What is the error?Solution
Step 1: Check 'Test' stage structure
Each stage must have a steps block wrapping commands; 'Test' stage lacks it.Step 2: Identify impact of missing steps block
Without steps block, Jenkins ignores commands inside the stage, so tests don't run.Final Answer:
Missing steps block inside 'Test' stage -> Option AQuick Check:
Every stage needs steps block = C [OK]
- Omitting steps block in stages
- Confusing agent syntax
- Thinking post section runs tests
Solution
Step 1: Ensure test stage runs after successful build
By default, stages run sequentially; test stage runs only if build succeeds.Step 2: Use 'post' block with 'always' to archive reports
The 'post' block with 'always' runs regardless of test success or failure, ensuring reports are archived.Final Answer:
Use 'post' block with 'always' to archive reports and normal stages for build and test -> Option DQuick Check:
Sequential stages + post always archive = D [OK]
- Running tests in parallel with build
- Archiving only on success or failure
- Skipping build stage before tests
Ready to prove it?
Pass the test and this pattern moves unknown
