0
0
Selenium Javatesting~10 mins

Getting text and attributes in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit with Selenium WebDriver
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.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();
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to URL 'https://example.com/buttonpage'Browser displays the page with a button having id 'submit-btn'-PASS
3Finds the button element by id 'submit-btn'Button element located on the page-PASS
4Retrieves visible text from the button elementButton text is read as 'Submit'Verify button text equals 'Submit'PASS
5Retrieves 'type' attribute value from the button elementButton attribute 'type' read as 'button'Verify button attribute 'type' equals 'button'PASS
6Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Button element with id 'submit-btn' is not found or text/attribute values do not match expected
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after finding the button element?
AThe visible text and the 'type' attribute of the button
BOnly the button's CSS styles
CThe button's position on the page
DThe button's click functionality
Key Result
Always verify both visible text and relevant attributes of elements to ensure the UI shows correct content and properties.