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