Set up Selenium and Java on Visual Studio

 Setting up selenium on Visual Studio is similar to setting up on Eclipse editor. However, to run the java program, we need to import certain libraries. We need to follow a few more steps to get the Selenium/WebDriver code running on the VS Code. Installation is easy and simple.

Setting up VS for Java.

To create a Java project, write code and run the code, we need to install Java extension Pack to the Visual Studio.  To install it from VS marketplace, click the link  here.  This will redirect you to the following page. Click on the Install button. 

Download ChromeDriver

To interact with the Chrome Browser, you should have latest ChromeDriver on your system and it should be discoverable by WebDriver. Also, the ChromeDriver major version has to match with your Chrome browser version. I suggest  to use latest Chrome browser and corresponding ChromeDriver.  Please note that Chrome Browser and ChromeDriver both are different. Download latest ChromeDriver from here

Base on your operating system, select either binary file or EXE file. Once downloaded, unzip it in the common folder that you can use across multiple project. Next, you download ChromeDriver should be discoverable by WebDriver.On Linux or macOS, you can use the following command to make the filediscoverable. 

export PATH=$PATH:~/<yourFolder>/drivers/bin/   

here <yourFolder>/drivers/bin/  is a folder path where you have downloaded and unzipped ChromDriver. 

There is another way to make this binary or EXE file discoverable by WebDriver. You should set the system property in your code. Add the path to ChromeDriver in your test code:

System.setProperty(“webdriver.chrome.driver”, “<yourFolder>/drivers/bin/”);

Create new standalone Java project:

Open Command Pallet in VS Code (Ctrl / Cmd + Shift + P).

In next Step, Select No Builds option, Select Project Location or Workspace and enter project name.  This will create a Java Project in the given folder. The New Project will have lib and src folder along with a sample java program that will print “Hello World” in the terminal.

Click on the run button available in the top right corner. 

So far, so good. You can create and run java program.  Now let’s set up Selenium/WebDriver. 

Setup Selenium WebDriver

Download the latest stable Selenium Web Driver client and Java language bindings. 

Now in VS Editor go the left side bottom and click on the + sign near Referenced Library under Java Section. See the following screenshot. Select Selenium server jasr file and import it. Now let’s import jar files for the java client.  Click on + sign and import all jar files from the Selenium-Java-4.8.1 folder. Again import all files from the   lib file under Selenium-Java-4.8.1 folder. 

Congratulations!!!!  You completed the set of Visual Studio for the Selenium with Java Programming language. 

Open Google.com using Selenium

Now let’s modify App.java file available under src folder. First we need to import WebDriver and then Chrome Driver class.

Import WebDriver and ChromeDriver. 

Adding the following two lines at the top of the App.java program.

Import org.openqa.selenium.WebDriver;

Import org.openqa.selenium.chrome.ChromeDriver;

Now inside main function, add code to open browser and get the URL. 

That’s It. 

Hope you like this article. Please do not forget to rate it.

Comments

Popular posts from this blog

Selenium: File download handling.

Major Differences between Python and Java

Cypress V/S Selenium