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.
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.
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(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | - | PASS |
| 2 | Navigates to https://example.com/form | Page with form and input field with id 'username' is loaded | - | PASS |
| 3 | Finds input element by id 'username' | Input field element located on the page | - | PASS |
| 4 | Gets the 'placeholder' attribute of the input field | Original placeholder attribute value retrieved (e.g., 'Enter username') | placeholder attribute is not null | PASS |
| 5 | Sets new 'placeholder' attribute to 'Enter your user ID' using JavaScript | Input field's placeholder attribute updated in the DOM | - | PASS |
| 6 | Gets the updated 'placeholder' attribute of the input field | Updated placeholder attribute value retrieved | placeholder attribute equals 'Enter your user ID' | PASS |
| 7 | Assertion checks that placeholder attribute is updated correctly | Test verifies the attribute change | assertEquals("Enter your user ID", updatedPlaceholder) | PASS |
| 8 | Browser closes and test ends | Browser window closed | - | PASS |