Setup ChromeDriver and open Google homepage
Preconditions (3)
✅ Expected Result: Chrome browser opens, navigates to Google homepage, page title is 'Google', and browser closes without errors
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class ChromeDriverSetupTest { private WebDriver driver; @BeforeEach public void setUp() { // Set the path to chromedriver executable System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @Test public void openGoogleHomePage() { driver.get("https://www.google.com"); String title = driver.getTitle(); Assertions.assertEquals("Google", title, "Page title should be 'Google'"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
The setUp method sets the system property to tell Selenium where the ChromeDriver executable is located, then creates a new ChromeDriver instance.
The test method openGoogleHomePage navigates to Google and asserts the page title is exactly 'Google'.
The tearDown method ensures the browser closes after the test to free resources.
This structure uses JUnit 5 annotations for setup and cleanup, which is a best practice for test automation.
Modify the test to use WebDriverManager to setup ChromeDriver automatically instead of setting system property manually.
System.setProperty("webdriver.chrome.driver", "/drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.quit();WebDriver driver = new ChromeDriver();
System.setProperty("webdriver.chrome.driver", "/drivers/chromedriver");
driver.get("https://example.com");Pass the test and this pattern moves unknown