Java environment setup (JDK, IDE) in Selenium Java - Build an Automation Script
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; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class EnvironmentSetupTest { private WebDriver driver; @BeforeEach public void setUp() { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); } @Test public void testOpenExampleDotCom() { driver.get("https://example.com"); String title = driver.getTitle(); assertEquals("Example Domain", title, "Page title should be 'Example Domain'"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test class EnvironmentSetupTest uses JUnit 5 and Selenium WebDriver to verify the Java environment setup.
setUp() method runs before each test. It uses WebDriverManager to automatically download and configure the ChromeDriver, then creates a new Chrome browser instance.
testOpenExampleDotCom() opens the URL 'https://example.com' and asserts the page title is exactly 'Example Domain'. This confirms Selenium and Java are working correctly.
tearDown() closes the browser after the test to clean up resources.
This structure follows best practices: using WebDriverManager avoids manual driver setup, JUnit 5 annotations organize setup and cleanup, and the assertion verifies the test outcome clearly.
Now add data-driven testing with 3 different URLs and verify their page titles
Practice
Solution
Step 1: Understand JDK role
The JDK provides tools to compile and run Java code, essential for Selenium tests written in Java.Step 2: Eliminate unrelated options
Writing HTML, managing databases, and designing UI are not functions of JDK.Final Answer:
To compile and run Java programs -> Option AQuick Check:
JDK = Compile and run Java [OK]
- Confusing JDK with IDE
- Thinking JDK manages databases
- Assuming JDK designs UI
Solution
Step 1: Identify correct JAVA_HOME path
JAVA_HOME should point to the root folder of the JDK installation, not the JRE or bin folder.Step 2: Verify incorrect options
Setting JAVA_HOME to JRE, bin, or Documents folder is incorrect and causes errors.Final Answer:
Set JAVA_HOME to the JDK installation folder path -> Option BQuick Check:
JAVA_HOME = JDK root folder [OK]
- Setting JAVA_HOME to JRE folder
- Including bin folder in JAVA_HOME
- Pointing JAVA_HOME to unrelated folders
System.out.println(System.getProperty("java.version"));Solution
Step 1: Understand System.getProperty("java.version")
This command prints the Java runtime version installed and used by the IDE.Step 2: Check other options
No error occurs if JDK is set correctly; Selenium version is unrelated; program won't hang.Final Answer:
The installed Java version number, e.g., 17.0.5 -> Option AQuick Check:
System property java.version = Java version [OK]
- Expecting Selenium version output
- Thinking it causes error if setup is correct
- Assuming program hangs
Solution
Step 1: Analyze error meaning
"Cannot find symbol" usually means Java compiler can't find classes, often due to wrong JDK setup.Step 2: Check other options
Selenium missing won't cause basic Java errors; internet or script syntax unrelated to this error.Final Answer:
JAVA_HOME is set incorrectly or IDE is not configured to use it -> Option CQuick Check:
Compiler errors = JDK setup issue [OK]
- Blaming Selenium for Java compile errors
- Ignoring JAVA_HOME setup
- Assuming internet affects compilation
Solution
Step 1: Identify correct installation order
First install JDK, then set JAVA_HOME so system knows JDK location.Step 2: Configure IDE and add Selenium
Configure IDE to use JDK, then add Selenium libraries to project for testing.Final Answer:
Install JDK -> Set JAVA_HOME -> Configure IDE to use JDK -> Add Selenium libraries to project -> Option DQuick Check:
JDK -> JAVA_HOME -> IDE config -> Selenium libs [OK]
- Installing Selenium before JDK
- Setting JAVA_HOME after IDE config
- Skipping Selenium library addition
Ready to prove it?
Pass the test and this pattern moves unknown
