Selenium Tutorial

Selenium is a valuable testing tool, a resource that all web application developers should know about. To that end, we present this tutorial to help you get familiarized with this versatile and powerful development resource.

But before we explore Selenium, let’s first address the need for automation testing and how Selenium came into the picture in the first place. 

Manual testing, a vital part of the application development process, unfortunately, has many shortcomings, chief of them being that the process is monotonous and repetitive. To overcome these obstacles, Jason Huggins, an engineer at Thoughtworks, decided to automate the testing process. He developed a JavaScript program called the JavaScriptTestRunner that automated web application testing. This program was renamed Selenium in 2004. 

Master important testing concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver with Selenium Certification Training course. Enroll now!

Selenium is an open-source, automated testing tool used to test web applications across various browsers. Unfortunately, Selenium can only test web applications, which leaves desktop and mobile apps out in the cold. However, tools like Appium and HP’s QTP, among others, can be used to test software and mobile applications. 

selenium-1

Selenium consists of a set of tools that facilitate the testing process. 

selenium-2
Fig: Selenium suite

Selenium IDE 

Shinya Kasatani developed Selenium Integrated Development Environment (IDE) in 2006. Conventionally, it is an easy-to-use interface that records the user interactions to build automated test scripts. It is a Firefox or Chrome plugin, generally used as a prototyping tool. It was mainly developed to speed up the creation of automation scripts. 

IDE ceased to exist in August 2017 when Firefox upgraded to the new Firefox 55 version, which no longer supported Selenium IDE. Applitools rewrote the old Selenium IDE and released a new version recently. The latest version came with several advancements, such as:

  • Reusability of test scripts 
  • Debugging test scripts
  • Selenium side runner
  • Provision for control flow statements
  • Improved locator functionality

selenium-3.

Now let’s take a closer, more detailed look at Selenium IDE. 

Installing IDE: 

Step 1- Open the Firefox browser 

Step 2- Click on the menu in the top right corner 

Step 3- Click on Add-ons in the drop-down box.

Step 4- Click on Find more add-ons and type “Selenium IDE” 

Step 5- Click on Add to Firefox

selenium-4

Once installed, the Selenium IDE icon appears on the top right corner of the browser. Once you click on it, a welcome message appears.


Selenium4

Recording a Test

Let’s first create a new test for a new project. Provide a name for your project, in this example, we will call it demo_project. Before recording, we must specify a valid URL. The recording begins once the browser navigates to this URL. Let’s navigate to the Facebook login page. 

Selenium6 

Clicking on “Start Recording” will send you to the Facebook page and start recording the user interactions. The user is at liberty to stop recording. All user actions are recorded and converted into a script. 

Save Your Work 

To save everything you've just done in the IDE, click the save icon in the top-right corner of the IDE. It will prompt you to for a name and a location of where to save the project. The result is a single file with a.side extension. 

Selenium7

Playback 

In-browser: You can play tests back in the IDE by selecting the test you wish to play and clicking on the Play button. 

For cross-browser playback, you can make use of the command-line-runner. 

Selenium8

Selenium Remote Control (RC) 

Paul Hammant developed Selenium Remote Control. But before learning about RC, let’s explore why it came into the picture in the first place. 

Initially, Selenium-Core was called "JavaScriptTestRunner," a tool built by Jason Huggins in 2004. It was a set of JavaScript functions that interpreted and executed Selenese commands using the browser's built-in JavaScript interpreter. Selenium-Core was then injected into the web browser. 

Now, let’s consider a JavaScript, test.js used by google.com. This program can access pages like google.com/mail or google.com/login within the google.com domain.

Selenium9
Fig: Selenium RC

However, the program cannot access elements of other domains like Yahoo.com. Local copies of Selenium-Core and the web browser had to be installed so that they belonged to the same domain. This is called the Same Origin Policy, and Selenium RC was introduced to address this limitation. The server acts as a client configured HTTP proxy and "tricks" the browser into believing that Selenium Core and the web application being tested share the same origin. 

Hence, Selenium RC is a server written in Java that makes provision for writing application tests in various programming languages like Java, C#, Perl, PHP, Python, etc. The RC server accepts commands from the user program and passes them to the browser as Selenium-Core JavaScript commands. 

Selenium10

Selenium WebDriver 

Developed by Simon Stewart in 2006, Selenium WebDriver was the first cross-platform testing framework that could configure and control the browsers on the OS level. It served as a programming interface to create and run test cases. 

