blog

Home / DeveloperSection / Blogs / Building our first Selenium Application

Building our first Selenium Application

Building our first Selenium Application

Sonali Desai724 29-Jul-2021

What is Selenium?

Selenium is a portable framework for web application testing that was created by Jason Huggins in 2004. It gives you the tools you need to write practical tests without having to learn a scripting language. It also includes the Selenese domain-specific language, which can be used to write tests in Scala, Ruby, Python, PHP, Perl, Java, Groovy, C#, and other common programming languages. Most modern web browsers, such as Google Chrome, Mozilla Firefox, Internet Explorer, Edge, Opera, Safari, and others, will run Selenium tests. It also works with a variety of operating systems, including Windows, Linux, and macOS. It is free and open-source software licensed under the Apache License 2.0.

There are four major components of Selenium discussed below:-

Selenium IDE: The Selenium Suite's key tool is the Selenium IDE. It is the simplest and most straightforward framework in the Selenium suite. It's available as a Chrome Extension and a Firefox Add-On. It's a web automation research platform that's free to use. Unlike Selenium WebDriver and RC, it does not require any programming logic to write test scripts.

Selenium RC: Selenium Remote Control (RC) is a Java-based server that accepts browser HTTP commands. In any programming language, RC assists us in writing automated tests for a web application.

Selenium WebDriver: Selenium Webdriver is a collection of APIs for testing web applications that are available for free. The Selenium Webdriver tool is used to automate web application testing to ensure that it functions properly.

Selenium Grid: Selenium Grid is a Selenium Suite component that allows you to run multiple tests simultaneously across multiple browsers, operating systems, and computers.

Our goal in this article:-

We are going to automate the login process in a simple website named “zero.webappsecurity.com”. We will be using Java as our programming language, Google Chrome browser as our browser, and Eclipse as our Integrated Development Environment (IDE). The goal is to make readers familiar with the process of writing automation scripts for websites and to get them started with Selenium.

Getting started up with the setup:-

First of all, we need to install the Eclipse IDE for writing our automation script. It can be downloaded from this link. Then, we need to download and unzip the Selenium server (Grid). This can be downloaded from this link.

Building our first Selenium Application

Then, we need to download and unzip Selenium Client and Webdriver Bindings for Java language from the same link.

Building our first Selenium Application

We need to download and unzip the ChromeDriver as well from this link.

Building our first Selenium Application

Getting started with the automation script:-

Open the Eclipse editor. There, create a new Java project from File => New Java Project. We will be naming our project Selenium.

Building our first Selenium Application

After that, we create a new package named SeleniumPackage inside this project.

Building our first Selenium Application

After that, we create a class named LoginScript.

Building our first Selenium Application

Next, we right click on the JRE System Library under Package Explorer and click on Build Path => Configure Build Path.

Building our first Selenium Application

Next, we head over to the Classpath and click on Add External Jars... We browse through our system and add our downloaded and unzipped Selenium Standalone Server Jar file to it. 

Building our first Selenium Application

We click on Apply and close now.

Now, we start with our script named LoginScript. We declare a main function inside the class. Inside the main function, the very first thing that we will do is to define the path to the chrome executable that we had downloaded earlier. We do it with the following line of code:-

System.setProperty('webdriver.chrome.driver', {Path to the executable file} );

Next, we create an instance of the ChromeDriver. This can be done from the following line of code:-

WebDriver driver = new ChromeDriver();

We see that there are errors in this line. The word WebDriver and ChromeDriver are underlined with red lines.

Building our first Selenium Application

This is because we need to import WebDriver and ChromeDriver into our script. This can be done by hovering over these words and selecting the import option. This includes the following pieces of code into our script:-

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

Next, we use the get( ) method of the driver object so as to open our required web page. This can be done with the following code:-

driver.get('http://zero.webappsecurity.com/');

We maximize the browser window by the following code:-

driver.manage().window().maximize();

We run the script until now to see where we are currently. We see the following output on our browser:-

Building our first Selenium Application

We write code to set a timeout for loading the web page. If the web page does not get loaded in this time frame, an exception will be thrown. The following code is used :-

driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

Now we write code to implicitly wait for twenty seconds. If the element is not found within 20 seconds, an exception will be thrown. This can be done by the following code:-

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

