0
0
Selenium Javatesting~15 mins

Page title and URL retrieval in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify page title and URL after navigating to example.com
Preconditions (3)
Step 1: Open a new browser window using Selenium WebDriver
Step 2: Navigate to the URL 'https://www.example.com'
Step 3: Retrieve the page title
Step 4: Retrieve the current page URL
Step 5: Verify that the page title is exactly 'Example Domain'
Step 6: Verify that the current URL is exactly 'https://www.example.com/'
Step 7: Close the browser
✅ Expected Result: The page title matches 'Example Domain' and the URL matches 'https://www.example.com/'
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert that the page title equals 'Example Domain'
Assert that the current URL equals 'https://www.example.com/'
Best Practices:
Use explicit waits if needed (not required here as page is simple)
Use WebDriver's getTitle() and getCurrentUrl() methods
Close the browser in a finally block or use try-with-resources
Use meaningful assertion messages
Automated Solution
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.assertEquals;

public class PageTitleAndUrlTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        // Assuming chromedriver is in system PATH
        driver = new ChromeDriver();
    }

    @Test
    public void testPageTitleAndUrl() {
        driver.get("https://www.example.com");

        String actualTitle = driver.getTitle();
        String actualUrl = driver.getCurrentUrl();

        assertEquals("Example Domain", actualTitle, "Page title should be 'Example Domain'");
        assertEquals("https://www.example.com/", actualUrl, "URL should be 'https://www.example.com/'");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test class uses JUnit 5 and Selenium WebDriver with ChromeDriver.

setUp() method initializes the ChromeDriver before each test.

testPageTitleAndUrl() navigates to 'https://www.example.com', retrieves the page title and URL, then asserts both match the expected values with clear messages.

tearDown() closes the browser after the test to free resources.

This structure ensures the browser opens fresh for each test and closes properly, following best practices.

Common Mistakes - 4 Pitfalls
Not closing the browser after test execution
{'mistake': 'Using hardcoded waits like Thread.sleep() instead of relying on page load or explicit waits', 'why_bad': 'Slows tests unnecessarily and can cause flaky behavior if page loads faster or slower', 'correct_approach': "Use WebDriver's built-in page load wait or explicit waits when needed"}
Using incorrect assertion methods or comparing to wrong expected values
Not verifying the URL after navigation
Bonus Challenge

Now add data-driven testing with 3 different URLs and their expected titles

Show Hint