0
0
Selenium Javatesting~15 mins

Why CI integration enables continuous testing in Selenium Java - Automation Benefits in Action

Choose your learning style9 modes available
Verify that the login functionality works correctly when integrated with CI
Preconditions (3)
Step 1: Open the login page URL
Step 2: Enter 'testuser' in the username field
Step 3: Enter 'Test@123' in the password field
Step 4: Click the login button
Step 5: Verify that the user is redirected to the dashboard page
✅ Expected Result: User successfully logs in and dashboard page is displayed with URL containing '/dashboard'
Automation Requirements - Selenium WebDriver with JUnit 5
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Verify the login button is clickable before clicking
Verify username and password fields are present and enabled
Best Practices:
Use explicit waits to wait for elements to be clickable or visible
Use Page Object Model to separate page interactions
Use assertions from JUnit 5 for validation
Use meaningful element locators (id or name preferred)
Close the browser after test execution
Automated Solution
Selenium Java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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 static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration;

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

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com/login");
    }

    @Test
    public void testSuccessfulLogin() {
        WebElement usernameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
        WebElement passwordField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password")));
        WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn")));

        usernameField.sendKeys("testuser");
        passwordField.sendKeys("Test@123");
        loginButton.click();

        wait.until(ExpectedConditions.urlContains("/dashboard"));
        String currentUrl = driver.getCurrentUrl();
        assertTrue(currentUrl.contains("/dashboard"), "URL should contain '/dashboard' after login");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with JUnit 5 to automate the login test.

Setup: We open the browser and navigate to the login page before each test.

Test: We wait explicitly for username, password fields, and login button to be visible and clickable. Then we enter the credentials and click login.

We wait until the URL contains '/dashboard' to confirm successful login and assert this condition.

Teardown: We close the browser after the test to clean up.

This approach uses explicit waits to avoid timing issues and meaningful locators (id). It follows best practices for maintainable and reliable tests.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() instead of explicit waits
Using brittle XPath locators that rely on full element paths
Not closing the browser after test execution
Bonus Challenge

Now add data-driven testing with 3 different sets of user credentials (valid and invalid) to verify login behavior.

Show Hint