An Introduction To Selenium Automation Testing

Example Subheading

July 21, 2022

By Ashwinder Kaur

What Is Selenium?

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.

Why The Name Selenium?

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 Components

Selenium IDE

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.

Selenium RC

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

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

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.

What Is Selenium WebDriver?

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.

Basic Steps In A Selenium WebDriver Script:

  • Create a WebDriver instance.
  • Navigate to a webpage.
  • Locate a web element on the webpage via locators in Selenium.
  • Perform one or more user actions on the element.
  • Preload the expected output/browser response to the action.
  • Run test.
  • Record results and compare results from them to the expected output.

Locators In Selenium

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:

  • ClassName – A ClassName operator uses a class attribute to identify an object.
  • cssSelector – CSS is used to create style rules for webpages and can be used to identify any web element.
  • Id – Similar to class, we can also identify elements by using the ‘id’ attribute.
  • linkText – Text used in hyperlinks can also locate element
  • name – Name attribute can also identify an element
  • partialLinkText – Part of the text in the link can also identify an element
  • tagName – We can also use a tag to locate elements
  • xpath – Xpath is the language used to query the XML document. The same can uniquely identify the web element on any page.

How To Locate A Web Element Using The "ID" Attribute?

"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.

How To Locate A Web Element Using The "name" Attribute?

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.

How To Locate A Web Element Using The "ClassName" 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.

How To Locate A Web Element Using The "LinkText" And "partialLinkText" Attribute?

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.

How To Locate A Web Element Using The "TagName" Attribute?

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.

How To Locate A Web Element Using The "cssSelector" Attribute?

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']");

How To Locate A Web Element Using The "xpath" Attribute?

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!

Ash Headshot

Ashwinder Kaur

Quality Assurance Lead

Ashwinder, aka "Ash" is a Quality Assurance Lead dedicated to ensuring high-quality products. She has a Bachelor's of Computer Science & Engineering ad more than 7 years of QA experience under her belt. Ash loves to spend time with her family, sightseeing (especially the Rocky Mountains), and enjoys solving math problems with her son.