Selenium8

    Fig: Selenium WebDriver

Unlike Selenium RC, WebDriver does not require a core engine like RC and interacts natively with the browser applications. WebDriver also supports various programming languages like Python, Ruby, PHP, and Perl. It can also be integrated with frameworks like TestNG and JUnit for test management. 

Selenium WebDriver’s architecture is simple and easy to understand: 

selenium-architecture

Fig: Selenium WebDriver architecture

  • Selenium test script - Selenium test script is the test code written in any of the mentioned programming languages that are interpreted by the driver. 
  • JSON Wire Protocol - JSON Wire Protocol provides a transport mechanism to transfer data between a server and a client. JSON Wire Protocol serves as an industry standard for various web services.
  • Browser drivers - Selenium uses drivers, specific to each browser to establish a secure connection with the browser.
  • Browsers - Selenium WebDriver supports various web browsers on which to test and run applications. 

Selenium WebDriver tutorial -

  1. Download and Install Java 8 or higher version - Install the latest version of the Java development kit. Click here to install it. 
  2. Download and configure Eclipse or any Java IDE of your choice - Open the URL link.

selenium-11

Scroll down the page and click on the latest version in the “more downloads” section.

The downloaded file will be a zip file. Unzip the file in a folder of your choice. Once unzipped, open the .exe eclipse file

selenium-12

The next step is to configure a workspace. Select a directory where you want to store all of your projects and click on the launch icon. 

selenium13

Once launched, the IDE workbench looks something like this.

seleniun-14

3. Download Selenium WebDriver Java Client

  • Navigate to the official Selenium page
  • Scroll down through the web page and locate Selenium Client and WebDriver Language Bindings.
  • Click on the "Download" link of Java Client Driver, as shown in the image. 

selenium-15

Once downloaded, unzip the file in a directory. It consists of the Jar files required to configure Selenium WebDriver in the IDE. 

selenium-16

4. Download the Browser driver - The automation scripts must be compatible with any browser. Every browser supported by Selenium comes with its driver files. These are essential to run the scripts. Moving ahead, download the latest driver file from this link.

selenium-17.

5. Configure Selenium WebDriver - The final step is to configure the Selenium WebDriver with the Eclipse IDE. In simple terms, we create a new Java project to build our test script.

selenium-18

Provide a project name and select the JRE that you wish to use. It is advisable to use the default JRE. Select it and click on finish. 

selenium-19

The next and most crucial step is to add the downloaded Java executable files [Step 3]. To do that, right-click on the Project>>Build Path>>Configure Build Path.

selenium-21

Select libraries and then Add External JARs.

selenium-20.

Open the folders in which you’ve saved your JAR files and select the two executable JAR files. Click on open to add them. 

selenium-22

Click on the libs folder>> Select the files>> Open 

selenium-23

Once you’re done adding the library files, click on Apply and Close. 

apply

With this, you have successfully configured the Webdriver with the Eclipse IDE. You can now go ahead and build your first test script. 

To do that, Right-click on Src folder>>New>>Class

new-class

Let’s now create a simple test script that launches the Firefox browser and opens the Facebook homepage. The script is provided below. 

import

Note: The second argument to the setProperty method is the location of your browser driver. In our case, we have installed the gecko driver. Hence, we paste the path along with the name of the file. 

Click on Run>>Run As>>Java Application

Run as - Java application

Selenium Grid 

Patrick Lightbody developed grid to minimize test execution time. Selenium Grid allows the parallel execution of tests on different browsers and different operating systems, facilitating parallel execution. Grid is exceptionally flexible and is integrated with other suite components for simultaneous performance. 

grid

Fig: Selenium Grid

The Grid consists of a hub connected to several nodes. It receives the test to be executed along with information about the operating system and browser to be run on. The Grid then picks a node that conforms to the requirements (browser and platform) and passes the test to that node. The node now runs the browser and executes the Selenium commands within it. 

Conclusion 

Hopefully, this article has helped you gain insights into Selenium and its workings. If you would like more information, watch our videos to get a better understanding. 

If you’re interested in launching your career as an automation engineer, then you should earn a course certification. Check out Simplilearn’s Selenium Training course and hone your Selenium skills into a robust and marketable resource!

About the Author

Chinmayee DeshpandeChinmayee Deshpande

Chinmayee is a Research Analyst and a passionate writer. Being a technology enthusiast, her thorough knowledge about the subject helps her develop structured content and deliver accordingly.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.