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.


1. Install Java Development Kit (JDK)

βœ… 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
    

2. Install an IDE

πŸ”Ή 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/)


3. Install Selenium WebDriver

βœ… Download the Selenium Java Client Library:

βœ… Install WebDriver for your browser:

πŸ”Ή Place the downloaded chromedriver.exe or geckodriver.exe inside your project folder.


4. Set Up Maven for Dependency Management

βœ… 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>

5. Write a Basic Selenium Java Test

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();
    }
}

6. Running the Test

βœ… 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

7. Optional: Setting Up TestNG for Better Test Management

βœ… 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

You're All Set! πŸš€

⚠️ **GitHub.com Fallback** ⚠️