Introduction to Selenium

Selenium is a free (open-source) automated testing suite for web applications that supports cross-browser and cross-operating-system interoperability. It is quite similar to HP’s QuickTest Pro (QTP, now UFT), only that Selenium focuses on automating web-based applications. Testing done using the Selenium tool is usually referred to as Selenium Testing.

Selenium is useful for testing web applications only. Neither desktop (software) testing nor the testing of mobile applications is possible with Selenium.

Watch this Selenium Tutorial

A web application is an application program stored on a remote server that is allowed to get accessed through a web browser over the Internet. Many websites contain web applications. Any website component that performs functions for users qualifies as a web application. Few examples of web applications are:

  • Google Search Engine
  • AliExpress

Coming back, the testing of web applications done through the Selenium tool is referred to as Selenium Testing.

Selenium Types

Selenium is not just a single tool but a suite of software, each catering to different testing needs of an organization. It has four components:

  • Selenium Integrated Development Environment (IDE)
  • Selenium Remote Control (RC)
  • Selenium Grid
  • Selenium WebDriver

Now that you know about its types, let’s talk about each of them briefly.

Selenium IDE

Selenium IDE (Integrated Development Environment) is a tool that helps you develop your Selenium test cases. It’s an easy-to-use Chrome and Firefox extension and is generally the most reliable method to develop test cases. It records users’ actions in the browser for you, using the existing Selenium commands, with parameters defined by the context of the web element. This is not only a time-saver but also an excellent way of learning Selenium script syntax. Previously known as Selenium Recorder, Selenium IDE was initially created by Shinya Kasatani, of Japan and contributed to the Selenium project in 2006.

It was introduced as a Firefox plugin for faster creation of test cases. As it was a Firefox extension, it could automate the browser through a record-and-play feature providing autocompletion support and the ability to move commands around quickly.

Scripts are recorded in a special test scripting language called Selenese for Selenium. Selenese comes up with commands for carrying out actions in a web browser and restoring data from the resulting pages.

The advantage of Selenium IDE is that the tests recorded via the plugin can be exported in different programming languages like Java, Ruby, Python, etc.

Selenium17

Selenium RC (Remote Control)

Let me first tell you that Selenium Core was the first version. But with that version, testers had to install both Selenium (a JavaScript program) and the web server containing the web application being tested on their local systems so that they would belong to the same domain.

Then, another ThoughtWorks’ engineer, Paul Hammant decided to create a server that will act as an HTTP proxy to trick the browser into believing that Selenium Core and the web application being tested belong to the same domain, thus making RC a two-component tool.

  • Selenium RC Server
  • Selenium RC Client (Library containing the programming language code)

RC can support the following programming languages:

  • Java
  • C#
  • PHP
  • Python
  • Perl
  • Ruby

Selenium2

Selenium Grid

Selenium Grid is a testing tool that lets you run your tests on various machines against different browsers. It is part of the Selenium suite that specializes in running multiple tests across different browsers, operating systems, and machines. You can connect to it with Selenium Remote Control by stating the browser version, browser, and operating system as per your choice. You will be able to specify these values through Selenium Remote Control capabilities. With Selenium Grid, one server makes a move as a hub. Tests communicate to the hub to get access to browser instances. The hub has a list of servers that provide access to browser instances (WebDriver nodes) and lets tests use these instances.

Selenium Grid allows parallel testing and also allows managing different browser versions and browser configurations centrally (instead of in each individual test).
There are multiple online platforms that provide an online Selenium Grid that you can access to run your Selenium automation scripts. For example, you can use LambdaTest.

Selenium Grid has more than 2,000 browser environments over which you can run your tests and truly automate cross-browser testing.

Selenium3

Selenium WebDriver

Founded by Simon Stewart in 2006, ThoughtWorks consultant in Australia. Selenium WebDriver was the first cross-platform testing framework that would control the browser at the OS level. Selenium WebDriver is a successor to Selenium RC. Selenium WebDriver accepts commands (sent in Selenium or via a Client API) and sends them to a browser.

