As we all belonging to Testing domain, familiar with Selenium. In April 2019 Simon Stewart — the founder of Selenium released a latest version as Selenium 4. It is still in alpha phase and it’s stable version is yet to be released. Stable release of Selenium till date is Selenium 3.141.59 for Java. The latest alpha version of Selenium 4 is 4.0.0-alpha-7 which is released in November 10, 2020.
\We all know that Selenium WebDriver supports multi-language, multi-browser, multi-OS platform.
In this post we are going to explore the new features and Enhancements added in Selenium. To use Selenium 4 features in your project you need to download Selenium 4 jars and install it.
Enhancements in Selenium 4:
1. Support for Docker
2. Identification of Parent Frame directly improved
3. In Selenium Grid some commands, properties, URL’s have changed and improved.
New Features of Selenium 4:
In Selenium 4, many features have been added till 4.0.0-alpha-7. Some of them are mentioned below:
1. Relative/Friendly Locators
2. Window and Tab Management
3. Element Screenshots
4. Full page screenshots support for Firefox
5. Element Location(Get Height, Width, Dimension, Coordinates)
6. Removes deprecated methods and classes.
WebDriver W3C Standardization- From latest release of selenium, Selenium 4 is WebDriver API standardization as per W3C standards. So WebDriver API will be compatible for implementation across multiple platforms.
Selenium added some Relative locators in new version as below:
These Relative locators use with only new withTagName() method.
Here for Relative locator, there is no auto import option available in eclipse to import those jars. So below import need to add in code manually.
import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
1. above(): Element which is located above the specific element.
We need to create a WebElement instead of giving direct path of element.
2. below(): Element which is located below the specific element.
3. toLeftOf(): Element which is located to the left side of specific element.
4. toRightOf(): element which is located to the right side of specific element.
5. near(): Element which is at most 50 pixels far away from the specified element. Pixel value is modifiable.
These tools idea is inspired by Sahi — an automation tool.
2. Window and Tab Management:
With Selenium 4 new features, we can run two different windows, tab at the same time and navigate between them or new URL’s.
Example for Windows:
WebDriver driver = new ChromeDriver();
WebDriver secondWindow = driver.switchTo().newWindow(WindowType.WINDOW);
Example for Tabs:
For tabs there is change for TAB only
WebDriver driver = new ChromeDriver();
WebDriver secondWindow = driver.switchTo().newWindow(WindowType.TAB);
3. Element Screenshot:
In Selenium 3, we could able to take screenshot of an entire page, but from Selenium 4 new released, we can take or capture specific element screenshot by using getScreenshotAs() method.
Check below example:
WebElement firstname = driver.findElement(By.cssSelector(“[name=’firstname’]”));
File file= firstname.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(“screenshot.png”));
4. Full page screenshots support for Firefox:
With Selenium 4 new released feature, now we can take full page screenshots with getFullPageScreenshotAs() method in the Firefox. You need to typecast it to FirefoxDriver instance instead of ‘TakesScreenshot’ interface.
File file =((FirefoxDriver)driver).getFullPageScreenshotAs(OutputType.FILE);
5. Element Location(Get Height, Width, Dimension, Coordinates):
Using Selenium 4 new features, we can achieve any specific element’s height, width, Dimension, coordinates by using getRect() method. Selenium 4 added Rectangle class which contains getRect() method to achieve these feature.
Check below code for better understanding:
WebElement element = driver.findElement(By.name(“demo”));
Rectangle rect = element.getRect();
Here, it checks the coordinates from top left corner of element.
System.out.println(name.getRect().getX());//To fetch x axis position
System.out.println(name.getRect().getY()); //To fetch y axis position
System.out.println(“Element Width: “+rect.width);
System.out.println(“Element Height: “+rect.height);
Or you can get it by using this way also,
So in this post, we have covered some of the features of Selenium4. In any case if you need selenium 4 documentation here. We will update according to changes.