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 selenium.zip 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

STEP 1

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

Basic Web Automation using Selenium with Java

STEP 2

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

STEP 3

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

STEP 4

Your Class will be created as below,

Basic Web Automation using Selenium with Java

STEP 5

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

Basic Web Automation using Selenium with Java

STEP 6

Select Java Build Path, then select classpath.

STEP 7

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.

STEP 8

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

STEP 9

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 org.openqa.selenium.chrome.ChromeDriver; // 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("webdriver.chrome.driver", driverPath); // sets the system  property to
        //value named
        //webdriver.chrome.driver and the
        //path is mentioned to get the
        //chrome driver
        WebDriver driver = new ChromeDriver();
        //Creating instance of Chrome Driver
        driver.get("https://www.c-sharpcorner.com/");
        //Navigating to URL
        driver.manage().window().maximize();
        //Maximizing Browser Window
        System.out.println("Successfully Opened the website");
        driver.findElement(By.id("loginBtn")).click();
        //Locating Login WebElement by ID and clicking it using click() method
        driver.findElement(By.id("TextBoxUserId")).sendKeys(Username);
        //Locating Username Text Box and Filling UserID
        driver.findElement(By.id("TextBoxPassword")).sendKeys(Password);
        //Locating Username Text Box and Filling User Password
        driver.findElement(By.xpath("//*[@id=\"ctl00_ContentMain_ButtonSignIN\"]")).click();
        //Locating  Login WebElement by ID and clicking it using click() method
        System.out.println("Successfully Logged in");
    } //end of main() method
} //end of class

STEP 10

Your code should look like this:

Basic Web Automation using Selenium with Java

STEP 11

Build and run your project, It will automatically:

  • Start the Chrome Browser.
  • Navigate to "https://www.c#corner.com
  • Click on Login Button
  • Enter the Credentials 
  • Click the Login Button and Finally open the User Dashboard.

Summary

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 https://www.csharpcorner.com 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.