This is implemented through a browser-specific driver, which sends commands to a browser and retrieves the results. Each driver launches and accesses a browser application. Different WebDrivers are:

  • Firefox Driver (Gecko Driver)
  • Chrome Driver
  • Internet Explorer Driver
  • MicroEdge
  • Opera Driver
  • Safari Driver
  • HTML Unit Driver

Benefits of Selenium WebDriver

  • Selenium WebDriver supports seven programming languages: Java, C#, PHP, Ruby, Perl, Python, and .Net.
  • It supports cross-browser interoperability that helps you perform testing on various browsers like Firefox, Chrome, IE, Safari, etc.
  • Tests can be performed on different operating systems: Windows, Mac, Linux, Android, and iOS.
  • Selenium WebDriver overcomes limitations of Selenium v1 like file upload, download, pop-ups, and dialog barrier

Cons of Selenium WebDriver

  • Detailed test reports cannot be generated.
  • Testing images is not possible.

No matter what, these shortcomings can be overcome by integrations with other frameworks. That is, for testing images Sikuli can be used, and for generating detailed test reports, TestNG can be used.

Now, you know what Selenium is and have a fair idea about the various tools of Selenium suite.

Next, I will tell you everything you need to know to get started with testing web apps using Selenium WebDriver. The below image depicts how WebDriver works:

Selenium4

What is Selenium WebDriver?

In this part, let’s dig deeper into learning Selenium WebDriver. Let’s understand more about what Selenium WebDriver is, what browser elements are, and how to locate browser elements on a web page.

Locating and testing of web elements in the web application is implemented through a browser-specific driver. It controls the browser by directly communicating with it.
In Selenium WebDriver, you have the liberty to write test scripts in different programming languages like Java, Perl, Python, Ruby, C#, PHP, and JavaScript. But, make a note that Mozilla Firefox is Selenium WebDriver’s default browser.

WebDriver was introduced as part of Selenium v2.0. Selenium v1.0 consisted of only IDE, RC, and Grid. But the major breakthrough in the Selenium project was when WebDriver was developed and introduced as an addition in Selenium v2. And, with the release of Selenium v3, RC has been deprecated and moved to a legacy package. Although you can still download webdriver and carry out tasks with RC, there wouldn’t be any support for it in Selenium v3.

In a nutshell, the advantages WebDriver has over RC are:

  • Support for more programming languages, operating systems, and web browsers
  • Ability to overcome the limitations of Selenium v1
  • Simpler commands when compared to RC, and a better API
  • Support for batch testing, cross-browser testing, and data-driven testing

The drawback WebDriver has when compared to RC is that test reports cannot be generated in WebDriver; whereas, RC generates detailed reports.
You must have heard the term ‘browser elements’ a number of times. The next part of this Selenium tutorial will be about these elements, and you will see how testing happens on these web elements.

What are Browser Elements?

Browser elements are different components present on web pages. The most common elements you will notice while browsing are:

  • Text boxes
  • CTA buttons
  • Images
  • Hyperlinks
  • Radio buttons/Checkboxes
  • Text area/Error messages
  • Dropdown box/List box/Combo box
  • Web table/HTML table
  • Frame

Testing these elements essentially means, you have to check whether they are working fine and responding the way you want them to. For example, if you are testing a text box, what would you test it for?

1. Whether you are able to send text or numbers to the text box
2. If you can retrieve text that has been passed to the text box, etc.

If you are testing an image, you might want to:

  • Download the image
  • Upload the image
  • Click on the image link
  • Retrieve the image title, etc.

Similarly, operations can be performed on each of the elements mentioned earlier. But only after the elements are located on the web page, you can perform operations on them and start testing them.

Locating Browser Elements Present on a Web Page

Each element on a web page does have an attribute (property). Elements can have multiple attributes and most of these attributes will be distinctive for different elements. Consider an example, there is a page having two elements: an image and a text box. Both these elements have a ‘Name’ attribute and an ‘ID’ attribute. These attribute values need to be distinctive for each of these elements. In other words, no two elements can have the same attribute value.

