0
0
Selenium Javatesting~15 mins

Getting and setting attributes in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify getting and setting attributes of a web element
Preconditions (2)
Step 1: Navigate to the test web page URL.
Step 2: Locate the input field with id 'username'.
Step 3: Get the current value of the 'placeholder' attribute of the input field.
Step 4: Set the 'value' attribute of the input field to 'testuser'.
Step 5: Get the updated 'value' attribute of the input field.
✅ Expected Result: The initial 'placeholder' attribute value is retrieved correctly. The 'value' attribute is set to 'testuser' and verified successfully.
Automation Requirements - Selenium WebDriver with Java and TestNG
Assertions Needed:
Assert that the initial placeholder attribute equals the expected placeholder text.
Assert that the value attribute after setting equals 'testuser'.
Best Practices:
Use explicit waits to ensure the element is present before interacting.
Use By.id locator for the input field.
Use TestNG assertions for validation.
Use try-finally or @AfterMethod to close the browser.
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.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.time.Duration;

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

    @BeforeMethod
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

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

        // Wait for the input field to be visible
        WebElement usernameInput = wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.id("username"))
        );

        // Get the placeholder attribute
        String placeholder = usernameInput.getAttribute("placeholder");
        Assert.assertEquals(placeholder, "Enter username", "Placeholder text should match");

        // Set the value attribute by sending keys
        usernameInput.clear();
        usernameInput.sendKeys("testuser");

        // Get the updated value attribute
        String value = usernameInput.getAttribute("value");
        Assert.assertEquals(value, "testuser", "Value attribute should be 'testuser'");
    }

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

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

Setup: The @BeforeMethod initializes the ChromeDriver and explicit wait.

Test: The test navigates to the page, waits for the input field with id 'username' to be visible, then retrieves its 'placeholder' attribute and asserts it matches the expected text 'Enter username'.

Next, it clears any existing text and sends the keys 'testuser' to set the value. It then retrieves the 'value' attribute and asserts it equals 'testuser'.

Teardown: The @AfterMethod closes the browser to clean up.

This approach uses explicit waits to avoid timing issues, uses By.id locator for reliability, and TestNG assertions for clear validation messages.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() instead of explicit waits
Using incorrect locator like XPath with absolute path
Trying to set attribute directly via JavaScript without verifying element presence
Bonus Challenge

Now add data-driven testing with 3 different usernames to set and verify the value attribute.

Show Hint