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.
- What is the Problem?
- Identify Dynamic Content
- Add a Weight Rule
- Add a Condition to the Weight Rule
- Conclusion
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. 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. 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.)
Identify Dynamic Content
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. 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. You can best see the difference by adding the element to the repository again (green).
Add a Condition to the Weight Rule
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. 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?“).
Conclusion