0
0
Selenium Javatesting~15 mins

CSS selector syntax in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify login button using CSS selector
Preconditions (1)
Step 1: Locate the login button using CSS selector '#loginBtn'
Step 2: Click the login button
Step 3: Verify that the URL changes to 'https://example.com/dashboard'
✅ Expected Result: After clicking the login button located by CSS selector, the user is redirected to the dashboard page URL
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the login button is found using CSS selector '#loginBtn'
Verify the page URL after clicking the button is 'https://example.com/dashboard'
Best Practices:
Use explicit waits to wait for the login button to be clickable
Use By.cssSelector locator strategy
Avoid using brittle or overly complex CSS selectors
Use Page Object Model pattern for element locators
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 java.time.Duration;

public class CssSelectorTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        try {
            // Navigate to login page
            driver.get("https://example.com/login");

            // Wait for login button to be clickable using CSS selector
            WebElement loginButton = wait.until(
                ExpectedConditions.elementToBeClickable(By.cssSelector("#loginBtn"))
            );

            // Assert login button is displayed
            if (!loginButton.isDisplayed()) {
                throw new AssertionError("Login button is not displayed");
            }

            // Click the login button
            loginButton.click();

            // Wait for URL to change to dashboard
            wait.until(ExpectedConditions.urlToBe("https://example.com/dashboard"));

            // Assert current URL is dashboard
            String currentUrl = driver.getCurrentUrl();
            if (!"https://example.com/dashboard".equals(currentUrl)) {
                throw new AssertionError("Expected URL 'https://example.com/dashboard' but got '" + currentUrl + "'");
            }

            System.out.println("Test Passed: Login button clicked and dashboard loaded.");
        } finally {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java to automate the manual test case.

First, it opens the Chrome browser and navigates to the login page URL.

It uses an explicit wait to wait until the login button is clickable, locating it by the CSS selector #loginBtn. This ensures the element is ready for interaction.

Then it asserts the button is displayed on the page to confirm the locator is correct.

Next, it clicks the login button and waits until the URL changes to the expected dashboard URL.

Finally, it asserts the current URL matches the expected dashboard URL to verify successful navigation.

The test prints a success message if all assertions pass and closes the browser in a finally block to ensure cleanup.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using overly complex or brittle CSS selectors
Mixing locator strategies inconsistently
Not verifying the element is displayed before clicking
Bonus Challenge

Now add data-driven testing with 3 different CSS selectors for login buttons on different pages

Show Hint