In the above example, the image and the text box can have neither the same ‘ID’ nor the same ‘Name’ value. However, there are some attributes that can be common for a group of elements on the page, like a group of elements can have the same value for ‘Class Name’.

There are eight attributes which can be used to locate elements on a web page, they are ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS, and XPath. Since the elements are located using these attributes, they are referred to as ‘Locators’. The locators are:

  • By.id
    driver.findElement(By.id(“xxx”));
  • By.name
    driver.findElement(By.name(“xxx”));
  • By.className
    driver.findElement(By.className(“xxx”));
  • By.tagName
    driver.findElement(By.tagName(“xxx”));
  • By.linkText
    driver.findElement(By.linkText(“xxx”));
  • By.partialLinkText
    driver.findElement(By.partialLinkText(“xxx”));
  • By.css
    driver.findElement(By.css(“xxx”));
  • By.xpath
    driver.findElement(By.xpath(“xxx”));

By looking at the syntax above, you might have realized why locators are called inside methods.

Now, you need to learn all the other methods, browser commands, and functions that can be used to perform operations on the elements.
But before moving on with the hands-on, creating an automated test, let’s first understand what dependencies are and how they help you in creating a Maven project.

You will need certain dependencies and libraries ready with you for the Selenium project which will help you perform automated testing of a web application and such a tool is known as Maven.

What is Maven in Selenium?

Maven is a build automation tool used primarily for Java projects by downloading its dependencies.

Basically, Maven is a software which helps you download dependencies for a software program. When you create a Selenium project, you need to specify all Selenium components which are required to be included inside a POM file for the Selenium project to be ready. Once the dependencies are added in the POM file, you can simply save the project and all these dependencies will automatically be downloaded.

Setting up Selenium with Maven and TestNG on Eclipse

Before diving into this section of Selenium tutorial, let’s see how Eclipse projects run. For making Eclipse Java projects run, you need a library that gives ability to produce an HTML report of execution and display the test case that has failed, which is done by TestNG library. When bugs can be accurately located like this, they can be fixed immediately to the relief of developers.

TestNG is a testing framework. It structures, groups, and launches tests. It also generates testing reports. To get a function executed, we need to include @Test annotation before the definition of that function.

When you run this file as TestNG suite, the execution will start and you will get the detailed test reports. You will get the test output in your console tab and the result of the test suite in the next tab.

You decided to use TestNG for several reasons:
1. TestNG annotations are easy-to-create test cases.
2. Test cases can be grouped and prioritized more easily.
3. TestNG supports parameterization.
4. It also supports data-driven testing using data providers.
5. It generates HTML reports.
6. Parallel test execution is possible.
7. TestNG readily supports integration with other tools and plugins like Eclipse IDE and build tools like ANT, Maven, etc.

Now, this tutorial will move on with the hands-on part. This section is divided into three parts:

  • Java JDK Installation
  • Eclipse Installation
  • Performing Selenium Test Case

Installing Java JDK

Step 1: Download Java JDK from the link provided below and then click on the Oracle JDK Download button
https://www.oracle.com/technetwork/java/javase/downloads/index.html


Click on the radio button, Accept License Agreement, and download the .exe file related to your OS type

Selenium8
Step 2: Open the downloaded execution file, and click on Next

Selenium9
Step 3: Select Developers Tools and click on Next, and you will be directed to the below screen. It will take a few moments to set up JDK in your system


Step 4: Click on Close to complete the set up

Selenium11
Before moving ahead, you need to add the environment variables to the path as follows.

Step 5: Go to local C drive > Program Files > Java > jdk-12.0.1 > bin

Selenium12
Step 6: Copy the path as shown above
Step 7: Go to Control Panel > System and Security > System > Advanced system settings > System Properties


Step 8: Click on Environment Variables as shown above and the below window pops up in which you will perform the following steps:

1. Click on Path in the System variables section, under Environment Variables
2. Click on Edit

Selenium14
Step 9:
1. Click on Browse and find your file path as local C drive > Program Files > Java > jdk-12.0.1
Keep clicking ‘OK’
2. Now, you have Java JDK installed in your system
 

