Selenium is an open-source API used for web browser automation.
This blog is to illustrate the new features introduced in Selenium 4.
- Selenium WebDriver supports different languages like Java, C#, Python, Ruby, PHP, etc.
- Selenium 4 does not have any JSON wire protocol over HTTP.
In Selenium4, we are not converting request and response in JSON format. Instead the information is transferred directly between client and server.
New Features Offered In Selenium 4
- Relative Locators
- Handling Multiple windows and Tabs
- Partial screenshots
- Capturing Height and Width of WebElement (UX validation)
- Chrome DevTools
- W3C WebDriver Protocol
Relative Locators
- Relative Locators are used to locate elements based on their position with respect to other elements. And they are easy to implement since they do not require an exact locator instead they use nearby elements to identify.
- The main advantage of relative locators is to identify elements with no unique attributes.
- We use Relative locators when an element does not have uniquely identifying attributes. For example, we might need to identify a certain button among other 2 buttons. And they all have the same class attribute, in this case we use relative locator of any other webelement to identify that particular button.
- If the button is above the submit button we will specify the locator of the submit button and driver.findElement(withTagName(“label”).above(nameEditBox)).
- TagName() is a new method added, returns an instance of RelativeLocator.RelativeBy class, a child class of By and offers methods to specify the direction of an element to be located.
- above()
to locate an element above the stated element
- below()
to locate an element below the stated element
- toLeftOf()
to locate an element located to left of a stated element
- toRightOf()
to locate an element located to right of a stated element
- near()
to locate an element at 50 pixels distant from a stated element.
Handling Multiple windows and Tabs
Do you need to work with multiple windows or applications in a single test flow? This is now possible!
Without creating a new driver object we can create and open a brand new or tabbed window in same session to manage multiple application in same test
We can use the newWindow() method to achieve this.
driver.switchTo().newWindow(WindowType.WINDOW);
This would help us in automating such scenarios where we need to automate multiple windows or multiple windows in a single test case. and also in executing multiple tests with single login.
Partial screenshots
In earlier version of Selenium, we were not able to take partial screenshot but Selenium 4 has given us the solution by providing us the Partial Screenshots of webelement feature. Partial screenshot helps us in debugging and the default screenshot taken will be stored in the project’s root level.
Capturing WebElement Height and Width for UX validation
Selenium 4 has provided a feature for UX validation, it helps to capture height and width of elements. We can use getRect().getHeight() and getRect().getWidth() to perform validation.
driver.findelement(By.id(“id”)).getRect().getheight();
driver.findelement(By.id(“id”)).getRect().getwidth();
Chrome DevTools
- Previously if we wanted to access developer tools we had to do that manually. But now Selenium 4 has come up with a new feature to test the backend call and track browser activity and diagnose problems.
- DevTools capabilities includes:
Inspecting Network Activity
Handling Developer Options
Viewing the DOM
Measuring Performance
W3C WebDriver Protocol
Earlier versions of Selenium other than selenium 4 used the JSON wire protocol to communicate between Webdriver APIs and browser native APIs. The communication between Webdriver and browser happens without encoding and decoding with W3C compliance.
Now the softwares following W3C protocol can be integrated with Selenium with no compatibility issues