Basic Web Automation Using Selenium With Java

Introduction to Selenium and Selenium Webdriver

The Selenium web driver is a web-based, automation testing framework that can test webpages initiated on various web browsers. You can write test scripts in java, C#, Python, Ruby, etc.

As technology is evolving toward the digital era, testing software or an application is becoming a necessity rather than a requirement. Selenium is one of the latest trends in the market right now. It is an open-source tool used to automate the tests carried out on different web browsers.

Need of Selenium

  • Using Selenium, it is easier to automate testing across various web applications.
  • It supports testing scripts coded in C, C++, Java, Python, Ruby, Pearl, etc.
  • Test cases are easy to implement.
  • Remote Control Server[RCS] of Selenium supports a wide variety of Operating Systems.
  • It is open-source and easy to learn.

More Features of Selenium

  • Selenium can be considered a leading cloud-based testing platform.
  • It supports parallel execution.
  • It requires fewer resources as compared to other web automation tools.

Components of Selenium

  1. Selenium IDE 
    Selenium IDE (Integrated Development Environment) is primarily a record run tool that a test case developer uses to develop selenium test cases. It is a simple record and playback kind of tool, which comes as an addon for Firefox, Chrome, etc. It is used for prototype testing and test cases written in IDE and can be exported in many programming languages like Ruby, Java, etc. It is an excellent tool for beginners to learn and understand the basic syntax of Selenium web driver as well as the simplest framework in Selenium Suite.
  2. Selenium Remote Control Server
    It is the first-ever tool of the Selenium Suite. It was earlier known as JavaScript Executor, and only due to this tool did Selenium gets its popularity in the testing market. Using RC, you can also perform cross-browser testing. This tool provides features like the wide support to programming languages to Selenium.
  3. Selenium web driver
    Selenium web driver was the first-ever cross-platform testing framework that could control the browser from the Operating System Level. In contrast to IDEs, Selenium web driver provides a programming interface to create and execute test cases. It is an upgrade to Remote Control Server since it is much faster i.e., It makes direct calls with the browser and does not need an RC server between a client and a server. Selenium RC needs an RC Server to interact with the web browser which the Selenium web driver did not.
  4. Selenium Grid
    Selenium Grid was used with RCs to run test cases on remote machines. Using Grid, Multiple Test Scripts can be executed at the same time on different machines. Parallel Execution can be easily achieved using hub node architecture, and one machine will assume the role of a hub and other machines will become the nodes. It is used by companies but maintaining it is a tedious task.

Setting up Selenium Environment

  1. At first, you have to make sure that JDK is installed on your System. If not first you need to install it.
  2. Then you have to download Selenium Jar Files for Java. You can download it from here.
  3. Choose the latest stable version and proceed, you will get a file. Extract it somewhere on your computer.
  4. After that download the suitable web driver for your browser version. For Chrome it's chrome driver, for Firefox it's geckodriver.
  5. After that download and install Eclipse IDE for Java Developers from here.
  6. Now follow the steps to create a basic automation application using Selenium in Java.

Automating Login in Java using Selenium


Open Eclipse IDE, and create a new Project named "C#_Auto_Login".

Basic Web Automation using Selenium with Java


Right-click on your project name, select New, then select package, enter the package name as "com.c_sharp_auto.pkg". Click on Finish Button.

Basic Web Automation using Selenium with Java


Right-click on the newly created package and select Java Class. Name it as C_Sharp_Auto and click finish.


Your Class will be created as below,

Basic Web Automation using Selenium with Java


Now right click on the project name, select properties, and select Java Build Path.

Basic Web Automation using Selenium with Java


Select Java Build Path, then select classpath.


Click on add external jar files and locate your selenium jar files folder which you have extracted earlier and select all of them i.e, Both Inside and Outside lib folder, then click open.


Click on Apply and close button and you are ready to go.


On Coding Area/Workspace Area, Type the following code:

package com.c_sharp_auto.pkg; //user-defined package
import org.openqa.selenium.By; //By is a class which have methods that locate different
//elements on webpage.
import org.openqa.selenium.WebDriver; // WebDriver is a remote control interface that
//enables introspection and control of browsers
import; // ChromeDriver is a class which is used to handle
//chromedriver software
public class C_Sharp_Auto {
    public static void main(String args[]) { //main() method
        String Username = "[email protected]"; //Username
        String Password = "*********"; //Password
        String driverPath = "E:\\DEEPAK JAVA\\Required Tools//chromedriver.exe"; //Path to Chromedriver
        System.setProperty("", driverPath); // sets the system  property to
        //value named
        // and the
        //path is mentioned to get the
        //chrome driver
        WebDriver driver = new ChromeDriver();
        //Creating instance of Chrome Driver
        //Navigating to URL
        //Maximizing Browser Window
        System.out.println("Successfully Opened the website");
        //Locating Login WebElement by ID and clicking it using click() method
        //Locating Username Text Box and Filling UserID
        //Locating Username Text Box and Filling User Password
        //Locating  Login WebElement by ID and clicking it using click() method
        System.out.println("Successfully Logged in");
    } //end of main() method
} //end of class


Your code should look like this:

Basic Web Automation using Selenium with Java


Build and run your project, It will automatically:

  • Start the Chrome Browser.
  • Navigate to "
  • Click on Login Button
  • Enter the Credentials 
  • Click the Login Button and Finally open the User Dashboard.


Congratulations! You have developed your first automated Login System in Java using Selenium Driver. Every time you run this test script or Java Project, the Selenium web driver will automatically start your chrome browser and then it will navigate and login to and login with the specified credentials. You can write various test scripts for various automation projects using Selenium. Let us Summarize what we learned so far.

  • Selenium is a web-based automation testing tool.
  • It is used for testing various websites and web applications.
  • It is an open-source tool.
  • Its Test Scripts can be written in C, C++, Java, Python Ruby, etc., and many other languages.
  • It has four basic components namely, Selenium Grid, Selenium web driver, Selenium IDE, and Selenium Remote Control Server.
  • You can Automate Logins, Downloading, Web Searches, etc. using the Selenium Web driver.
  • For Google Chrome, Selenium provides chromedriver as its web driver.
  • For Mozilla Firefox, Selenium provides geckodriver as its web driver.
  • Before Using Selenium in Java, you have to add RequiredExternal Jar Files to your Java Project.
  • For locating different web elements, you have to use different locators provided by Selenium such as id, xpath, class, etc.

For more articles on Selenium Projects, let me know in the comments section.

Similar Articles