0
0
Selenium Javatesting~10 mins

Getting and setting attributes in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, finds an input field, gets its current placeholder attribute, sets a new placeholder attribute using JavaScript, and verifies the change.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
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 AttributeTest {
    WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
    }

    @Test
    public void testGetAndSetAttribute() {
        driver.get("https://example.com/form");
        WebElement inputField = driver.findElement(By.id("username"));

        // Get the placeholder attribute
        String originalPlaceholder = inputField.getAttribute("placeholder");

        // Set a new placeholder attribute using JavaScript
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].setAttribute('placeholder', 'Enter your user ID')", inputField);

        // Get the updated placeholder attribute
        String updatedPlaceholder = inputField.getAttribute("placeholder");

        // Verify the placeholder changed
        assertEquals("Enter your user ID", updatedPlaceholder);
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://example.com/formPage with form and input field with id 'username' is loaded-PASS
3Finds input element by id 'username'Input field element located on the page-PASS
4Gets the 'placeholder' attribute of the input fieldOriginal placeholder attribute value retrieved (e.g., 'Enter username')placeholder attribute is not nullPASS
5Sets new 'placeholder' attribute to 'Enter your user ID' using JavaScriptInput field's placeholder attribute updated in the DOM-PASS
6Gets the updated 'placeholder' attribute of the input fieldUpdated placeholder attribute value retrievedplaceholder attribute equals 'Enter your user ID'PASS
7Assertion checks that placeholder attribute is updated correctlyTest verifies the attribute changeassertEquals("Enter your user ID", updatedPlaceholder)PASS
8Browser closes and test endsBrowser window closed-PASS
Failure Scenario
Failing Condition: Input element with id 'username' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after setting the new placeholder attribute?
AThat the input field is visible on the page
BThat the page URL is correct
CThat the placeholder attribute value is updated to 'Enter your user ID'
DThat the browser is Chrome
Key Result
Use JavaScript execution in Selenium to set attributes that cannot be changed directly via WebElement methods, and always verify attribute changes with assertions.