Selenium is the primary thing that strikes to mind when we plan to automate web application testing. Here, in this Selenium tutorial blog, we’ll cover all Selenium concepts from basic to advanced in detail with an easy-to-understand approach. This tutorial is specifically designed for beginners with little or no prior knowledge of Selenium.
Before getting started, let’s see the list of topics covered in this blog, to get started with Selenium.
In this Selenium tutorial, we will start from the basics of Selenium and learn all the major Selenium concepts that a Selenium professional must be aware of. Now, let’s have a look at the components of this tutorial.
Selenium is one of the most popular Open Source Web UI (User Interface) automation testing suites, developed by Jason Huggins in 2004 as an internal tool at Thought Works. This automated testing framework validates web applications across multiple browsers and platforms. Various programming languages like Java, Python, PHP, Ruby, C#, etc., are used for creating Selenium Test Scripts and deployed on platforms like Windows, Linux, or Mac.
Selenium only tests web applications; we can neither test any mobile application or desktop application using Selenium. Also, note that Mozilla Firefox is Selenium WebDriver’s default browser. This automation testing framework integrates with other automation test tools such as Jenkins, Docker, Maven, etc., for achieving continuous testing. Also, it integrates with JUnit and TestNG to manage test cases and generate reports.
If you want to enrich your career and become a professional in Selenium, then enroll in "Selenium Training" - This course will help you to achieve excellence in this domain. |
Following are the reasons that made Selenium such a widely adopted tool:
Selenium installation is a 3 step process.
Install Java 8 or higher version
Install Eclipse IDE
Install and Configure Selenium WebDriver.
1. Install Java: Follow the below steps to complete Java installation.
2. Install Eclipse: Follow the below steps to install and configure Eclipse.
Download Eclipse Installer from http://www.eclipse.org/downloads
Specify the folder where you want Eclipse to get installed. The default folder will be in your User directory. Select the 'Install' button to begin the installation.
Step 4: After installation, click on launch.
Now, this will start eclipse IDE for you.
3) Install and configure Selenium WebDriver
Launch the "eclipse.exe" file inside the "eclipse" folder that we extracted while installing Eclipse. If you installed Eclipse correctly, the executable should locate on C:eclipseeclipse.exe. Eclipse stores your projects in a folder called a workspace.
Just accept the default location when asked to select a workspace.
Selenium is not just a single tool but a suite of software, each caters to a specific role in aiding the development of web application test automation. Selenium is composed of various components like:
The below-mentioned diagram gives you a fair understanding of selenium components and test automation tools.
[ Check out Selenium Commands with Examples ]
Selenium IDE is a tool that provides a complete integrated development environment for selenium tests. Shinya Kastani from Japan created it in 2006 and donated it to Apache's Selenium project. It's easy to use Firefox Add-On and Chrome Extension for faster execution of test cases and also allows you to record, edit, and debug functional tests.
Using the record-and-play feature automates the browser by giving auto-completion support and moving commands faster around. A particular test scripting language called Selenese records the scripts in Selenium. It comes up with commands to carry out actions in a web browser and restore data from the resulting pages. The benefit of using Selenium IDE is that tests recorded within the plugin can be used in programming languages like Python, Ruby, Java, etc. through export options.
To create test scripts using Selenium IDE, we need to use Selenium WebDriver or the Selenium RC.
Check out the below-mentioned image for Firefox's IDE plugin.
[ Related Article: Installation of Selenium IDE ]
Other than Selenese, Tests in Selenium supports various programming languages and communicates with Selenium through calling methods in the Selenium Client API. Presently, Selenium supports client APIs for C#, Java, Ruby, Python, R, and JavaScript.
Selenium Remote Control (RC) is also referred to as Selenium Core for a long time before WebDriver merge brought up Selenium 2. To overcome the same-origin policy created by Selenium core, Paul Hammant, a ThoughtWorks engineer introduced Selenium RC.
Selenium Remote Control involves an HTTP proxy server that "tricks" the browser into believing that both the web application and selenium core tested from the same domain. Therefore, making it has a two-component tool.
The above diagram depicts the brief architecture of Selenium RC.
Selenium RC manages various programming languages like PHP, Java, C#, Perl, Ruby, Python, etc. It was depreciated and moved to the legacy package from Selenium v3 onwards.
The drawback with Selenium RC is that when we try to execute test cases, we need to start the Selenium Standalone server manually and Selenium WebDriver overcomes the problem.
A browser automation framework that accepts commands and sends them to a browser is called a Selenium WebDriver. It communicates and controls the browser directly and executes through a browser-specific driver. WebDriver is an upgrade to RC as it runs much faster and makes direct calls to the Browser. The below diagram depicts how the selenium web driver works.
Selenium WebDriver supports the following:
Simon Stewart founded selenium WebDriver in 2006. It’s the first cross-platform that controls the Browser from the OS level. In contrast to Selenium IDE, WebDriver allows programming interfaces for creating and executing test cases. The web elements written under the test cases will enable you to identify the web elements on the web pages and perform actions on those elements.
[Related Article: RPA vs Selenium]
Selenium WebDriver API provides communication between both browsers and programming languages. The below image represents the architectural representation of Selenium Webdriver.
Let's understand each of them in detail:
Looking for Best Selenium Online Training Platfrom in Bangalore? To Enroll a Free Demo Click Here. |
Locating elements in selenium WebDriver is done through Selenium Locators by findElement() and findElements() methods.
Elements on a web page can have various attributes, and most of them will be notable for different elements.
The following are the eight attributes that we use to locate elements on a webpage:
Method | Syntax |
By ID | driver.findElement(By.id (<element ID>)) |
By name | driver.findElement(By.name (<element name>)) |
By class name | driver.findElement(By.className (<element class>)) |
By tag name | driver.findElement(By.tagName (<html tagname>)) |
By link text | driver.findElement(By.linkText (<linktext>)) |
By partial link text | driver.findElement(By.partialLinkText (<linktext>)) |
By CSS | driver.findElement(By.cssSelector (<css selector>)) |
By XPath | driver.findElement(By.xpath (<xpath>)) |
What are Browser Elements
Selenium WebDriver is one of the most widely used tools among all the set of tools available. It's essential to understand how to use Selenium for interacting with web apps. In this section, we'll learn how to communicate with GUI objects using Selenium Webdriver.
It performs application interaction using elements to develop user-defined functions. Browser elements are different components present on web pages. The most common elements we notice are:
Testing these actions essentially means checking whether they respond the way we want and working fine. For example, if you wish to test text boxes, what would you test it for?
If you are testing an image, you will download and upload the image, click on the image link, retrieve the image title, etc.
Similarly, operations are executed on each of the elements mentioned earlier.
Selenium Grid was initially called HostedQA and developed by Patrick Lightbody. With a combination of RC (remote control), it runs tests on the remote machines. By using Grid, multiple test scripts will operate on various devices at the same time.
It supports parallel execution using Hub-Node architecture. We can connect to the selenium RC by just stating the browser version and operating system details. Through remote control capabilities, we can specify these values. One server makes a move as a hub in Selenium WebDriver and tests communicate to the hub to access browser instances. The hub contains a list of servers that gives access to the browser instances (WebDriver nodes) and allows tests to use these instances. Test scripts executed on various nodes can be written in multiple programming languages.
Selenium Grid has more than 2000 browser environments that enable you to run tests and automate cross-browser testing.
TestNG stands for Test Next Generation, and it’s an open-source framework inspired by JUnit, which uses annotations. It supports grouping, parameterization, parallelization, sequencing techniques in the code, etc., which was not possible earlier.
Besides managing test cases, TestNG even maintains detailed reports of tests.
Since selenium does not support code execution in test cases, we can use TestNG for the same purpose.
TestNG framework provides the following features −
Annotation defines a piece of code that controls the flow of methods. Annotations play a significant role in the TestNG framework.
Some of them are listed below:
Annotated method | Use |
@BeforeSuite | This annotation will run only once before all tests in the suite have run. |
@BeforeTest | This annotation executes before the first @Test annotated method. |
@BeforeClass | This annotation executes before the first @Test method execution. |
@BeforeMethod | This annotation executes before every @Test annotated method |
@Test | Executed the methods under the test |
@AfterMethod | This annotation executes after every @Test annotated method. |
@AfterClass | This annotation executes after all the test methods in the current run. |
@AfterTest | This annotation executes when annotated methods complete the execution |
@AfterSuite | This annotation will run once after the execution of all tests in the suite has run. |
Software testing is one of the mainstreams in the recent past. Testing all the processes of the software against odds improves the performance of the system. Powerful tools like selenium, QTP, Cucumber, etc. dominate the software testing market. And, Selenium stands top on the list because of its features and functionalities.
As many companies have started using web applications, the need for Selenium is shooting up. The below listed are the few roles you can consider to work as Selenium WebDriver Professional:
Following are the limitations of Selenium:
There are seven essential elements of a Selenium test script, which apply to any test case and any application under test (AUT):
Import Packages/Statements:
The import statements imports built-in and user-defined packages into the Java source file.
1. Create a Selenium WebDriver Instance
Webdriver driver=new ChromeDriver();
To launch a website on the desired Browser, set up system properties to the Browser driver’s path.
System.setProperty(wedriver.chrome.driver","File path for the Exe");
2. Configure the Browser if needed
If you want to maximize the Browser, then you can do that by the code below.
driver.manage().window().maximize();
3. Navigate To The Required URL
Open a browser with the desired URL and write the below syntax.
driver.get("https://www.linkedin.com/login");
4. Locate The HTML Element
Try to locate the email and password field of the login area of Linkedin. Below is the DOM structure for the email input box:
Format:
driver.findElement(By.id("password")); WebElement
password=driver.findElement(By.id("password"));
driver.findElement(By.xpath("//button[text()='Sign in ']")); WebElement login=driver.findEllement(By.xpath('//button[text()='Sign in']"));
5. Perform an action on an HTML element
To execute actions, we need to make use of methods "sendKeys" and "click".
username.sendKeys("xyz@gmail.com");
password.sendKeys("exampleAboutSelenium123"); login.click();
Learn Selenium Interview Questions and Answers that help you grab high-paying jobs |
6. Verify & Validate The Action
To verify and validate the results, we need assertions. They play a vital role in comparing both the actual and expected results. The assertion class is presented by both the TestNG and JUnit framework, and we can choose either.
The below syntax will help you to assert the outcome from actions:
Assert.assertEquals(String actual, String expected);
So, in this case, we will save our actual URL post-login into a string value which is:
String actualUrl="https://www.linkedin.com/feed/";
And expected URL can be found from the below method:
String expectedUrl=driver.getCurrentUrl();
So your final assertion would become as:
Assert.assertEquals(actualUrl, expectedUrl);
The entire code we explained here is present in the below code snippet.
import java.utl.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.Driver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class LoginUsingSelenium{
@Test public void login(){//TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","path of driver");
WebDriver driver=new ChromeDriver(); driver.manage().window().maximize();
driver.get("http://www.linkedin.com/login");
WebElement username=driver.findElement(By.id("username"));
WebElement password=driver.findElement(By.id("password"));
WebElement login=driver.findElemet(By.xpath("//button[text()='Sign in']"));
username.sendKeys("example@gmail.com");password.sendKeys("password");
login.click(); String actualUrl="https://www.linkedin.com/feed/";
String expectedUrl=driver.getCurrentUrl();
Assert.assertEquals(expectedUrl,actualUrl);}}
With this, we have come to the end of this Selenium tutorial. We hope you understood the concepts and added some value to your knowledge. If you wish to gain more insights into Selenium, then check out our Selenium Training Online.
If you have any questions on Selenium or want any topic to learn from us, please comment below and try our best to help.
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Selenium Training | Nov 23 to Dec 08 | View Details |
Selenium Training | Nov 26 to Dec 11 | View Details |
Selenium Training | Nov 30 to Dec 15 | View Details |
Selenium Training | Dec 03 to Dec 18 | View Details |
Madhuri is a Senior Content Creator at MindMajix. She has written about a range of different topics on various technologies, which include, Splunk, Tensorflow, Selenium, and CEH. She spends most of her time researching on technology, and startups. Connect with her via LinkedIn and Twitter .