How to Install Selenium on Visual Studio Code (Windows, JUnit)

How to Install Selenium on Visual Studio Code (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 VS Code IDE

Click here to find the download link for VS Code. Click the Windows button to download the installer.



Open the installer, accept the license agreement and click Next >.



In the following window, choose a location for VS Code in your files and click Next >.



In the next screen, choose whether you want to create a start menu shortcut for VS Code and if so, what folder you want it to go into. Click Next >.



The next window contains multiple settings that you should only check or uncheck if you know what they do. When you have decided on which ones to enable or disable, click Next >.



Click Install to finish the configuration and install VS Code.



When the installation is complete, check the box to "Launch Visual Studio Code" and click Finish.




3. Downloading Java Selenium Driver files

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. Setting up VS Code to use Selenium Java

Open the IntelliJ window and click View > Extensions.



In the extensions explorer, search for Extension Pack for Java and click Install the first result row.



Once the extension has been installed, go to the project explorer and click Create Java Project.



Click Maven in the following list.



Click maven-archetype-quickstart.



Click the latest version available.



Give the project a group ID (can be left as the default value if you don't know what to put here) and press ENTER.



Give the project an artifact ID (if you don't know what it is, leave it as the default value) and press ENTER.



Choose a location for the project in your folders.

During the project creation, you will be prompted in the terminal section to "Define value for property 'version' 1.0-SNAPSHOT:". Press ENTER here to leave it at its default value and then press ENTER again to confirm.

Open the created project when prompted to do so.

Click here and select the latest version of Selenium Java.



Copy the contents of the "Maven" tab.



Go back to the IntelliJ window and open the project's pom.xml file. Remove the line <scope>test</scope> from the "junit" dependency. Then paste the previously copied text below said dependency.



In the following dialog, click Yes to synchronise the project with the new pom file changes.




5. Testing the Selenium Setup

Right-click the test\java\com\example folder in your project folder (where com\example is your group ID with backslashes instead of dots) and click New File.



Provide the class with a name ending with ".java" and press ENTER.

In the script for this class, copy in the following code. Replace "NewClass" and "com.example" with the names of your class and GroupId respectively. Replace FILEPATH with the path to the chromedriver file.

package com.example;

import org.junit.After;
import org.junit.Test;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

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

    @After
    public void afterTest() {
        driver.quit();
    }
}

Click the green play button next to your test method to run the test case.



If the installation was successful, you should see a window open and close briefly and after that, you should see the following success indicator in the explorer.




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.