There are various new features added in Selenium 4. These enhancements are accompanied by deprecations and the addition of some methods. Let’s understand the deprecated methods and their replacement.
Drivers Constructor
In Selenium 3, some driver constructors accept the DesiredCapabilities object as a parameter. DesiredCapabilities is a class that is used to set basic properties of browsers such as browser name, browser version, operating systems etc. to perform cross-browser testing.
In Selenium 4, DesiredCapabilities is replaced with Options. The Options interface provides methods to change the properties of the browser such as cookies, incognito, headless, disable pop-ups, add extensions, etc. Testers can either use only Options or combine Desired Capabilities with Options.
Note: Options are preferably used with Capabilities for customizing driver sessions.
Selenium 3
Selenium 4
New Methods in the Actions Class
Actions class in Selenium provides methods to automate mouse and keyboard user interaction with WebElement of the web application. Selenium 4 provides few additional Actions methods which replace the classes under the org.openqa.selenium.interactions package.
click(WebElement)
In Selenium 3, toGitHub click a webelement we could use either of below two methods:
Selenium 4 provides a new method click(WebElement) in the Actions class which works as a replacement for the above methods.
Snippet
doubleClick(WebElement)
doubleClick(WebElement) method is used to perform double click on a WebElement. This method replaces moveToElement(WebElement).doubleClick() in Selenium 4.
Snippet:
Output
contextClick(WebElement)
contextClick(WebElement) method is used to perform a right click on a WebElement. This method replaces moveToElement(WebElement).contextClick() in Selenium 4.
Snippet
Output
clickAndHold(WebElement)
clickAndHold(WebElement) method is used to perform click action on a WebElement without releasing the mouse. This method replaces moveToElement(WebElement).clickAndHold() in Selenium 4.
Snippet
Output Screenshot: The screenshot after moving the dragbox by some offset using clickAndHold() method
release()
The release() and release(WebElement) methods are used for releasing the depressed left mouse button.
Snippet
Output Screenshot: The screenshot after dragging and releasing the box from source to destination
The sample codes are available at the Github repository here.
Changes in the FluentWait class
FluentWait<T> implements a generic functional interface Wait<T>.
Selenium
Selenium 3.11 and above
Implicit Wait
Selenium 4 has replaced the TimeUnit with Duration. The Duration class can be imported from java.time package and has methods to represent time duration in nano, millis, seconds, minutes, hours, days and so on. The method implicitlyWait(long , TimeUnit) from the type WebDriver.Timeouts are also deprecated. In Selenium 4, implicitlyWait method takes only one parameter.
FindsBy
The FindsBy interface, part of the org.openqa.selenium.internal package, is deprecated in Selenium 4. The changes are internal to the Selenium framework. Therefore, Selenium users can continue using the FindElement(By) and FindElements(By) as used in Selenium 3.
Conclusion
This article covers the deprecated, modified and new methods in Selenium 4. We need to consider these changes when we are migrating from Selenium 3 to Selenium 4.