Complete Guide: Setting Up Selenium with Java for Test Automation - Nytrotype/Selenium-with-Java-automation-work GitHub Wiki
Below is a step-by-step guide to install and run Selenium with Java on your system.
β
Download JDK: https://www.oracle.com/java/technologies/downloads/
β
Install and configure JAVA_HOME environment variable:
- Open System Properties β Advanced β Environment Variables.
- Add a new system variable:
JAVA_HOME = C:\Program Files\Java\jdk-XX
- Add Javaβs bin folder to the Path variable:
C:\Program Files\Java\jdk-XX\bin
πΉ IntelliJ IDEA (Recommended) β [Download IntelliJ IDEA](https://www.jetbrains.com/idea/download/)
πΉ VS Code β [Download VS Code](https://code.visualstudio.com/)
πΉ Eclipse IDE β [Download Eclipse](https://www.eclipse.org/downloads/)
β Download the Selenium Java Client Library:
- Visit https://www.selenium.dev/downloads/
- Extract the JAR files.
- Add them to the projectβs dependencies.
β Install WebDriver for your browser:
- ChromeDriver (for Chrome): https://chromedriver.chromium.org/downloads
- GeckoDriver (for Firefox): https://github.com/mozilla/geckodriver/releases
πΉ Place the downloaded chromedriver.exe or geckodriver.exe inside your project folder.
β
Install Maven: https://maven.apache.org/download.cgi
β
Verify installation by running:
mvn -version
β
Create a Maven Project (Skip if using a standard Java project).
β
Add Selenium dependencies to pom.xml
:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.x.x</version>
</dependency>
</dependencies>
Create a Java class and write a simple Selenium script:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumTest {
public static void main(String[] args) {
// Set path for ChromeDriver
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
// Launch Chrome
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
// Print page title
System.out.println("Page Title: " + driver.getTitle());
// Close browser
driver.quit();
}
}
β
Open a terminal in VS Code or IntelliJ IDEA (Ctrl + ~
)
β
Navigate to your Java project folder:
cd path/to/your/project
β Compile & Run:
javac SeleniumTest.java
java SeleniumTest
β If using Maven, run:
mvn test
β
Install TestNG in pom.xml
:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.x.x</version>
<scope>test</scope>
</dependency>
β
Create a TestNG.xml
file for structured test execution:
<suite name="Test Suite">
<test name="GoogleTest">
<classes>
<class name="SeleniumTest"/>
</classes>
</test>
</suite>
β Run TestNG:
mvn test