Selenium Tutorial – Learn Selenium from Experts
ITCourCes Selenium tutorial will help you perform your first automation test on Facebook’s login page by learning how elements are located and how automation testing is performed, to generate a test result report using TestNG. This Selenium tutorial will provide you the basic and advanced concepts of Selenium and hence it is designed for both beginners and professionals.
Our Selenium tutorial includes all the topics of Selenium like ‘Introduction to Selenium’, ‘Selenium Types’, ‘What is Selenium WebDriver?’, ‘What is Maven in Selenium?’, and so much more.
Introduction to Selenium
Selenium may be 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 typically mentioned as Selenium Testing.
Selenium is useful for testing web applications only. Neither desktop (software) testing nor the testing of mobile applications is feasible with Selenium.
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 an internet application.
Few examples of web applications are:
Coming back, the testing of web applications done through the Selenium tool is referred to as Selenium Testing.
But there surely has to be shortcomings right?
Before going any longer during this what’s Selenium blog, you need to know the story behind how Selenium came into being what it’s today. So, let’s understand the scenario in the below order:
Need For Software Testing
Software testing is where it all boils right down to. Today’s world of technology is totally dominated by machines, and their behaviour is controlled by the software powering it. Will the machines behave exactly as we would like them to? Every time? Everywhere? The answer to those questions dwells software testing.
At the top of the day, it’s the software application’s success rate which goes to regulate your business growth. The same thing is often said even for web applications because most businesses today are completely reliant on the web.
Take, for example, an e-commerce company. Be it Amazon or eBay or Flipkart, they rely on the customer traffic on their web sites and traffic on their web-based mobile applications for business.
Imagine, if something catastrophic happens just like the prices of a variety of products being capped off at 10$, all due to little bug during a “not so easily readable” part of the code. Then what can be done, and how can we prevent it the next time?
By testing the code before deployment right? So, that is the need for software testing. But, what is Selenium? Is it a software testing tool? Well, Selenium is an automation testing tool!
Before I go any longer, let me make you understand that Software testing is of two types: Manual Testing & Automation Testing. Selenium was founded as an automation testing tool to beat the drawbacks/ limitations of Manual testing. So, in the next section of this selenium blog, let’s understand the challenges with manual testing.
Challenges With Manual Testing
Manual testing means the (web) application is tested to execute the test cases without using any automation tools or manually tested by QA testers or. Tests got to be performed manually in every environment, employing a different data set and therefore the success/failure rate of each transaction should be recorded.
Look at the above image of an inferior chap, who manually confirms the transactions recorded. The challenges he’s facing cause fatigue, boredom, delay in work, mistakes and errors due to manual effort. This led to the invention of Selenium (automation testing tool).
Automation Testing Beats Manual Testing
Automation testing beats manual testing every time. Why? Because it’s faster, needs less investment in human resources, it’s not susceptible to errors, frequent execution of tests is feasible, supports lights out the execution, supports regression testing and also functional testing.
Let’s take a similar example to the one mentioned earlier. Suppose there’s a login page and that we got to verify if all the login attempts are successful, then it’ll be very easy to write down a bit of code which can validate if all the transaction/ login attempts are a hit or not (automated test suit execution).
Moreover, these tests can be configured in such a way that they are tested in different environments and web browsers. What else can be done? You can automate the generation of the result file, by scheduling it for a particular time during the day. Then you can also automate the generation of reports based on those results and whatnot.
The key point is that automation testing makes a tester’s job an entire lot simpler. Check out the image below which shows a more relaxed environment during which an equivalent tester is functioning.
.Now, let me talk about Selenium in particular.
Now allow us to see where Selenium stands within the market.
Selenium vs QTP vs RFT:
I have compared its performance with two other popular tools: QTP and RFT in the table below.
Dedicated HP support
Dedicated IBM support
Hardware consumption during script execution
Ample amount of coding skills and experience needed
Only for Windows
Only for Windows
Java and C#
It is pretty clear from the above table why Selenium is the most preferred tool. But there are many flavors in Selenium and you should know which is the appropriate right Selenium tool for your needs.
Selenium isn’t just one tool but a set of software, each catering to different testing needs of a corporation . It has four components:
Now that you know about its types, let’s talk about each of them briefly.
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 a superb 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.
Selenium RC (Remote Control)
Then, another ThoughtWorks’ engineer, Paul Hammant resolute to make a server which will act as an HTTP proxy to trick the browser into believing that Selenium Core and therefore the web application being tested belong to an equivalent domain, thus making RC a two-component tool.
RC can support the following programming languages:
Selenium Grid may be a testing tool that allows you to run your tests on various machines against different browsers. It is part of the Selenium retinue that specializes in running various tests over 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 a web Selenium Grid that you simply 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.
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 receives instruct (sent in Selenium or via a Client API) and addresses them to a browser.
This is applied through a browser-specific driver, which sends instructions to a browser and retrieves the results. Each driver launches and accesses a browser application. Different WebDrivers are:
Benefits of Selenium WebDriver
Cons of Selenium WebDriver
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, 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:
Before I get started with Selenium WebDriver, let me show you the topics I will be covering in this blog which will help you write your first Selenium code for automation testing. In this blog, I even have written Selenium code to check automated login to Facebook. The topics are:
Drawbacks Of Selenium RC and therefore the Birth Of WebDriver
Let’s first discuss the limitations of Selenium RC because that was the reason for the eventual development of WebDriver. You might be surprised when I say that Selenium RC became an instant hit when it was launched. That was because it overcame the same-origin policy issue which was a major problem while testing web apps with Selenium Core. But do you know what the same-origin policy issue was?
But when Selenium RC came into the image, it rid testers of the same-origin policy issue. But, how did RC do that? RC did that by working on other elements called Selenium RC server. So, RC may be a tool which may be a combination of two components: Selenium RC server and Selenium RC client.
Even though Selenium RC was a serious hit, it had its own share of problems. The major one being the time taken for executing tests. Since the Selenium RC server is that the middle man within the communication between the browser and your Selenium commands, test executions are very time-consuming. Besides the time factor, RC’s architecture is additionally slightly complicated.
To overcome these problems, the Selenium WebDriver was developed. WebDriver is quicker because it interacts directly with the browser and there’s no involvement of an external proxy server. The architecture is also simpler as the browser is controlled from the OS level. The below image will help you understand how WebDriver works.
Another benefit with WebDriver is that it supports testing on the HTMLUnit driver which is a headless driver. When we say a headless driver, it refers to the fact that the browser has no GUI. RC on the other hand does not support HTML Unit drivers. These are some of the reasons why WebDriver scores over RC.
What Is a Selenium WebDriver?
In this part of Selenium tutorial blog, I will dig deep into Selenium WebDriver. There is a good chance that you will be aware of the details I have covered in the below paragraph, but I will be revising it anyway.
WebDriver was introduced as part of Selenium v2.0. Selenium v1 subsist only IDE, RC and Grid. But the main breakthrough within the Selenium project was when WebDriver was developed and introduced as a replacement in Selenium v2. However, with the release of Selenium v3, RC has been deprecated and moved to a legacy package. You can download and work with RC but don’t predict any support for it.
In a nutshell, the benefits WebDriver has over RC are:
But the disadvantage in comparison to RC is that the test reports can’t be generated. RC generates detailed reports.
The below image depicts how WebDriver works:
You must have heard the term “browser elements” variety of times. The next a part of this Selenium tutorial is going to be about what are these elements and the way testing happens on these web elements.
What Are Browser Elements?
Elements are the different components that are present on web pages. The most common elements we notice while browsing is:
Testing these elements essentially means we have to check whether they are working fine and responding the way we want it to. For example, if we are testing text boxes, what would you test it for?
If we are testing an image, we might want to:
Similarly, operations can be performed on each of the elements mentioned earlier. But only after the elements are located on the web page, we can perform operations and start testing them right? So, subsequent topic, i will be able to be covered during this Selenium tutorial blog is element locator techniques.
Locating Browser Elements Present On The Web Page
Every element on an internet page will have attributes (properties). Elements can have quite one attribute and most of those attributes are going to be unique for various elements. For example, consider a page having two elements: a picture and a text box. Both these components have a ‘Name’ attribute and an ‘ID’ attribute. These attribute values got to be unique for every element. In other words, two elements cannot have an equivalent attribute value. Elements can have an equivalent value for ‘Class Name’.
In the example considered, the image and text box can neither have an equivalent ‘ID’ value nor an equivalent ‘Name’ value. However, there are some attributes that can be common for a group of elements on the page. I will tell you which of them are those attributes later, but before that permit me to list down the 8 attributes using which we will locate elements. Those features are ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS and XPath.
Since the weather are located using these attributes, we ask them as ‘Locators’. The locators are:
By watching the syntax above, you would possibly have realized locators are called inside methods. So, before going any further, you need to learn all the other methods, browser commands and functions that can be used to perform operations on the elements.
Operations On Browser Elements
From this section of the blog onwards, you will be having a lot of fun because there will be less theory and more codes. So be prepared, and keep your Eclipse IDE open with the specified Selenium packages installed.
To start testing an internet page, we’d like to first open a browser, then navigate to the online page by providing the URL right? Check out the below piece of code, where I have replicated the same. Firefox browser will first be initiated and then it will navigate to Facebook’s login page.
public class WebDriverClass
public static void main(String args)
WebDriver driver = new FirefoxDriver();
import org.openqa.selenium.WebDriver; may be a library package which contains the specified class to initiate the browser loaded with a selected driver.
import org.openqa.selenium.firefox.FirefoxDriver; may be a library package which contains the FirefoxDriver class needed to start out FirefoxDriver because the browser initiated by the WebDriver class.
System.setProperty(“webdriver.gecko.driver”, “files/geckodriver.exe”); – This command notifies the run-time engine that the Gecko driver is present within the specified path. After Firefox 35, we’d like to download the Gecko driver for working with WebDriver. In case you want to test on chrome, then you have to download ChromeDriver, which is a .exe file and specify it’s path in this line of code. We have to do it similarly in case of other browsers also.
WebDriver driver = new FirefoxDriver(); – This command is employed to initiate a replacement Firefox driver object.
driver.get(“https://itcources.com/”); – This method is used to open the specified URL.
driver.getTitle(); – This command gets the title of the tab that is currently open in the browser.
driver.quit(); – This command closes the browser driver.
But, what if you want to navigate to a different URL and then do testing? In that case you can use the navigate.to() command as shown in the below code snippet. If you then want to come back to the previous page, then you can do that by using navigate.back() command. Similarly for refreshing the present page, you’ll use navigate.refresh() command.
If you want to maximize the size of the browser window, then you can do that by using the code in the snippet below.
In case you want to set a custom size for the browser window, then you can set your own dimensions as shown in the below code snippet.
Dimension d = new Dimension(420,600);
Now that you know most of the basics, let’s go to the next topic in this Selenium tutorial blog. Let’s try to find an element on the web page and then perform any operation that is possible.
I’m pretty sure, you all have Facebook accounts. So, let me show you how to log into Facebook by passing the credentials from the code itself.
There are two text fields in the Facebook login page, one for Email/Phone and another for Password. We have to locate these two elements, pass the credentials to those elements and then find the third element: Login button which needs to be clicked on.
Look at the screenshot below. It is the screenshot of Facebook’s login page.
If you Inspect (Ctrl + Shift + i) this page, then you will get the same window in your browser. Then, under Elements, a list of all the weather present on the page and their attributes are going to be displayed. There are three portions highlighted in the above screenshot. The first highlighted element is an email text field, the second is the password text field and the third is the Login button.
If you’ll recall, I discussed earlier that these elements are often located using element locator techniques. Let’s use it to locate these elements and send the sector values.
This is the syntax for detecting the element: driver.findElement(By.id(“xxx”));
For sending it values, we can use the method sendKeys(“credentials“);
For clicking on a button, we have to use the method click();
So, let’s get started with finding the element and performing an operation on it. The code for it is in the below snippet.
In line #1, we are identifying the Email element by its unique ‘Name’ attribute and sending it the EmailID.
In line #2, we are identifying the Password element by its unique ‘Name’ attribute and sending it the password.
In line #3, we are locating the Login button element by its unique ID and clicking on that button.
Adding just these lines of code might not be enough. That is because of the dynamics of the page, it might not respond immediately and by the time the page loads, WebDriver will get terminated and throw a timeout exception error. This issue might not happen on Facebook’s page because it is fast but will most likely happen in any other E-Commerce site and other dynamic web sites.
To overcome this problem, we need to use an advanced technique. We need to request our WebDriver to wait after the page is accessed and after it loads completely, we need to locate the elements and then perform actions.
In case you want your WebDriver to wait until all the elements load in a web page and then close the browser, then we can achieve that by using a driver.wait() method or Threads.sleep() method. However, if you are writing more advanced code, then you should use Implicit waits or Explicit waits. In the next blog of this Selenium tutorial series, I will clarify the concept of wait conditions. But for our case, the below commands are enough.
// or use this:-
But, while working with wait conditions, remember to import this library:
We do it because the class to wait and its related methods will be present in this library.
The entire code I explained, is present in the below code snippet.
public class WebDriverClass
public static void main(String args)
WebDriver driver = new FirefoxDriver();
// or use
When you replace the credentials with your actual email and password and execute this code, then Facebook will open in a new window, enter your credentials and login to your account.
Voila! You have successfully logged in, which means your complete code executed completely.
I have used the ID and Name attributes for locating elements. You can use any locator for finding the elements. XPath is the most utility and foremost of locator techniques. But, as long as you can find even one of the attributes and use them for locating elements, you should be good. You can see the below video delivered by an industry expert, where she has shown all the above-mentioned features of Selenium WebDriver with hands-on.
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 that 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 the Selenium tutorial, let’s see how Eclipse projects run. For making Eclipse Java projects run, you need a library that gives the 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 a 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:
Now, this tutorial will move on with the hands-on part. This section is divided into three parts:
Installing Java JDK:
Step 1: Download Java JDK from the link provided below then click on the Oracle JDK Download button
Click on the radio button, Accept License Agreement, and download the .exe file associated with your OS type
Step 2: Open the downloaded execution file, and click on on Next
Step 3: Select Developers Tools and click on on Next, and you’ll be directed to the below screen. it’ll take a couple of moments to line up JDK in your system
Step 4: Click on on the brink of complete the found out
Before moving ahead, you would like to feature the environment variables to the trail as follows.
Step 5: attend local C drive > Program Files > Java > jdk-12.0.1 > bin
Step 6: Copy the trail as shown above
Step 7: attend instrument panel > System and Security > System > Advanced system settings > System Properties
Step 8: Click on Environment Variables as shown above and therefore the below window pops up during which you’ll perform the subsequent steps:
Keep clicking ‘OK’
Step 1: attend the Eclipse download page (the recent one is Eclipse Installer 2019-06 R), with this link: https://www.eclipse.org/downloads/
Step 2: Click on Download 64 bit. you’d 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 simply have successfully found out Java and Eclipse in your environment, let’s perform a Selenium test suit using Maven.
Performing Selenium test suit
Step 1: Start a replacement Maven Project
Step 2: Check the Create an easy project checkbox and click on on Next
Step 3: Enter Group Id and Artifact Id and click on on Finish
Group Id: Group Id is that the Id of the project’s group. Generally, it’s unique among a corporation .
Artifact Id: Artifact Id is that the Id of the project. It specifies the name of the project.
Step 4: Now, your project would seem within the Project Explorer section as shown below:
Before moving on to the scripting part, you would like to configure the Maven Dependencies to perform Selenium test suit in Eclipse. you’ll be adding the Maven Dependencies to the pom.xml file under the target folder.
Step 5: Now, plow 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 can be already present in your .xml file
Step 9: Click on the Apply and shut button
Step 10: attend www.facebook.com, right-click on the page, and click on on Inspect
Step 11: Follow the method shown below and replica the xpath of email:
Step 13: Do an equivalent for password xpath
Step 14: Do an equivalent for login xpath also
Step 15: Your code would appear as if this:
public class App
public void test()
driver = new ChromeDriver();
//The website name you would like to go to
Note: Here, you want to provide a legitimate email ID and password. during this example, dummy ID and password would be used.
Step 18: You’ll be navigated to your FB account
Step 19: attend Eclipse IDE again and see the console. you’d see the Test report with the results
That’s it! you’ve successfully performed a Selenium test suit .
Creating Automated Tests
There are three steps to execute automated tests:
Finding an Element on the Web Browser
An element on a web browser can be found using:
Performing an Action on That Element
The next step is performing an action. For that you can try the following options:
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:
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.