0
0
Selenium Javatesting~15 mins

Opening URLs (driver.get) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Open a web page using Selenium WebDriver
Preconditions (3)
Step 1: Launch the Chrome browser using Selenium WebDriver
Step 2: Navigate to the URL 'https://example.com' using driver.get()
Step 3: Verify that the current URL of the browser matches 'https://example.com/'
✅ Expected Result: The browser opens and loads the page at 'https://example.com'. The current URL matches exactly 'https://example.com/'.
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the current URL after navigation matches the expected URL
Best Practices:
Use WebDriverManager or set system property for driver executable
Use explicit waits if needed (not required here since get() waits for page load)
Close the browser after test execution
Use try-finally or @After method to ensure browser closes
Automated Solution
Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class OpenUrlTest {
    public static void main(String[] args) {
        // Set system property for ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();
        try {
            // Open the URL
            driver.get("https://example.com");

            // Verify the current URL
            String currentUrl = driver.getCurrentUrl();
            assertEquals("https://example.com/", currentUrl, "URL should match the expected URL");

            System.out.println("Test Passed: URL opened correctly.");
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

This code sets up the ChromeDriver path so Selenium can control the browser. It creates a new Chrome browser instance, then uses driver.get() to open the URL https://example.com. After the page loads, it checks the current URL with driver.getCurrentUrl() and compares it to the expected URL using assertEquals. Finally, it closes the browser with driver.quit() inside a finally block to ensure the browser closes even if the test fails.

This approach is simple and clear, perfect for beginners to understand how to open a URL and verify it using Selenium WebDriver in Java.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not setting the system property or driver path correctly', 'why_bad': 'Selenium cannot find the browser driver executable, so the test fails to start the browser.', 'correct_approach': "Set the system property 'webdriver.chrome.driver' to the correct path of the ChromeDriver executable before creating the driver instance."}
Using driver.navigate().to() instead of driver.get() without understanding difference
Not closing the browser after test execution
Hardcoding incorrect URL without trailing slash and failing assertion
Bonus Challenge

Now add data-driven testing to open three different URLs and verify each one

Show Hint