Installing Selenium Java on Eclipse (Windows, TestNG)

Installing Selenium Java on Eclipse (Windows, TestNG)

1. Downloading and Setting up Java Development Kit

If you already have java setup on your PC as a system variable and do not wish to update it, skip to the next section:

As this guide is going to use Java as its target language, you will first need to set up Java on your PC. For the rest of the guide we will refer to the development kit as JDK.

First, click here to download the latest version of JDK. You will need to check that the download corresponds with your OS (Mac, Linux or Windows). This guide assumes you are using windows, so select the x64 installer under the windows tab.



Next, open the file that you just downloaded. Follow each of the steps in the window that opens. Take note of the folder path you are installing the JDK into.

Once it is downloaded, open the windows start menu and look for "environment variables". Click Edit the system environment variables.



Check the list of values in the system variables section:

  • If there is no variable named JAVA_HOME, click the new button. For the variable name, enter "JAVA_HOME" and for the variable value, enter the path to the folder where you installed the JDK. E.g: C:\Program Files\Java\jdk-19.

  • If there is a variable named JAVA_HOME but it doesn't contain the version corresponding with the version you installed, double-click the "JAVA_HOME" variable. Update the variable value to point to the folder where you installed the JDK. E.g: C:\Program Files\Java\jdk-19. Click OK to confirm.

Double-click the path variable.



NOTE: DO NOT EDIT ANY OTHER VALUE IN THE SYSTEM PATH OTHER THAN THE ONE MENTIONED IN THIS GUIDE. IT CAN AFFECT OTHER FUNCTIONALITY ON YOUR COMPUTER.

If the folder path you entered as the value for JAVA_HOME is not present in the list of values, click new and paste in that folder path.

To check that this setup of Java was successful, open a cmd or powershell window and enter the command: java --version. If you are not shown a version of Java or if the version is not correct, go back and restart the process.



2. Installing Eclipse IDE

Click here to download the latest installer for Eclipse IDE.

Open the file you just downloaded and when it asks for what type of Eclipse installation you want, select Eclipse IDE for Java Developers.



Afterwards, you will be asked where you want eclipse installed, you can leave this as its default value unless you have somewhere specific you want it to be installed. In the same window you will also be asked which JDK you want to use if you have 2 or more JDKs downloaded. Select the latest version if so.



Next click launch and choose the workspace folder. This will be where your projects are stored.



Once the workspace folder has been chosen, click launch to open Eclipse.

3. Downloading Java Selenium Driver files

Click here and make sure you are under the section titled: Selenium Clients and WebDriver Language Bindings. Choose the Stable version under Java.



Extract the downloaded zip file to a location in your file system, the location doesn't matter as we will be adding them into eclipse later. Just make sure you can remember where you put them.

Click here to download the chromedriver that matches the version of chrome on your PC. To check the version of chrome you have installed, click the three small dots in the top-right of the Chrome window, then click Help > About Google Chrome.



Create a folder somewhere and store Chromedriver.exe there, note down the location for later.

4. Configuring Eclipse to use Selenium files

Launch Eclipse if it isn't already open, and choose your workspace file location.

Click File > New > Java Project to create a new project. Fill out the name and uncheck the box Create module-info.java file. Ignore the other settings and confirm creation.



Right-click the project you just created and click New > Package.



Then, once the package is created, right-click the package and click New > Class. This is where we will test your selenium installation. Put any future tests in this package.



Now right-click the project and select Properties. Search the Properties window for Java Build Path and select it from the side navigation options. Navigate to the Libraries section and, if it is present, left-click the Classpath folder.

Once Classpath is selected, click Add External Jars. In the opened file explorer, navigate to the folder you extracted the selenium libraries to in Section 3.



Select all the files in the folder and click Open, then do the same for the files in the sub-folder. Then click Apply and Close



From the navigation toolbar at the top of the eclipse window, click Help > Eclipse Marketplace....



In the Marketplace window search for "TestNG" and select install in the box titled: "TestNG for Eclipse".



Accept whatever you are prompted to during the installation process of TestNG. If you don't feel safe with accepting any of these prompts, look online to verify that this is a legitimate and safe plugin.

5. Testing the Selenium Setup

In the class you created in Section 4, copy in the following code. Replace "newpackage" and "NewClass" with the names you chose for the package and class respectively. Replace FILEPATH with the path to the chromedriver file.

package newpackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class NewClass {
    @Test
    public void test() {
        System.setProperty("webdriver.chrome.driver", "FILEPATH");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com");
        driver.quit();
    }
}

If you get an error in the code saying org.testng cannot be resolved, click the light bulb in margin and click "Add TestNG library".



Getting errors you can't seem to fix

If you received errors during this process and can't find an easy fix anywhere, you might be considering shortcuts to finish the process. If you just want to get to a point where you can start writing your own selenium code right away, use MaxTAF Cloud.

You won't receive any errors while setting up MaxTAF Cloud because it is all already set up for you. All you need is a valid email address, and you can start writing test cases for free. The only costs occur when you feel like you need advanced features, like running many tests in parallel, or an extended storage space for test reports and cases.

Leave a reply

Your email address will not be published. Required fields are marked*

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.