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
@BeforeClass
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("browserVersion", "90.0");
capabilities.setCapability("platformName", "mac");
capabilities.setCapability("applicationName", "Testing");
capabilities.setCapability("chrome.driver","90.0");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(capabilities);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.qed42.com");
}
Selenium 4
Using Options class only: Create an instance of Options class, set the requirements and pass it to the Driver constructor.
@BeforeClass
public void setUp() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
options.setAcceptInsecureCerts(true);
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(capabilities);
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://expired.badssl.com/");
}
Combining DesiredCapabilities with Options class: Use the merge(Capabilities) method of the Options class.
@BeforeClass
public void setUp() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
options.setAcceptInsecureCerts(true);
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(capabilities);
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://expired.badssl.com/");
}
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:
driver.findElement(WebElement).click();
OR
action.moveToElement(WebElement).click();
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.
Snippet:
@Test
public void dblClickAction() {
driver.get("https://api.jquery.com/dblclick/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe")));
Actions action = new Actions(driver);
WebElement doubleClickBox = driver.findElement(By.xpath("//span[text()='Double click the block']//parent::body/div"));
System.out.println("Color Before: " +doubleClickBox.getCssValue("background-color"));
action.doubleClick(doubleClickBox).build().perform();
System.out.println("Color After: " + doubleClickBox.getCssValue("background-color"));
}
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
@Test
public void contextClickAction() {
driver.get("https://swisnl.github.io/jQuery-contextMenu/demo.html");
WebElement rightBtn = driver.findElement(By.className("btn"));
Actions action = new Actions(driver);
action.contextClick(rightBtn).perform();
List elements = driver.findElements(By.cssSelector("li span"));
System.out.println("WebElements After Right Click:");
for (WebElement element : elements) {
System.out.println("\t" + element.getText());
}
}
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.
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.
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.
Selenium
In Selenium 3, the methods withTimeout() and pollingEvery() take two parameters int and TimeUnit.
With Selenium 3.11 (and above), the methods withTimeout() and pollingEvery() take only one parameter java.time.Duration.
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(Duration.ofSeconds(SECONDS))
.pollingEvery(Duration.ofSeconds(SECONDS))
.ignoring(Exception.class);
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.
@BeforeClass
public void setUp()
{
System.setProperty("webdriver.chrome.driver", /usr/local/bin/chromedriver");
driver = new ChromeDriver();
//deprecated
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
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.