Introduction to Selenium
Selenium is a free (open-source) automated testing suite for web applications, which supports cross-browser and cross-operating-system interoperability. It is quite similar to HP’s Quick Test Pro (QTP, now UFT), but Selenium focuses only on automating web-based applications. Testing done using the Selenium tool is usually referred to as Selenium testing.
It is useful for testing web applications only. Neither desktop (software) testing nor the testing of mobile applications is possible with Selenium.
Read more of this Selenium testing tutorial to get in-depth knowledge of automation testing.
Watch this Selenium Tutorial for Beginners
A web application is an application program stored on a remote server, which 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
Coming back, to test the performance of these web applications, companies carry out the testing process. The testing of web applications done through the Selenium tool is referred to as Selenium testing.
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 one of them briefly.
Selenium IDE (Integrated Development Environment) is a tool that helps you develop your test cases. It is 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 was contributed to the Selenium project in 2006.
It was introduced as a Firefox plugin for the faster creation of test cases. As it was a Firefox extension, it could automate the browser through a record-and-play feature providing auto-completion 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, such as Java, Ruby, Python, etc.
Selenium RC (Remote Control)
Then, a 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 (the library containing the programming language code)
RC can support the following programming languages:
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 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.
It was founded by Simon Stewart, a ThoughtWorks’ Consultant in Australia, in 2006. Selenium WebDriver was the first cross-platform testing framework that could 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 the browser.
This is implemented through a browser-specific driver, which sends commands to the browser and retrieves the results. Each driver launches and accesses a browser application. Different WebDrivers are:
- FirefoxDriver (GeckoDriver)
To extensively understand the working of WebDriver and its benefit when used with Selenium, watch the below Selenium WebDriver tutorial video:
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, namely, 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, such as 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 the Selenium suite.
Next, you will learn all that you need to know to get started with testing web apps using Selenium WebDriver. The below image depicts how WebDriver works:
What is Selenium WebDriver?
In this Selenium WebDriver tutorial, let’s dig deeper into 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.
WebDriver was introduced as part of Selenium v2.0. Selenium v1.0 consisted of only IDE, RC, and Grid. The major breakthrough happened in the Selenium project when WebDriver was developed and introduced as an addition to Selenium v2. With the release of Selenium v3, RC was deprecated and moved to a legacy package. Although you can still download WebDriver and carry out tasks with RC. There would not 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 over 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 automation testing tutorial for beginners will be about browser 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
- Radio buttons/checkboxes
- Text area/error messages
- Dropdown box/list box/combo box
- Web table/HTML table
- Frame, etc.
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 the 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. However, only after the elements are located on the web page, you can perform operations on them and start testing them.
Watch this video on Selenium Tutorial for Beginners:
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. Suppose 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, e.g., a group of elements can have the same value for ‘Class Name’.
There are eight attributes that 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 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?
Maven is a build automation tool used primarily for Java projects by downloading its dependencies.
Basically, Maven is a software that helps you download dependencies for a software program. When you create a Selenium project, you need to specify all Selenium components that are required to be included inside a POM file for the Selenium project to be ready. Once the dependencies are added to 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 the tutorial, let’s see how Eclipse projects run. For making Eclipse Java projects run, you need a library that can produce an HTML report of execution and display the test case that has failed. It is done by the TestNG library. When bugs are 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, you need to include the @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 will 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 a 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
Click on the radio button, Accept License Agreement, and download the .exe file related to your OS type
Step 2: Open the downloaded execution file, and click on Next
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 setup
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
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
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
Step 1: Go to the Eclipse download page (the recent one is Eclipse Installer 2019-06 R) through this link: https://www.eclipse.org/downloads/
Step 2: Click on Download 64 bit. You would land on a page as shown below:
Step 3: Once the download is finished, launch the installer
Step 4: Click on Eclipse IDE for Enterprise Java Developers
Step 5: Once done, click on INSTALL
Step 6: Then, click on Launch
Now that you have successfully set up Java and Eclipse in your environment, let’s perform a Selenium Test Case using Maven.
Performing a Selenium Test Case
Step 1: Start a new Maven Project
Step 2: Check the Create a simple project checkbox and click on Next
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 across an organization.
Artifact Id: Artifact Id is the Id of the project. It specifies the name of the project.
Step 4: Now, your project would appear in the Project Explorer section as shown below:
Before moving on to the scripting part, you need to configure the Maven dependencies to perform the 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
1. Right-click on the file name
2. Click on Properties
1. Select Java Build Path
2. Click on Libraries
3. Click on Add Library
1. Click on TestNG
2. Press Next, and then Finish
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
Step 11: Follow the process shown below and copy the XPath of the email:
- 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 the web elements of a web page, uniquely
4. Copy the RelXpath. XPath is defined as an XML path. It is a syntax or language for locating any element on the net page using XML path expression
1. Paste the xpath in the xpath expression (i.e., key)
2. Enter the value (email ID)
Step 13: Do the same for password xpath
Step 14: Do the same for login xpath as well
Step 15: Your code would look like this:
public class App
public void test()
driver = new ChromeDriver();
//The website name you want to visit
Note: Here, you must provide a valid email ID and password. In this example, a dummy ID and password would be used.
1. Right-click on the screen and scroll down to Run As
2. Click on 1 TestNG Test
1. Once done, the 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
Step 18: You’ll be navigated to your FB account
Step 19: Go to Eclipse IDE again and see the console. You would see the Test report with the results
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
Finding an Element on the Web Browser
An element on a web browser can be found using:
- 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 textboxes 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 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 will 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 Selenium 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 exciting packages. So, you must take the assistance from experts and get yourself qualified enough to make the best of these opportunities.
Are you interested? Check out Intellipaat’s Selenium Training – WebDriver Course and enroll now!