A guide to notable changes and deprecations for engineers looking to migrate from Selenium 3 to Selenium 4.
There is huge excitement within the testing community since Simon Stewart, the lead of the Selenium project, announced the release of Selenium 4 with major changes and W3C standardization to the Selenium suite (Selenium IDE, Selenium WebDriver and Selenium Grid) during the official Selenium Conference 2018 at Bangalore, India.
Supercharge Selenium with Applitools Visual AI
Get StartedAs of early September 2020, the alpha version of Selenium 4 is available and can be downloaded to explore.
Manoj Kumar has already explained at a high level what’s new in Selenium 4, and in this weekly blog series, we will discuss these features in detail with code snippets, benefits and possible use cases for the end users and well as the dev community who have built tools/products on top of Selenium.
Key takeaways
- Understand the deprecated components and their replacements
- Plan the migration from Selenium 3 to 4
Selenium is a toolset for web browser automation that allows us to remotely control browser instances and emulate a user’s interaction with the browser. There are many changes in Selenium 4, the major being the W3C standardization, which implies that Selenium can be integrated with any software without any compatibility issues. Along with W3C standardization, there are few new methods added and deprecated/replaced.
Let’s discuss these in detail.
What’s been deprecated in Selenium 4?
There are a couple of deprecations in Selenium 4. Let’s explore them.
FindsBy
FindsBy interfaces are part of org.openqa.selenium.internal package having findElement(By) and findElements(By) methods, implemented by the RemoteWebDriver class. These are now deprecated as part of Selenium 4.
This is an internal change and does not impact end users, however if you have a product or tool built on top of Selenium APIs, have a look into these changes.
The By class can be used with findElement(By) and findElements(By) just like before.
Actions
The Actions class is a user-facing API for emulating complex user gestures like hovering, mouse movements, etc.
A few new methods have been added to the Actions class as a replacement of the classes under package org.openqa.selenium.interactions:
click
A new method click(WebElement) is added to Actions class and is a replacement of moveToElement(onElement).click() and is used to click on an element.
clickAndHold
Similarly, clickAndHold(WebElement) is used to click on an element without releasing and should be used as an replacement of moveToElement(onElement).clickAndHold()
contextClick
contextClick(WebElement) is a replacement of moveToElement(onElement).contextClick() which was used to right click on an element.
doubleClick
doubleClick(WebElement) is used to double click on an element and is a replacement of moveToElement(element).doubleClick().
release
release() is used to release the depressed left mouse button at the current mouse location. This was earlier part of org.openqa.selenium.interactions.ButtonReleaseAction class, and is now moved to Actions class.
Examples
The below example demonstrates the usage of Actions methods discussed in Selenium 3 from line 17 to 33.
These methods can be replaced as shown in the below code snippet as part of Selenium 4 implementation from line 37 to 53.
FluentWait
The methods withTimeout() and pollingEvery() from FluentWait class have been changed. Now both methods accept a single parameter java.time.Duration in replacement of two parameters int and TimeUnit. Lets see the usages.
In the Selenium 3 example below, we see on line 6, the pollingEvery() method takes two parameters int and TimeUnit and can be imported from the package java.util.concurrent.TimeUnit. On line 7, withTimeout() also accepts int and TimeUnit as parameters.
Selenium 4 has replaced the TimeUnit with Duration and here is the sample code snippet demonstrating it. On line 12, pollingEvery() method now accepts only one parameter, Duration.
The Duration class can be imported from the java.time package and has methods to represent time duration in nano, millis, seconds, minutes, hours, days and so on.
In our example we have used ofMillis(int) and ofSeconds(int) methods.
Similarly, on line 14 withTimeout() method accepts Duration as a parameter.
Driver Constructors
A couple of driver constructors have been deprecated. Namely, the ones that accepted Capabilities objects have been replaced with ones that accept Options.
This means you will need to create a specific Options object for whichever Driver class you’re using, set your requirements and pass this object to the Driver constructor.
Examples for each of the drivers can are below
ChromeDriver
ChromeDriver(Capabilities) is replaced by ChromeDriver(ChromeOptions).
SafariDriver
SafariDriver(Capabilities) is replaced by SafariDriver(SafariOptions).
EdgeDriver
EgdeDriver(Capabilities) is replaced by EdgeDriver(EdgeOptions).
FirefoxDriver
FirefoxDriver(Capabilities) is replaced by FirefoxDriver(FirefoxOptions).
InternetExplorerDriver
InternetExplorerDriver(Capabilities) is replaced by InternetExplorerDriver(InternetExplorerOptions).
Summary
We went through the major deprecations in Selenium 4 and have outlined the ones that are important for end users in their migration from Selenium 3 to 4.
The list of all deprecations can be explored in the Selenium WebDriver API docs. There are considerable internal interfaces, classes and ENUMs deprecated which have no impact on end users but may be of importance to vendors of tools and products built on top of Selenium.
In the next post of this series, we will explore the newly added features offered in Selenium 4 and the benefits they provide.
About the Author
Shama Ugale is a quality assurance specialist at Thoughtworks. She has great knowledge about Selenium project and Selenium 4 that she is happy to share with you. You can finder on LinkedIn at https://www.linkedin.com/in/shama-ugale-7a95b549/