0
0
Selenium Javatesting~15 mins

Jenkins pipeline integration in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Automate login test and integrate with Jenkins pipeline
Preconditions (3)
Step 1: Open browser and navigate to 'https://example.com/login'
Step 2: Enter 'testuser' in username field with id 'username'
Step 3: Enter 'Test@1234' in password field with id 'password'
Step 4: Click on login button with id 'loginBtn'
Step 5: Verify that the URL changes to 'https://example.com/dashboard'
Step 6: Verify that an element with id 'welcomeMessage' is displayed
✅ Expected Result: User is successfully logged in, dashboard page is displayed with welcome message visible
Automation Requirements - Selenium WebDriver with TestNG
Assertions Needed:
Verify current URL is 'https://example.com/dashboard'
Verify welcome message element is displayed
Best Practices:
Use Page Object Model to separate page locators and actions
Use explicit waits to wait for elements
Use TestNG annotations for setup and teardown
Use Jenkins pipeline to run tests automatically on code commit
Automated Solution
Selenium Java
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.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Hardcoding XPath locators that are brittle
Not closing the browser after tests
Mixing test logic and page locators in the same class
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations

Show Hint