Selenium is a free (open-source) automated testing framework used to validate web applications across different browsers and platforms. You can use multiple programming languages like Java, C#, Python etc to create Selenium Test Scripts. Testing done using the Selenium testing tool is usually referred to as Selenium Testing.
Selenium Software is not just a single tool but refers to a suite of tools that are widely used in the testing community when it comes to cross-browser testing. Selenium cannot automate desktop applications; it can only be used in browsers. It is considered to be one of the most preferred tool suites for automation testing of web applications as it provides support for popular web browsers which makes it very powerful.
The name Selenium came from a joke which Jason Jason Huggins (primary creator of Selenium) cracked once to his team. During Selenium’s development, another automated testing framework was popular made by the company called Mercury Interactive. Since Selenium is a well-known antidote for Mercury poisoning, Jason suggested that name and that is how we got to call this framework up to the present.
Selenium IDE (Integrated Development Environment) is primarily a record/run tool. It is an add-on or an extension available for both Firefox and Chrome that generates tests quickly through its functionality of record and playback. You don’t need to learn any test scripting language for authoring any functional tests.
In the case of working with Selenium RC (Remote Control), one must have good knowledge of at least one programming language. This tool allows you to develop responsive design tests in any scripting language of your choice. Server and client libraries are the two main components of Selenium RC. Its architecture is complex and it has its limitations.
Selenium WebDriver is an enhanced version of Selenium RC. It was introduced in the market to overcome the limitation faced in Selenium RC. Though it is an advanced version of RC, its architecture is completely different from that of RC. Just like Selenium RC, Selenium WebDriver too supports multiple programming platforms to provide wider flexibility and requires knowing any one programming language.
Selenium Grid is a tool that is used for concurrent execution of test cases on different browsers, machines, and operating systems simultaneously. This tool makes Cross-browser compatibility testing very easy. There are two versions of the Selenium Grid – the older version is known as Grid 1 and the recent version is known as Grid 2.
Selenium WebDriver is a web framework that permits you to execute cross-browser tests. This tool is used for automating web-based application testing to verify that it performs expectedly. It allows you to choose a programming language to create test scripts.
Locators are the way to identify an HTML element on a web page. Locators are one of the essential components of Selenium infrastructure, which help Selenium scripts in uniquely identifying the WebElements (such as text box, button, etc.) present on the web page.
Selenium supports the following locators:
"ID" as a locator is one of the most common ways of identifying elements on a web page. According to W3C, an ID for a web element always needs to be unique. The ID is one of the fastest and unique ways of locating web elements and is considered as one of the most reliable methods for determining an element.
To find an element with id attribute 'firstName' on the web page, we can use the following syntax using the "By" class:
By.id("firstName");
So, we can locate any web element which has a specified "id" attribute associated with it.
Selenium also offers users a way to identify the element using the name attribute. But contrary to id, a web page can have multiple elements with the same “name” attribute. So, if we are going to use the name attribute for the identification of a web element, we should always make sure that the name attribute must contain a unique value.
To find an element with name attribute 'gender'' on the web page, we can use the following syntax using the "By" class:
By.name("gender");
So, we can locate a web element that has a unique name attribute.
ClassName allows Selenium to find web elements based on the class values of the DOM.
To find an element with className attribute 'practice-form-wrapper'' on the web page, we can use the following syntax using the "By" class:
By.className("practice-form-wrapper");
To identify it successfully, we need to make sure that the class name value that we are using for locating the web element, i.e., web form, is unique, and any other class doesn’t have the same value. If any other class contains the same value as this, then Selenium will select whichever element comes first while scrapping through the web page.
LinkText and partialLinkText both are quite similar in functionality and locate web elements by using the hyperlink texts. We can only use them for elements containing anchor tags. Similar to other locator strategies, if multiple hyperlinks with the same texts are present on the page, then Selenium will always choose the first one.
For identifying elements using link text, we need to use the hyperlink text, as shown below:
By.linkText("Home");
Similarly, partial link text can also recognize the element by using part of the hyperlink text, as shown below:
By.partialLinkText("Ho");
It will identify any link that contains ‘Ho’ in the hyperlink text. Likewise, if several links are containing “Ho”, then Selenium will select the first one.
This locator type uses tag names to identify elements in a web page. The tag name is the HTML tag, such as ***input, div, anchor tag, button, etc.
The following syntax finds the web elements with a tagName:
By.tagName("a");
The tagName locator returns all the elements from the page that contains a specified tag.
CSS or Cascading style sheets are used extensively to style webpages. As these days most of the web pages are dynamically designed. Thus it is quite challenging to get a unique id, name, or class to locate element. In this type of situation, CSS selectors can be a great alternative as these are way faster compared to another locator strategies.
Sample code to find the element with tag_name 'input', attribute name 'id' and attribute value 'userName' using cssSelector will be the following:
By.cssSelector("input[id= 'userName']");
XPath uses the XML expression to locate an element on the webpage. Similar to CSS selectors, Xpath is quite useful in locating dynamic elements on a webpage. Xpath can access any element present in the webpage even when they have dynamic properties.
The basic syntax of identifying a web element using the XPath locator strategy is as follows:
//tag_name[@attribute_value]
Sample code to find the element with tag_name 'input', attribute name 'id' and attribute value 'userName' using XPath will be the following:
By.xpath("//input[@id='userName']");
Thanks for reading the post!
Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.