Hidden Functions In Selenium 4
Hidden Functions In Selenium 4
Take a look at some of the features in each of the Selenium components that have been released with the new version of Selenium 4.
Join the DZone community and get the full member experience.Join For Free
Selenium Makes Automating Browsers Easier and Better
There's no doubt that Selenium is one of the best automation undertakings in the QA testing industry. This article will highlight some of the functions in the pre-release early version of Selenium 4. You can download Selenium 4 Alpha from Maven’s Repository, and the code from this article is available at https://github.com/RexJonesII/blog.
All components in the Selenium Suite have something new, including Selenium IDE, Selenium WebDriver, and Selenium Grid.
- Selenium IDE allows users to record, playback, edit, and debug a test.
- Selenium WebDriver is an API that allows us to create and execute test scripts.
- Selenium Grid helps us to run our test across multiple browsers, operating systems, and machines in parallel
You may also enjoy: Components of the Selenium Automation Tool
Most of the time, when people say Selenium, they are referring to Selenium WebDriver, so let’s start with Selenium WebDriver.
W3C WebDriver Protocol
According to Simon Stewart, Selenium’s Project Lead, the W3C WebDriver Protocol is the reason for upgrading to Selenium 4. It’s fractionally different compared to the original JSON Wire Protocol. One of the advantages involves our test which runs consistently between browsers. A standardization of W3C encourages compatibility across different software WebDriver API implementations.
The major difference with Selenium adopting the W3C Protocol is how sessions are created and element interactions are performed using Actions API. With the Actions API, we can perform multiple actions at the same time. Here’s a screenshot of the Selenium Client and WebDriver language bindings, browser drivers, and web browsers without the original JSON Wire Protocol.
Selenium 4 presents an opportunity for us to work with multiple tabs and windows. We can leverage the
WindowType typeHint ) method. This method creates a new browser window and switches the focus to the new browser window. It’s handy if we choose to load a new window/tab then open a different page and complete an action. The following test scripts load a new tab and window via TestProject.
A new tab is loaded and the page title is printed after switching to the tab. The next test script performs the same steps but switches to a new window.
The purpose of Relative Locators is to find an element regarding the position of another element. There are five overloaded methods with a By and WebElement parameter. In alphabetical order, the methods are
- above – element located above the defined element
- below – element located below the defined element
- near – element located near the defined element
- toLeftOf – element located to the left of the defined element
- toRightOf – element located to the right of the defined element
For example, let’s say it’s challenging to find the red heart icon on CarMax’s website. However, the Location drop-down is easier to find than the red heart icon.
The following test script finds the Location drop-down then uses the
withTagName method. This method is a parameter of
findElement and returns
RelativeLocator. The red heart icon is positioned to the right of Location drop-down. Therefore, the test script finds the element implementing the
toRightOf Relative Locator and the Location drop-down WebElement. After finding the red heart icon, the test script clicks the icon to load the Favorites page.
Chrome DevTools and ChromiumDriver
Chrome DevTools is a set of developer tools built directly into the Chrome browser. Fortunately, Selenium 4 provides a direct communication line to the browser by way of DevTools. As a result, we can write more stable locators and track what’s going on in the browser.
With Selenium 3, ChromeDriver and EdgeDriver inherited implementations from the RemoteWebDriver class. Those implementations changed in Selenium 4. Now, ChromeDriver and EdgeDriver are inherited from ChromiumDriver. The ChromiumDriver class contains several methods to establish a connection with DevTools.
In addition, Selenium 4 has a class named DevTools. The purpose of DevTools is to increase productivity. Some of the capabilities include:
- Viewing the DOM
- Handling Developer Options
- Adding Listeners
- Creating a Session
- Inspecting Network Activity
- Emulating Network Connections
- Measuring Performance
Selenium Grid 4 has a new architecture supporting four separate processes: Router, Distributor, Session Map, and Node. With Selenium 2, the Hub included three of those processes (Router, Distributor, Session Map).
- Router – listens to a new session request
- Distributor – selects a node to run our test
- Session Map – responsible for mapping session ID to the node
- Node – test machine where Test Script execute
Moreover, Selenium Grid 4 allows observability and updated to be more modern. Observability permits us to trace and log what’s going on. As a request comes in, there will be a trace ID to help our debugging efforts.
Modern means Selenium Grid will be more convenient for modern technology like Docker and Kubernetes. We can use Docker which allows us to scale the grid and Kubernetes to set up a grid machine. The grid can take advantage of infrastructures like AWS and Azure. It will be easier to use virtual machines.
In the past, Selenium IDE was only available as a Firefox extension using the XPI file. XPI is a cross-platform compressed installation used by Mozilla application including Firefox. Mozilla changed from using the XPI file and Selenium IDE stopped working for Firefox. As a resurgent, Selenium IDE is now called Selenium IDE TNG (The Next Generation). It’s available as a Firefox and Chrome Extension.
Backup Element Selectors are beneficial when a change is made to a web application. Selenium IDE records our test using multiple locators. As a result, our test will continue to pass using a backup element selector if a developer modifies an element. For example, Selenium IDE records the test with an XPath locator, CSS locator, id locator, etc. so our test doesn’t fail. This provides power to a stable Test Script.
The ability to include Control Flows (conditional logic and loops) are available from Selenium IDE. This offers a powerful set of options to execute our test scripts. Conditional statements determine what happens next if a condition is met or not met. The loop executes a statement(s) for a specific number of times in a program. Here’s a list of the conditions and loops provided by Selenium IDE:
- if – executes a statement when a condition is true
- else if – only executes when the first if condition is false
- else – extends the if condition when the condition is false
- repeat if - execute a block of statements then determine whether a condition is true then repeat if it is true. Execute a block of statements at least one time
- while - only execute a block of statements if a condition is true
- times - determine the number of times a condition/block of statements should execute
For more information, view the Change Log to see updates regarding each programming language:
- C# - https://github.com/SeleniumHQ/selenium/blob/master/dotnet/CHANGELOG
- Java - https://github.com/SeleniumHQ/selenium/blob/master/java/CHANGELOG
- Python - https://github.com/SeleniumHQ/selenium/blob/master/py/CHANGES
- Ruby - https://github.com/SeleniumHQ/selenium/blob/master/rb/CHANGES
Opinions expressed by DZone contributors are their own.