Hello everybody,

Automation testers are very curious nowadays. Selenium 4 is about to be released, we wanted to show you some sample codes for the new features. There are big changes in Selenium 4 on WebDriver and Selenium Grid parts. In that article we’ll concentrate on Selenium 4 features below:

  • Taking Element Screenshot
  • Open New Window and Tab
  • Get Element Location and Size

There are many more features coming like Chrome DevTool protocol and Friendly Locators. Those are big concepts and we’ll concentrate on them in separate articles.

To add Selenium 4 in your Java project you can use Maven or Gradle.

Maven Dependency

Gradle Dependency

Take Screen Shot of a WebElement

WebElement object has a new method called getScreenshotAs which enables you to take a screenshot of a specific WebElement. Here’s the snippet.

Open Window of Tab

You can now open a blank window or a tab on your browser. You can use the below snippet to open a new tab. After opening a new tab, you can use navigate().to() methods to go to another page.

Opening a new window is also very easy. All you need to do is to change the WindowType to WINDOW.

Getting Object Location and Size

In the previous version of Selenium, there was a method to access the Dimension of a WebElement. Now, there’s a Rectangle object. By using this Rectangle property, you can access the location and size of the WebElement.

 

In the next article, we’ll deep dive into advanced features of Selenium 4.

You can access the sample code via https://github.com/swtestacademy/Selenium4

Happy testing