0
0
Selenium Javatesting~10 mins

Page title and URL retrieval in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, retrieves its title and URL, and verifies they match expected values.

Test Code - JUnit
Selenium Java
import org.openqa.selenium.WebDriver;
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.*;

public class PageTitleUrlTest {
    private WebDriver driver;

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

    @Test
    public void testPageTitleAndUrl() {
        driver.get("https://example.com");
        String title = driver.getTitle();
        String url = driver.getCurrentUrl();

        assertEquals("Example Domain", title);
        assertEquals("https://example.com/", url);
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://example.comBrowser displays the Example Domain page-PASS
3Retrieves page title using driver.getTitle()Title retrieved: 'Example Domain'Check if title equals 'Example Domain'PASS
4Retrieves current URL using driver.getCurrentUrl()URL retrieved: 'https://example.com/'Check if URL equals 'https://example.com/'PASS
5Assertions for title and URL passTest confirms title and URL are correctassertEquals for title and URLPASS
6Browser closes and test endsBrowser window closed-PASS
Failure Scenario
Failing Condition: Page title or URL does not match expected values
Execution Trace Quiz - 3 Questions
Test your understanding
What method retrieves the current page URL in Selenium WebDriver?
Adriver.getUrl()
Bdriver.getPageUrl()
Cdriver.getCurrentUrl()
Ddriver.getLocation()
Key Result
Always verify both page title and URL to ensure the correct page loaded before continuing with further tests.