Installing Eclipse

Step 1: Go to the Eclipse download page (the recent one is Eclipse Installer 2019-06 R), with this link: https://www.eclipse.org/downloads/

Selenium16
 Step 2: Click on Download 64 bit. You would land on a page as shown below:

Selenium18
Step 3: Once the download is finished, launch the installer
Step 4: Click on Eclipse IDE for Enterprise Java Developers

Selenium19
Step 5: Once done, click on INSTALL

Selenium20
Step 6: Then, click on Launch

Selenium21
Now that you have successfully set up Java and Eclipse in your environment, let’s perform a Selenium Test Case using Maven.

Performing Selenium Test Case

Step 1: Start a new Maven Project


Step 2: Check the Create a simple project checkbox and click on Next

Selenium24
Step 3: Enter Group Id and Artifact Id and click on Finish
Group Id: Group Id is the Id of the project’s group. Generally, it is unique among an organization.
Artifact Id: Artifact Id is the Id of the project. It specifies the name of the project.

Selenium25
Step 4: Now, your project would appear in the Project Explorer section as shown below:

Selenium26
Before moving on to the scripting part, you need to configure the Maven Dependencies to perform Selenium Test Case in Eclipse. You will be adding the Maven Dependencies to the pom.xml file under the target folder.
Step 5: Now, go ahead and add the below dependencies to the pom.xml file
Note: Copy the code from below and paste it after the junit dependency which will be already present in your .xml file

 <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
    </dependency>
    
    <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.141.59</version>
    </dependency>
  
    <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>3.141.59</version>
    </dependency>
    <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.141.59</version>
    </dependency>
 

 Selenium27
Step 6:
1. Right-click on the file name
2. Click on Properties

Selenium28
Step 7:
1. Select Java Build Path
2. Click on Libraries
3. Click on Add Library
 Selenium29
Step 8:
1. Click on TestNG
2. Press Next and then Finish

Selenium30
Step 9: Click on the Apply and Close button
Step 10: Go to www.facebook.com, right-click on the page, and click on Inspect

Selenium31
Step 11: Follow the process shown below and copy the xpath of email:

1. You will use this arrow to select an element in the page to inspect it
2. Select the email web-element, which will point to the section of this web-element’s html code
3. Click on Chropath, this helps you in getting Xpaths and CSS selectors for web elements of a web page, uniquely
4. Copy the RelXpath. XPath is defined as XML path. It is a syntax or language for locating any element on the net page using XML path expression

Selenium32
Step 12:
1. Paste the xpath in the xpath expression (i.e., key)
2. Enter the value (email ID)

Selenium33
Step 13: Do the same for password xpath

Selenium34
Step 14: Do the same for login xpath as well

Selenium35
Step 15: Your code would look like this:

@Test
public class App
    
{           
 WebDriver driver;
            
            public void test()
            {
            System.setProperty("webdriver.chrome.driver","C:\\Users\\intellipaat\\Desktop\\chromedriver.exe");
      
      
                          driver = new ChromeDriver();
                         //The website name you want to visit
                         driver.get("http://www.facebook.com");
      driver.findElement(By.xpath("//input[@id='email']")).sendKeys("helloworld@gmail.com");
      driver.findElement(By.xpath("//input[@id='pass']")).sendKeys("helloworld123");
                         driver.findElement(By.xpath("//input[@id='u_0_2']")).click();
              }
}
 

 

Note: Here, you must provide a valid email ID and password. In this example, dummy ID and password would be used.

Selenium36
Step 16:
1. Right-click on the screen and scroll down to Run As
2. Click on 1 TestNG Test


Step 17:
1. Once done, Chrome browser window will pop up
2. It will search for facebook.com
3. Then, it will automatically type the email
4. It will automatically type the password as well
5. It will then click on Log in and navigate you to your FB homepage

Selenium37
Step 18: You’ll be navigated to your FB account
Selenium38
Step 19: Go to Eclipse IDE again and see the console. You would see the Test report with the results

Selenium39
That’s it! You have successfully performed a Selenium Test Case.

Creating Automated Tests

