Overview –

Selenium 4 Alpha 3 changes are pushed in Maven. Ther are so many changes it is bringing but we will be having a look at one of the important changes which are the Relative locator which earlier was friendly locator.

What are the Relative locators?

As the name suggests, its relative position of webelement.

Selenium 4 alpha-3 is released with much-awaited Relative locators which work very much like xpath axes. These new locator methods will find elements based on their relative location to other webobjects, Using relative locator an element can be located easily by

  • above(): target element which is above specified element
  • below():  target element which appears below the specified element
  • toLeftOf():  target element which appears to the left of a specified element
  • toRightOf():  target element which appears to the right of a specified element
  • near():  target element which is almost 50 pixels away from the specified element. 

How to use it, let’s take an example.

Therefore Relative work similarly Xpath Axes concept and identifies relative webobject.

Lets write a small code for it-

Configure Maven PoM file

First of all add selenium 4-aloha version, by adding a dependency in maven, as below-

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>RelativelocatorTest</groupId>
  <artifactId>RelativelocatorTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.0.0-alpha-3</version>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.7.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Example of Relative locator in below example-

In below code, Code is launching Orange HRM login page and with the help of Relative locator filling values in Username and Password-

In below code-

1- Admin value is inserted using “LOGIn Portal” Caption highlighted in Red circle

2- Password sendkeys are executed using Admin text box property

package Test;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;

public class Dummyrun {

    WebDriver driver;

    @Before
    public void setUp() throws InterruptedException {

    	WebDriverManager.chromedriver().setup();        
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://opensource-demo.orangehrmlive.com/");
        Thread.sleep(5000);
    }

    @Test
    public void relativeLocatorTest() throws InterruptedException {

    
		  WebElement ele_B = driver.findElement(By.cssSelector("div[id='logInPanelHeading']"));
		  WebElement ele_A =driver.findElement(By.cssSelector("div[id='divUsername']"));
      
         WebElement UserName =  driver.findElement(withTagName("input")
                 .below(ele_B));       

         UserName.sendKeys("Admin");
         Thread.sleep(4000);
         WebElement Password =  driver.findElement(withTagName("input")
                 .below(ele_A));
         Password.sendKeys("admin123");     
    }

    @After
    public void tearDown() {
        if(driver != null) {
            driver.close();
            driver.quit();
        }
    }
}

Other changes in V4- alpha-3

Apart from Relative locators, please have a look at what other changes in alpha3 it brings-

Refer below link for detail change logs-

https://github.com/SeleniumHQ/selenium/blob/master/java/CHANGELOG

Change log in v4.0.0-alpha-3

  • Add “relative” locators. The entry point is through the- RelativeLocator.
    Usage is like driver.findElements(withTagName("p").above(lowest));
  • Add chromedriver cast APIs to remote server (#7282)
  • By is now serializable over JSON.
  • Add ApplicationCache, Fetch, Network, Performance, Profiler,
    ResourceTiming, Security and Target CDP domains.
  • Fixing Safari initialization code to be able to use Safari Technology
    Preview.
  • Ensure that the protocol converter handles the new session responses
    properly.
  • Expose devtools APIs from chromium derived drivers.
  • Expose presence of devtools support on a role-based interface
  • Move to new Grid, deleting the old standalone server and grid implementation.
  • Switch to using HttpHandler where possible. This will impact projects that
    are extending Selenium Grid.
  • Respect “webdriver.firefox.logfile” system property in legacy Firefox driver.
    Fixes #6649
  • Back out OpenCensus support: OpenTracing and OpenCensus are merging, so
    settle on one for now.
  • Only allow CORS when using a —allow-cors flag in the Grid server
  • If you’re using the Java Platform Module System, all modules
    associated with the project are generated as “open” modules. This will change in a future release.
  • The version of Jetty being used is unshadowed.

Summary

We learned that Relative locator worked very similar to what Xpath AXES use to do.

Predecessor, chile, parent, ancestor etc . Instead of writing complex Axes we can use Relative locator to achieve web object identification where direct Xpath is difficult to find.

Hope this post helped you. Happy Testing !!!