How to Use Ranorex With Relative Locators From Selenium 4

Dec 15, 2023 | Product Insights, Test Automation Insights

Relative Locators From Selenium

Selenium 4 introduced relative locators. This new feature allows the user to locate an object in relation to another object on the screen. While Ranorex Studio’s object repository is unable to handle this feature as a user you can create user code to access this feature.

How to Add User Code to Your Solution

Adding user code is easy when you follow the steps below.

1. Right-click on a recording in the solution and select View Code, or expand the Recording.rxrec item and open the Recording.UserCode.cs inside of it like below:

2. Inside the file that opens, you can add your user code, like below:

3. After placing your custom code and saving the file, go to the recording module and click on Add new action -> User Code -> New user code method.

4. In the Method column of the newly added action, select “Select from library…” from the dropdown and you will be presented with the window below, where you can select a method from your custom code:

5. Click Confirm and the custom code user action is added to the recording.

Once you’re ready to add your custom code, your User Code file should start with the following header. This ensures that all necessary libraries are included:

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Repository;
using Ranorex.Core.Testing;

namespace MyTest3

Sample User Code to Access the Relative Locators of Selenium 4 

Now, let’s dive into an example. The following user code demonstrates how to access Selenium 4’s relative locators within Ranorex:

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;
using System.Linq;
using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Repository;
using Ranorex.Core.Testing;
using OpenQA.Selenium;
namespace MyTest3

public partial class Recording1

/// <summary>
/// This method gets called right after the recording has been started.
/// It can be used to execute recording specific initialization code.
/// </summary>

private void Init()
// Your recording-specific initialization code goes here.
public void Test_Selenium_Relative_Locators_In_Usercode()
      var webDriverEndpoint = Host.Current.TryGetAsWebDriverEndpoint();
      if (webDriverEndpoint != null)
           OpenQA.Selenium.IWebDriver driver = webDriverEndpoint.WebDrivers.FirstOrDefault();
                // Get base elements
                var firstCheckbox = driver.FindElement(By.Id("isAgeSelected"));
                // ToRightOf locator
                var relativeLocatorToRight = RelativeBy.WithLocator(By.TagName("input")).RightOf(firstCheckbox);
          var inputFieldToRight = driver.FindElement(relativeLocatorToRight);

          // ToLeftOf locator
          var relativeLocatorToLeft = RelativeBy.WithLocator(By.TagName("input")).LeftOf(inputFieldToRight);
          var inputFieldToLeft = driver.FindElement(relativeLocatorToLeft);
          // Below locator
          var relativeLocatorBelow = RelativeBy.WithLocator(By.TagName("input")).Below(firstCheckbox);
          var inputFieldBelow = driver.FindElement(relativeLocatorBelow);
          // Above locator
          var relativeLocatorBelow2 = RelativeBy.WithLocator(By.TagName("input")).Below(inputFieldBelow);
          var inputFieldBelow2 = driver.FindElement(relativeLocatorBelow2);
          var relativeLocatorBelow3 = RelativeBy.WithLocator(By.TagName("input")).Below(inputFieldBelow2);
          var inputFieldBelow3 = driver.FindElement(relativeLocatorBelow3);
          var relativeLocatorAbove = RelativeBy.WithLocator(By.TagName("input")).Above(inputFieldBelow3);
          var inputFieldAbove = driver.FindElement(relativeLocatorAbove);

          // Near locator
          // This doesn't work yet because the C# selenium client is broken for the "near" locator

    //          var inputFormsDropdown = driver.FindElement(By.ClassName("dropdown-toggle"));
    //     var relativeLocatorNear = RelativeBy.WithLocator(By.ClassName("dropdown-toggle")).Near(inputFormsDropdown, 100);
    //     var datePickersDropdown = driver.FindElement(relativeLocatorNear);
    //     datePickersDropdown.Click();

Ranorex and Selenium 4 Relative Locators

As we’ve seen, integrating Selenium 4’s relative locators with Ranorex opens up new possibilities for enhancing your automated testing strategies. Although Ranorex Studio’s object repository doesn’t directly support this feature, the flexibility of adding custom user code allows you to leverage these powerful locators. This approach not only expands the capabilities of your testing tools but also ensures your applications are rigorously tested against a wider range of scenarios. We encourage you to experiment with this technique in your own testing environments. Remember, the key to successful automation lies in continuous learning and adaptation.

Related Posts:

What Is Code Profiling and How to Choose the Right Tool?

What Is Code Profiling and How to Choose the Right Tool?

Developers and programmers have long been resorting to tools, techniques, and methods to help them better create software applications, with one of them being code profiling. But what is code profiling, and how can it enhance your IT programming projects? Our...

End-to-End Testing vs Integration Testing Explained

End-to-End Testing vs Integration Testing Explained

Understanding end-to-end testing vs integration testing is essential for the successful outcome of your software development project. Both testing methods are necessary. Each offers unique benefits that contribute to your overall software quality and functionality....

How to Develop a DevOps Testing Strategy

How to Develop a DevOps Testing Strategy

In the ever-evolving world of software development, the demand for faster, more reliable, and higher-quality software has never been greater. In order to meet these demands, organizations are turning to DevOps for the development and testing needs of their software....