Installing Selenium Java on Eclipse (Windows, JUnit)

Installing Selenium Java on Eclipse (Windows, JUnit)

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. Installing JUnit

Click here and click Download and install.



Click junit.jar.



From the following page, click the download arrow in the row with the latest version and select jar.



Go to the previous page and click hamcrest-core.jar.



Click Downloads and then click jar.



Find an appropriate location (you can use the root C: folder for simplicity's sake) and create a new folder called "JUnit".

Move both of the jar files you just downloaded into the "JUnit" folder.

Press the windows button and search for Environment variables. Click Edit the system environment variables.



Click Environment Variables.



Click New.... For the Variable name, enter JUNIT_HOME and for the Variable value, enter the path to the "JUnit" folder. Click OK to finish creating the variable.



Now click New... again. For the Variable name, enter CLASSPATH and for the Variable value, enter the following: %CLASSPATH%;%JUNIT_HOME%\JUnit4.10.jar;.;. Click OK to finish creating the variable.



5. 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 JUnit folder you created in section 4 and click open. Then click Apply.



Click Add External Jars again and go to the folder where you stored the Selenium files in section 3. Click open, then click Add External Jars again and go to the subfolder within the selenium folder and click open again. Click Apply and Close when finished.




6. 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.junit.Test;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

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

Right-click the code in the class and click Run as > JUnit Test



If the installation was successful, you should see a JUnit tab open showing that one run was executed with 0 failures or errors.



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.