0
0
Selenium Javatesting~10 mins

Jenkins pipeline integration in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test automates a login page using Selenium WebDriver in Java. It verifies that the login button is clickable and that the user is redirected to the dashboard page after login. The test is designed to be run in a Jenkins pipeline to ensure continuous integration.

Test Code - JUnit 5 with Selenium WebDriver
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.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class LoginTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        // Assuming chromedriver is in system PATH
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, 10);
    }

    @Test
    public void testLogin() {
        driver.get("https://example.com/login");

        WebElement usernameInput = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
        WebElement passwordInput = driver.findElement(By.id("password"));
        WebElement loginButton = driver.findElement(By.id("loginBtn"));

        usernameInput.sendKeys("testuser");
        passwordInput.sendKeys("password123");

        wait.until(ExpectedConditions.elementToBeClickable(loginButton));
        loginButton.click();

        wait.until(ExpectedConditions.urlContains("/dashboard"));
        String currentUrl = driver.getCurrentUrl();
        assertEquals("https://example.com/dashboard", currentUrl);
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts - JUnit initializes and runs testLogin methodNo browser open yet-PASS
2ChromeDriver instance created and browser window opensBrowser window is blank-PASS
3Navigates to https://example.com/loginLogin page is loaded with username, password fields and login buttonWaits for presence of username input fieldPASS
4Finds username, password input fields and login button by their IDsAll elements are present on the page-PASS
5Enters 'testuser' in username and 'password123' in password fieldsInput fields filled with test credentials-PASS
6Waits until login button is clickableLogin button is enabled and clickable-PASS
7Clicks the login buttonBrowser sends login request-PASS
8Waits until URL contains '/dashboard'Page redirects to dashboardChecks current URL equals 'https://example.com/dashboard'PASS
9Test completes and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Login button is not found or not clickable, or URL does not change to dashboard after login
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the login button?
AThat the URL changes to the dashboard page
BThat the login button becomes disabled
CThat the username field is cleared
DThat an alert popup appears
Key Result
Use explicit waits like WebDriverWait with ExpectedConditions to handle dynamic page elements and avoid flaky tests in Jenkins pipelines.