Test Overview
This test opens a web page, finds a button element, retrieves its visible text and the value of its 'type' attribute, and verifies both values are as expected.
This test opens a web page, finds a button element, retrieves its visible text and the value of its 'type' attribute, and verifies both values are as expected.
import org.openqa.selenium.By; 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 GetTextAndAttributeTest { WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); } @Test public void testButtonTextAndTypeAttribute() { driver.get("https://example.com/buttonpage"); WebElement button = driver.findElement(By.id("submit-btn")); String buttonText = button.getText(); String buttonType = button.getAttribute("type"); assertEquals("Submit", buttonText); assertEquals("button", buttonType); } @AfterEach public void tearDown() { 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 URL 'https://example.com/buttonpage' | Browser displays the page with a button having id 'submit-btn' | - | PASS |
| 3 | Finds the button element by id 'submit-btn' | Button element located on the page | - | PASS |
| 4 | Retrieves visible text from the button element | Button text is read as 'Submit' | Verify button text equals 'Submit' | PASS |
| 5 | Retrieves 'type' attribute value from the button element | Button attribute 'type' read as 'button' | Verify button attribute 'type' equals 'button' | PASS |
| 6 | Test ends and browser closes | Browser window closed | - | PASS |