There are three steps to execute automated tests:

  • Find an element on the web browser
  • Perform an action on that element
  • Test and create a test report with the results

Selenium40

Finding an Element on the Web Browser

An element on a web browser can be found using:

  • ID
  • Name
  • Class Name
  • Tag Name

Performing an Action on That Element

The next step is performing an action. For that you can try the following options:

  • Click(): Used to click on a web element
  • sendKey(): Used to take values into text boxes
  • Clear(): Used to clear text boxes of its current value
  • Submit(): WebDriver will automatically trigger the submit function of the web application where that element belongs to

Testing and Reporting

Report generation is extremely vital once you do the Automation Testing, likewise as for Manual Testing. By looking at the result, you can easily identify how many test cases are passed, failed, and skipped. By viewing the report, you’ll come back to understand what the status of the project is.
Selenium Internet Driver is employed for automating the web-application, however, it will not generate any reports. TestNG will generate the default report.

Selenium Career Opportunities

As numerous companies have started using web applications, the need for Selenium is shooting up. It has grown tremendously and has become one of the most integral parts of various businesses. When it comes to web testing tools, Selenium has surely become one of the best in the industry to help you with automation testing services.
There are various career opportunities for a tester. Below mentioned are some of the popular job roles you can consider to work as a Selenium WebDriver professional:

  • Automation Test Lead
  • Senior Test Engineer
  • Quality Engineer
  • Selenium Automation Analyst
  • QA Engineer

The most interesting part here is that the industry has experienced a humongous surge in creating job opportunities with very exciting packages. So, you must take the assistance of experts and get yourself qualified enough to make the best of these opportunities.

Interested? Check out Intellipaat’s Selenium Testing Training – WebDriver Course and enroll now!

Table of Contents

What is Selenium?

Selenium Overview

Selenium is a free (open-source) automatic testing suite for web applications across completely different browsers and platforms. It is quite the same as HP QuickTest Pro (QTP, currently UFT); however, Selenium focuses on automating web-based applications. Testing done using Selenium tool is usually referred to as Selenium Testing. Remember that, testing of only web applications is possible with Read More

Installation – Selenium Tutorial

Selenium Environment Setup

Installing Selenium requires going through following steps: Step 1 : Install Java Before moving further download and install Java Software Development Kit (JDK) in the first place. The location from where you can download Java is: http://www.oracle.com/technetwork/java/javase/downloads/index.html Watch this Selenium Tutorial [videothumb class="col-md-12" id="SxrtXHQ-rd0" alt="Selenium Tutorial" title="Selenium Tutorial"] Step 2 : Install Eclipse IDE Next, download and Read More

Selenium IDE

How to Download & Install Selenium IDE for Firefox

In this chapter you will get to know more about Selenium IDE. Read below: Selenium IDE (Integrated Development Environment) is a plug-in for Mozilla Firefox web browser for running Selenium scripts. It allows the software testing professionals to control the tests, such as record, edit, play, debug, etc. It consists of Read More

Recording of a Test

Steps to record Selenium test

In order to record a test, follow the given steps: Step 1 : Open Mozilla Firefox web browser, go to tools and click on Selenium IDE. There type the link http://store.demoqa.com  Now start the recoding procedure and initiate the test case. Step 2 : After entering the link, you will land on a page Step Read More

Selenium Cheat Sheet

Selenium User Handbook

Are you looking for a quick reference to turn to every time you need to look up basic operations of selenium? Well if you are then this is just what you need. We at Intellipaat understand that it could be a bit of a hassle to remember every aspect of a technology and that is why we Read More

Features of Selenium

Let us know what is so special about Selenium:

Combination of tool and DSL - Selenium is an absolute combination of tools and DSL (Domain Specific Language) in order to carry out various types of tests. It allows you to record the tests carried out through the browser. It supports multiple web browsers like Internet Explorer, Safari, Firefox, Chrome, etc. Read More

Next

Recommended Videos

2 thoughts on “Selenium Tutorial – Learn Selenium from Experts”

Leave a Reply

Your email address will not be published. Required fields are marked *