If software is configurable, it often means that the software relies on dynamic content – dynamic content is typically based on dynamic identifiers (IDs). Using dynamic IDs often leads to problems in test automation because they are newly generated each and every time an element is displayed. This blog post will illustrate how easy it is with Ranorex to overcome this frequent problem in test automation in an automated way.

Robust Test Automation with Dynamic IDs


What is the Problem

Ranorex decides which attribute will be used for object identification based on predefined attribute weights (or “RanoreXPath weights”). Usually the ID of an object will be the best and most stable attribute for identifying and object and therefore has the highest weight. But when facing dynamic IDs, the ID cannot be used to identify an element anymore because it changes each time the element is reloaded/displayed. That means that you manually have to change how the objects are identified in your repository (which might be previously created by the recorder). This blog post will illustrate how you can add so called “weight rules” (RanoreXPath weight rules) allowing you to continue using the recorder and no longer changing your repository manually afterwards.

Identify Dynamic Content

First of all we have to identify in general whether we do have a dynamic ID problem or not. Let’s have a look at yahoo.com which is based on YUI (Yahoo User Interface library), a free open source JavaScript and CSS library. This library uses dynamic IDs by default. To analyze the occurrence of dynamic contents you can use Ranorex Spy from the start menu. Use the “Track” button to analyze an element of the web page.

Identify UI element with dynamic ID

When you take a closer look at the attribute “Id” of the just tracked element, you will see that it contains dynamic content (yui_3_8_1_1_13679224741219_543).

That leads on the one hand to a poor readability, and on the other hand destroys the testability of the page as the element cannot be identified by its ID anymore after reloading.

To illustrate this behavior, add the analyzed element to the repository (using the context menu), reload the website and add the element again. You will see that the same element will occur twice in your repository having different RanoreXPaths based on different IDs. Highlighting the first (red) repository element will not work as an element with the given ID is not available anymore. Also, the second (blue) element will only work until you reload the page again and a new ID is generated.

Different Paths After Reload

So this way we identified that we do have a dynamic ID problem. (For the sake of completeness: In most of the cases the attribute “Id” leads to dynamic content issues, but of course there can be other attributes creating identification problems as well.)

 

Add a Weight Rule

What we know now is that we will run into a problem when choosing the attribute “Id” to identify elements. Now we have to identify where our dynamic ID belongs to and create a rule on the base of that. As we can see in the “Overview” tab of Ranorex Spy, it belongs to the capability “WebElement”:

Identify Problem

Write down the capability and the belonging attribute.

After identifying the correct capability and its attribute, we add a path weight rule to “filter out” the dynamic content and use another property for object identification. To do so, make sure that all other Ranorex instances (Studio, Recorder, Spy, etc.) are closed and only the currently using Spy instance is open. Then open “RanoreXPath Weight Rules” editor from settings dialog and add a new weight rule by pressing the “+” button:

Add Path Weight

Choose an appropriate rule name, select the previously identified capability as well as the attribute, and set the weight to 0:

Add Path Weight 02

Have a look at the attribute overview (“Show Attribute Overview…”) to get a feeling for the existing attribute weights. In order to navigate to a specific capability, simply press the initial letter of the capability’s name on your keyboard.

Attributes Overview

As you can see in this dialog, the weight of the attribute “Id” is set to a value of 200. When lowering this weight to “0” the attribute “InnerText” will be used to identify the element as it’s the next higher value of 140. If no other attribute is available (because the weight is set to zero or it has no value) the index of the element will be taken to identify it (e.g. [1]).

(For the sake of completeness: In addition to lowering the weight of attributes you don’t want to use, you can also raise an attribute’s weight to favor the use of this specific attribute for object identification.)

Add a Condition to the Weight Rule

By lowering the weight of the attribute “Id” for the capability “WebElement”, the attribute “Id” will no longer be used for object identification in general. This rule will not only filter out dynamic IDs on yahoo.com but also all other IDs for all other web pages which might not be what you want to achieve.

To overcome this behavior, you can add a condition by pressing the “Add Condition” button. This defines which IDs should be filtered and which shouldn’t.

You might have found out already that dynamically generated YUI IDs always have the prefix “yui” followed by “_” and a number for x times. This can simply be represented by the regular expression

yui(_[0-9]+)+

The matching expression must start with the string “yui” followed by “_” and a number for one or more times. The whole “_” and number stuff must occur one or more times. For further details about regular expression have a look at the regular expressions wiki page or the chapter RanoreXPath in our user guide. Select the attribute you want to match against the regular expression from the dropdown, and then enter the regular expression.

Add Path Weight Rule

Check whether the newly created rule in the left sided list is enabled, and save your changes by pressing the “OK” button.

You can check whether the newly added RanoreXPath weight rule works or not by tracking the element again on the website. You will see that the attribute “Id” is no longer used to identify the element, but by the “InnerText” attribute now.

Identify UI element without the use of the dynamic ID

You can best see the difference by adding the element to the repository again (green).

Repository

 

Share the new Rule with your Team

Once you successfully created a new rule, other colleagues testing the Yahoo web page might be interested in it, too. You can easily share the rule by using straightforward Copy and Paste functionality. Therefore copy the rule to clipboard via CTRL + C or by using the “Copy” icon in the toolbar.

Copy Rule

Then paste it to the text editor of your choice (e.g. Notepad) by pressing CTRL + V and save the file with a meaningful name, e.g. Webelement_YUI_IDs_rule.txt.

Save Txt

On the target machine of your colleague, copy the entire file content (CTRL + A, CTRL + C) and paste it to “RanoreXPath Weight Rules” Dialog by pressing CTRL + V or use the toolbar icon “Paste”.

Paste Rule

 

Conclusion

Using the RanoreXPath Weight Rules can assist you in automatically creating a robust repository which is the fundament of a robust test automation framework.

Also, it gives you the great advantage to continue using the recorder, and no longer being forced to change all your dynamic UI-elements in the repository manually. Just re-record and you will be fine.

By sharing the rule with your team, other testers save time and can also directly start recording.
Since you created a global “path weight rule” for your object identification, the rule will also be applied if you create a repository manually (as described in the blog post “Did you know… that you can manually generate a recording?“).

Added Path Weight

You might also like these articles