We use WebDriver setup to control the Chrome browser automatically for testing websites. It helps us check if web pages work as expected without clicking manually.
WebDriver setup (ChromeDriver) in Selenium Java
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver();
Replace path/to/chromedriver with the actual location of your ChromeDriver file.
This code starts a new Chrome browser window controlled by your test script.
System.setProperty("webdriver.chrome.driver", "C:/drivers/chromedriver.exe"); WebDriver driver = new ChromeDriver();
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver"); WebDriver driver = new ChromeDriver();
This program opens Chrome, goes to example.com, prints the page title, then closes the browser.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ChromeDriverSetup { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:/drivers/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } }
Make sure the ChromeDriver version matches your installed Chrome browser version.
Close the browser with driver.quit() to free system resources.
Use absolute paths for the ChromeDriver location to avoid errors.
WebDriver setup lets your test code control Chrome browser automatically.
Set the ChromeDriver path before creating a ChromeDriver object.
Always close the browser after tests to keep your computer clean.
Practice
Solution
Step 1: Understand WebDriver role
WebDriver controls browsers to simulate user actions in tests.Step 2: Identify ChromeDriver's function
ChromeDriver is the specific driver that controls the Chrome browser.Final Answer:
To control the Chrome browser automatically during tests -> Option AQuick Check:
ChromeDriver controls Chrome browser [OK]
- Confusing ChromeDriver with test case writing
- Thinking ChromeDriver creates reports
- Mixing browser control with database tasks
Solution
Step 1: Recall Java system property syntax
Setting ChromeDriver path uses System.setProperty with key "webdriver.chrome.driver".Step 2: Match correct method and parameters
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe"); correctly uses System.setProperty with the driver path string.Final Answer:
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe"); -> Option AQuick Check:
Use System.setProperty for driver path [OK]
- Using non-existent methods like setPath or setChromeDriver
- Assigning path directly to WebDriver variable
- Missing quotes or wrong method names
System.setProperty("webdriver.chrome.driver", "/drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.quit();Solution
Step 1: Analyze driver setup and navigation
System.setProperty sets the driver path correctly, ChromeDriver instance is created, and driver.get opens the URL.Step 2: Check browser closing command
driver.quit() closes the browser after navigation.Final Answer:
The Chrome browser opens example.com and then closes -> Option CQuick Check:
Correct setup opens and closes browser [OK]
- Assuming syntax error without checking code
- Thinking driver.get does not open URL
- Forgetting driver.quit closes browser
WebDriver driver = new ChromeDriver();
System.setProperty("webdriver.chrome.driver", "/drivers/chromedriver");
driver.get("https://example.com");Solution
Step 1: Check order of setting driver path
System.setProperty must be called before creating ChromeDriver to locate the driver executable.Step 2: Identify consequence of wrong order
Creating ChromeDriver before setting path causes runtime error as driver is not found.Final Answer:
System.setProperty must be called before creating ChromeDriver instance -> Option BQuick Check:
Set driver path before ChromeDriver creation [OK]
- Calling driver.get before setting driver path
- Using non-existent driver.setPath method
- Ignoring order of setup steps
Solution
Step 1: Understand parallel test isolation
Each test should have its own ChromeDriver instance to avoid interference.Step 2: Importance of quitting driver
Calling driver.quit() after each test frees resources and closes browser properly.Final Answer:
Create a new ChromeDriver instance for each test and call driver.quit() after each test -> Option DQuick Check:
Separate driver per test + quit to avoid conflicts [OK]
- Sharing one driver instance across tests
- Not quitting driver causing resource leaks
- Relying on manual process killing
Ready to prove it?
Pass the test and this pattern moves unknown
