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.
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.
- Using Options class only: Create an instance of Options class, set the requirements and pass it to the Driver constructor.
- Combining DesiredCapabilities with Options class: Use the merge(Capabilities) method of the Options class.
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.
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.
doubleClick(WebElement) method is used to perform double click on a WebElement. This method replaces moveToElement(WebElement).doubleClick() in Selenium 4.
contextClick(WebElement) method is used to perform a right click on a WebElement. This method replaces moveToElement(WebElement).contextClick() in Selenium 4.
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.
Output Screenshot: The screenshot after moving the dragbox by some offset using clickAndHold() method
The release() and release(WebElement) methods are used for releasing the depressed left mouse button.
- release() method already exists in earlier versions of Selenium. In Selenium 4 this method is moved from org.openqa.selenium.interactions.ButtonReleaseAction class to the Actions class.
- release(WebElement) method replaces moveToElement(WebElement).release() in Selenium 4
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>.
- The Fluent Wait defines the maximum amount of time for the web driver to wait for a condition, as well as the frequency with which to check the condition before throwing an ElementNotVisibleException exception.
- It checks for the web element at regular intervals until the object is found or timeout happens.
- Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.
- In Selenium 3, the methods withTimeout() and pollingEvery() take two parameters int and TimeUnit.
Selenium 3.11 and above
- With Selenium 3.11 (and above), the methods withTimeout() and pollingEvery() take only one parameter java.time.Duration.
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.
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.
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.