Weight rules and dynamic IDs | Ranorex Studio User Guide
Help CenterUser GuideRanorex Studio expert

Weight rules and dynamic IDs

On the previous pages of this chapter, we explained how attribute weights influence the generation of RanoreXPath expressions and showed you where in Ranorex Studio you can change attribute weights. On this page, we’ll now use this information to solve a more complex issue in test automation: robustly identifying dynamic UI elements.

For this purpose, we will adjust attribute weights in a weight rule and then apply this rule using a rule condition. The Ranorex Studio Demo Application will serve as the AUT for this example.

In this chapter

    Start Ranorex Studio Demo App and Spy

    If you don’t already have the Demo App on your computer, you can download it here and unpack it to a folder of your choice.

    Start the Demo App and click the tab Dynamic IDs.
    Click Show image with dynamic ID.

    Note icon

    Note

    The image displayed receives a new random ID each time you click the checkmark to display it, i.e. a dynamic ID. You can see the current ID to the right of the image.

    Issues when using dynamic IDs for identification

    Let’s look at what issues come up when using a dynamic ID to identify a UI element. To illustrate this, we track the image two times:

    If the image isn’t already visible, click Show image with dynamic ID.
    In Ranorex Spy, click Track and track the image in the Demo App. Note the generated RanoreXPath expression.
    In the Demo App, hide the image again.
    Show the image once more.
    Track the image again and note the generated RanoreXPath expression.

    Hiding and displaying the image generates a new ID for it each time. When tracking the same image before and after redisplaying it, two different RanoreXPath expressions are therefore generated. They use different values for the attribute ControlName.

    RanoreXPath expression when displaying the image the first time.

    RanoreXPath expression when displaying the image the second time.

    Note icon

    Note

    Dynamic IDs/values aren’t always immediately recognizable, but often, they consist of a fixed prefix or suffix (e.g. img_) and an alphanumeric string that doesn’t follow any particular pattern.

    Prefix img_

    Randomly and dynamically generated alphanumerical string

    Aside from being harder to read, RanoreXPath expressions containing such dynamic IDs also prevent robust identification and therefore automated testing of the referenced UI elements. To solve this issue without having to manually edit RanoreXPath expressions for dynamic UI elements every time, we need to change attribute weights and implement them with a weight rule.

    Create a new weight rule

    To get a robust RanoreXPath expression without a dynamic ID in it, we need to create a new weight rule with adjusted attribute weights. This way, Ranorex Studio will avoid the dynamic attribute and use a different, more robust one to identify the UI element.

    In our case, 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 and could therefore be used instead of ControlName. However, its weight is lower, so that’s where we need to make an adjustment.

    We will now use a weight rule to tell Ranorex Studio to use the attribute ControlTypeName instead of ControlName when generating the RanoreXPath expression. To define this new rule, open the configuration dialog for attribute weights (see ⇢ Attribute weights) and:

    In the configuration dialog, click Add.

    Enter a name.

    Select the capability that contains the attribute whose weight you want to change. In our case, this is Control.

    Select the attribute whose weight you want to change. In our case, this is ControlName.

    Set the new weight value. We reduce ControlName’s weight to 99. This will cause Ranorex Studio to ignore it for RanoreXPath generation and use whichever other attribute has the highest weight. In our case, this will be ControlTypeName.

    Click OK or Apply.

    tipp icon

    Hint

    Instead of reducing one attribute’s weight, you can also increase another’s, e.g. you could ControlTypeName’s weight to above 150 in our case. This is a matter of preference.

    Test the new weight rule

    Let’s now check whether the weight rule works as intended. To test the new weight rule:

    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 used 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 has a weight of 99 and is therefore ignored by Ranorex Studio for RanoreXPath generation.

    The RanoreXPath expression is now robust against changes in the dynamic ID. However, our weight rule applies to all WinForm UI elements, so the attribute ControlName won’t be used in any of them for identification. This could be a disadvantage when trying to identify a UI element where ControlName would be a robust attribute. To solve this, we need to apply a weight rule condition.

    Create a rule condition

    To prevent our rule from being applied to all WinForms UI elements, we need to define a rule condition that restricts the rule’s use to the cases where we want it applied.

    Note icon

    Note

    To create a sensible rule condition, you need to recognize a pattern in the dynamic ID. In our 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 the rule we created above.

    Click Add condition.

    This is where you set the source the condition refers to, i.e. the UI element that must have the defined attribute and the defined regex in its RanoreXPath expression. The options correspond to nodes in the UI element tree displayed in Ranorex Spy

    • self: Ranorex Studio checks the UI element it is identifying (i.e. generating the RanoreXPath for) itself, i.e. the image of the cat in our case. Usually the best choice.
    • parent: Ranorex Studio checks the UI element that is the parent of the UI element it is identifying (i.e. generating the RanoreXPath for), i.e. the container containing the image in our case.
    • toplevel: Ranorex Studio checks the UI element that is the top-level ancestor of the UI element it is identifying (i.e. generating the RanoreXPath for), i.e. the application ( = Demo App) containing the image, in our case.

    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. All attributes of the capability selected in the rule definition above are available. In our case, ControlName is the correct attribute to choose, since this is the one with the dynamic ID.

    The ⇢ regex that the attribute’s value will be checked against. If it matches, the condition is fulfilled. In our case, we only want the weight rule to 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.

    Test the rule condition

    Now let’s test whether the rule condition works as intended. To do so, we’ll first track a UI element with a dynamic ID, i.e. the image in the Demo App. This will show whether the weight rule is applied correctly so that the generated RanoreXPath does not use the attribute ControlName.

    Then we’ll track a UI element that doesn’t have a dynamic ID. This will show whether the weight rule is NOT applied so that the generated RanoreXPath does use the attribute ControlName.

    Track the dynamic UI element

    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 used the attribute ControlTypeName instead of ControlName.

    Overview of all the attributes for this UI element.

    The weight rule works and is applied correctly.

    Track the non-dynamic UI element

    Start the Demo App.

    Click the tab Image-based automation.

    Click Show image.

    In Ranorex Spy, click Track and track the image.

    The generated RanoreXPath shows that Ranorex Studio used the attribute ControlName with the value TheCat.

    Overview of all the attributes for this UI element.

    As intended, the weight rule is not applied.