0
0
Selenium Javatesting~10 mins

First Selenium Java test - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a browser, navigates to a website, finds the page title element, and checks if the title text is correct.

Test Code - JUnit 5 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 FirstSeleniumTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        // Set path to chromedriver executable if needed
        driver = new ChromeDriver();
    }

    @Test
    public void testPageTitle() {
        driver.get("https://example.com");
        WebElement titleElement = driver.findElement(By.tagName("h1"));
        String titleText = titleElement.getText();
        assertEquals("Example Domain", titleText);
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and ChromeDriver instance is createdBrowser window opens, blank page-PASS
2Browser navigates to https://example.comBrowser shows Example Domain page with heading 'Example Domain'-PASS
3Find element by tag name 'h1'Element found with text 'Example Domain'-PASS
4Get text from the found elementText retrieved: 'Example Domain'-PASS
5Assert that the text equals 'Example Domain'Text matches expected valueassertEquals("Example Domain", titleText)PASS
6Close browser and quit driverBrowser window closes-PASS
Failure Scenario
Failing Condition: The element with tag name 'h1' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test check after opening the page?
AThe page URL contains 'example.com'
BThe text inside the <h1> element is 'Example Domain'
CThe browser window is maximized
DThe page background color is white
Key Result
Always verify that the element you want to test exists on the page before asserting its content. Use clear locators like tag name or id for reliable element finding.