For this to work, we need to import the following:-

import java.util.concurrent.TimeUnit;

Now, we write code so that the sign-in button is clicked. We first inspected the web page and found that the sign-in button has an id of “signin_button”.

Building our first Selenium Application

The automation of clicking sign-in button is done by the following code:-

driver.findElement(By.id('signin_button')).click();

On running our script now, we see that we have successfully been redirected to the login page.

We again write code to set a timeout for loading the web page. If the web page does not get loaded in this time frame, an exception will be thrown. The following code is used:-

driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

Now we write code to implicitly wait for twenty seconds. If the element is not found within 20 seconds, an exception will be thrown. This can be done by the following code:-

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

Next, we inspect the input fields for the username and password. We find that they have ids “user_login” and “user_password” respectively. Similarly, they have names as “user_login” and “user_password” respectively.

Building our first Selenium Application

So, we write code to find elements with this name and then fill in the details. From the site, we can infer that the correct login credentials are username: username and password: password.

Building our first Selenium Application

We can fill in the login credentials using the following code:-

WebElement username_Input = driver.findElement(By.name('user_login'));

username_Input.sendKeys('username');

WebElement username_Password = driver.findElement(By.name('user_password'));

username_Password.sendKeys('password');

We need to import the following to run the above code :-

import org.openqa.selenium.WebElement;

Now we need to click the Sign in button, once the credentials have been filled in. We first inspect the Sign in button.

Building our first Selenium Application

The code for automating the clicking of Sign in button is:-

driver.findElement(By.name('submit')).click();

We now run the script once again. We get the following output:-

Building our first Selenium Application

We need to click on Advanced and then click on Proceed to zero.webappsecurity.com. We need to automate this process. First, we inspect the Advanced button.

Building our first Selenium Application

As we can see, it has an id of details-button. Similarly, we inspect the Proceed link.

Building our first Selenium Application

We can clearly see that it has an id “proceed-link”. Now we write code to automate the process of clicking advanced and then the proceed link. This is done by the following snippet :-

driver.findElement(By.id('details-button')).click();

driver.findElement(By.id('proceed-link')).click();

On running this script, we get to see the following page as the final page:-\

Building our first Selenium Application

So, we have successfully logged in. We can write more code to playaround with the website. For example, you can write code to automate the process of logging out from this website. For this, you need to first find the element having “username” as its name and then click on it. Then, you need to find the element having “Log out” as its name from the drop-down and click it.

The Complete Code :-

package SeleniumPackage;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginScript {
 public static void main(String args[])
 {
  System.setProperty('webdriver.chrome.driver', 'C:\\Users\\user\\Downloads\\selenium\\chromedriver_win32\\chromedriver.exe');
  WebDriver driver = new ChromeDriver();
  driver.get('http://zero.webappsecurity.com/');
  driver.manage().window().maximize();
  driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  driver.findElement(By.id('signin_button')).click();
  driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
  driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  WebElement username_Input = driver.findElement(By.name('user_login'));
  username_Input.sendKeys('username');
  WebElement username_Password = driver.findElement(By.name('user_password'));
  username_Password.sendKeys('password');
  driver.findElement(By.name('submit')).click();
  driver.findElement(By.id('details-button')).click();
  driver.findElement(By.id('proceed-link')).click();
 }
}

Conclusion:-

On the market, Selenium testers are in high demand. Since Selenium is becoming more common, the demand for Selenium testers is also growing. Testing repeated test scenarios on each and every new build takes almost no time at all with Selenium. As a consequence, the workload of the tester is reduced. It enables the tester to operate on several test scenarios at once. Due to machine overhead (performance) or lack of functionality, other methods do not have the luxury of parallel and distributed testing, which can be a major factor in persuading you to learn Selenium. In the sector, it is critical to complete tests quickly and efficiently.

In this article, we have shown how with an example on how to get started with using Selenium for automating web pages. It is only with practice that you can understand it better. Make sure to try your hands on some more websites and play around with them. This will strengthen your confidence in using Selenium. Remember Practice makes a man perfect. So, keep practicing and you will excel as an automation tester.


I am Freelance Technical Blogger and a Content Marketer. I spend my time dabbling in on a lot of things that help expand my learning horizons. Keep Learning is my Motto..!

Leave Comment

Comments

Liked By