This page explains how to identify dynamic UI elements robustly by adjusting attribute weights and applying rule conditions. You solve complex identification issues by creating a weight rule that forces Ranorex Studio to avoid dynamic attributes in favor of robust ones.
Issues when using dynamic IDs for identification
Dynamic IDs prevent robust identification because they change every time the application state resets. As a result, they also prevent automated testing of the referenced UI elements.
For example, in the Dynamic IDs tab of Ranorex Studio Demo App, the cat image receives a new random ID each time you click the checkmark to display it.
If you track this image using Ranorex Spy, you can see that the application generates two different RanoreXPath expressions. They use different values for the attribute ControlName.
- Prefix img_
- Randomly and dynamically generated alphanumerical string
To solve this issue without having to manually edit RanoreXPath expressions for dynamic UI elements every time, you need to change attribute weights and implement them with a weight rule.
Create a new weight rule
To generate a robust RanoreXPath without a dynamic ID, you create a weight rule that forces Ranorex Studio to use a different, more robust attribute to identify the UI element.
For the Ranorex Studio Demo App example, the automatically chosen attribute ControlName belongs to the Control group of the technology-specific capability WinForms.
- ControlName’s value is the dynamic ID. The attribute also has the highest weight at 150. Therefore, Ranorex Studio uses it in generating the RanoreXPath expression.
- The attribute ControlTypeName has the stable value PictureBox. You could use this attribute instead of ControlName. However, its weight is lower, so that’s where you need to make adjustments.
To define the new rule that uses the attribute ControlTypeName instead of ControlName when generating the RanoreXPath expression, do the following:
- Open the configuration dialog for attribute weights. For more information, see Attribute Weights.
- Click Add.
- Enter a name for the rule.
- Select the Capability that contains the attribute whose weight you want to change. For the Demo App, choose Control.
- Select the attribute whose weight you want to change. In this case, ControlName.
-
Set the new weight value to 99.
- Click OK or Apply.
By lowering the weight of ControlName, you force Ranorex Studio to select the next highest-weighted attribute, in this case ControlTypeName. Instead of reducing one the weight for the ControlName attribute, you can also increase ControlTypeName’s weight to above 150 to achieve the same result.
Test the new weight rule
Verify the new weight rule as follows:
- Start the Demo App.
- Click the tab Dynamic IDs.
- Click Show image with dynamic ID.
- In Ranorex Spy, click Track and track the image.
- The generated RanoreXPath shows that Ranorex Studio uses the attribute ControlTypeName instead of ControlName.
- The overview shows that the dynamic ID is still the value for the attribute ControlName. However, this attribute now weighs 99 and is therefore ignored by Ranorex Studio for RanoreXPath generation.
The RanoreXPath expression is now robust against changes in the dynamic ID. However, the new weight rule applies to all WinForm UI elements. This means that Ranorex does not use attribute ControlName in any of the UI elements for identification. This could be a disadvantage when trying to identify a UI element where ControlName is a robust attribute. To solve this, you need to apply a weight rule condition.
Create a rule condition
To prevent your rule from being applied to all WinForms UI elements, you need to define a rule condition that restricts the rule’s use to the cases where you want to apply it.
To create a sensible rule condition, you need to recognize a pattern in the dynamic ID. In the Ranorex Studio Demo App example, the pattern is that the dynamic ID always consists of the prefix img_ and then an alphanumeric string.
To create a rule condition:
- Open the configuration dialog for attribute weights.
- Select your rule and then click Add condition.
-
Set the Source the condition refers to:
- self: Ranorex Studio checks the UI element it is currently identifying. In the example, the image of the cat. This is usually the best choice.
- parent: Ranorex Studio checks the UI element that is the parent of the UI element it is currently identifying. In the example, the container of the image.
- toplevel: Ranorex Studio checks the UI element that is the top-level ancestor of the UI element it is identifying. In the example, it is the Demo App containing the image.
- Select the Attribute that must be present in the UI element’s generated RanoreXPath and contain the defined regex. In other words, the attribute with the dynamic value. In the example, ControlName is the correct attribute to choose, since this is the one with the dynamic ID.
- Enter the Match RegeEx that the application must use to check against the attribute’s value. In the example, you want the weight rule to only apply if the RanoreXPath expression generated for a UI element contains the attribute ControlName with a value that consists only of the prefix img_ and an alphanumerical string.
The rule now applies only when the ControlName matches the specified pattern.
Test the rule condition
Verify the rule condition by tracking different elements in the Demo App using Ranorex Spy:
Track the dynamic UI element
- In the Demo App, go to the Dynamic IDs tab and check the Show image with dynamic ID option.
- Track the image with Ranorex Spy.
Ranorex Studio uses the attribute ControlTypeName instead of ControlName to generate the RanoreXPath.The weight rule works and is applied correctly.
Track a non-dynamic UI element
- In the Demo App, go to the Image-based automation tab and check the Show image option.
- Track the image with Ranorex Spy.
The generated RanoreXPath uses the attribute ControlName with the value TheCat. As intended, the application does not apply the weight rule.