Selenium dependency configuration 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.*; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class SeleniumDependencyConfigTest { private WebDriver driver; @BeforeEach public void setUp() { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); } @Test public void testOpenExampleDotCom() { assertNotNull(driver, "WebDriver should be initialized"); driver.get("https://example.com"); String currentUrl = driver.getCurrentUrl(); assertEquals("https://example.com/", currentUrl, "URL should be https://example.com/"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
The @BeforeEach method sets up the WebDriver using WebDriverManager, which downloads and configures the ChromeDriver automatically. This avoids manual driver setup.
The test method testOpenExampleDotCom verifies the WebDriver instance is created and navigates to https://example.com. It asserts the current URL matches the expected URL with a trailing slash, which browsers add automatically.
The @AfterEach method closes the browser to clean up resources after the test.
This setup ensures dependencies are correctly configured and Selenium WebDriver works as expected in a Maven Java project.
Now add data-driven testing to open three different URLs and verify each loads correctly
Practice
Solution
Step 1: Understand Selenium dependencies
Selenium dependencies provide the necessary libraries to control browsers automatically in Java projects.Step 2: Purpose of adding dependencies
Adding these dependencies allows your project to use Selenium features for automated testing.Final Answer:
To include Selenium libraries so you can write automated browser tests -> Option AQuick Check:
Selenium dependencies = automated browser tests [OK]
- Thinking dependencies improve runtime speed
- Confusing dependencies with UI design tools
- Assuming dependencies compile code
pom.xml?Solution
Step 1: Identify the latest stable Selenium Java dependency
The latest stable Selenium Java binding is version 4.10.0 with artifactId 'selenium-java'.Step 2: Check the correct groupId and artifactId
The groupId must be 'org.seleniumhq.selenium' and artifactId 'selenium-java' for Java bindings.Final Answer:
Correct Maven dependency with version 4.10.0 and artifactId selenium-java -> Option BQuick Check:
Maven dependency for Selenium Java = org.seleniumhq.selenium selenium-java 4.10.0 [OK]
- Using old Selenium versions
- Wrong artifactId like selenium-server or selenium-webdriver
- Incorrect version numbers
dependencies {
testImplementation 'org.seleniumhq.selenium:selenium-java:4.10.0'
}Solution
Step 1: Understand Gradle configuration scope
Using 'testImplementation' means dependencies are available only for compiling and running test code, not main code.Step 2: Effect on Selenium libraries
Selenium Java libraries will be accessible during test execution but not included in the main application runtime.Final Answer:
Selenium Java libraries available only for test code compilation and execution -> Option DQuick Check:
testImplementation = test-only Selenium libs [OK]
- Assuming Selenium is included in main runtime
- Thinking project build will fail
- Believing Selenium is ignored during tests
pom.xml but your test code shows errors like package org.openqa.selenium does not exist. What is the most likely cause?Solution
Step 1: Check dependency installation process
After adding dependencies, Maven needs to download and link them by running 'mvn clean install' or refreshing the project.Step 2: Understand error cause
The error means Selenium classes are not found because dependencies are not downloaded or linked yet.Final Answer:
Forgot to run Maven build or refresh dependencies -> Option CQuick Check:
Missing package error = missing dependency download [OK]
- Blaming Java version without checking dependencies
- Assuming package location causes error
- Thinking Selenium can't be used with Maven
Solution
Step 1: Identify correct versions and scopes
Selenium 4.10.0 and WebDriverManager 5.3.2 are latest stable versions. Use 'implementation' to include these libraries in the main runtime since WebDriverManager manages drivers at runtime.Step 2: Understand dependency scopes
'implementation' includes dependencies in both main runtime and test code, which is necessary for WebDriverManager to function properly.Final Answer:
implementation 'org.seleniumhq.selenium:selenium-java:4.10.0' and implementation 'io.github.bonigarcia:webdrivermanager:5.3.2' -> Option AQuick Check:
Gradle implementation deps = selenium-java + webdrivermanager included in runtime [OK]
- Using old versions
- Using testImplementation which excludes runtime usage
- Using compileOnly which excludes runtime
Ready to prove it?
Pass the test and this pattern moves unknown
