Handling dynamic IDs from Telerik framework
Usually in automation there is a golden rule not to automate changing requirements that is because if you do that the ROI that you are gaining from your automation suite is very little. but what if your elements in your automation application is changing? Are you not going to automate it?
The answer is NO!! If you are automating an application with dynamic ID generation look very closely at the changing IDs. If you check one element from different browsers you will see that there is always a static part for the IDs but a dynamic part is added to the static ID to generate an unique ID to the element.
This could happen if you're automating an application which uses a mechanism to generate webpages like Episever, Sitecore, Crownpeak, Kentico. In all these systems there is a underlined webpage generation method so this might generate elements with dynamic IDs
Example:
Imagine your login button has a dynamic ID : relateLoginbtn_Ctl012
if you load the elements explorer several times you might able to see that the ending number "012" is always changing but the text part of it is static, so if you check this ids by refreshing the browser couple of times you could see the ids as below
relateLoginbtn_Ctl212
relateLoginbtn_Ctl001
relateLoginbtn_Ctl259
So now you know for sure that only the ending number is changing. This changing part should be in the front of the element id, in the middle of the id or at the back of the id. so how do we automate these kind of elements?
Telerik gives you couple of options to tackle such situations, you can find the objects by ID name
ID is exactly (if the ID is static)
ID Contains (search for some text in the ID)
ID Starts with (the starting point is static)
ID Ends with (the endinging point is static)
use a regular expression to match the ID
There are more methods but these are the most important ones
so how these ID capturing works?
this may vary how do you capture the element in your test script, you may use ActiveBrowser.FindByID or you may using a complex method like page object model or paje factory just for the maintenance purposes, it dosent matter which method you uses, I will demonstrate the easiest way here, so you can modify it for your preferences.
if the login button is a HtmlInputButton
Is exactly -> id: relateLoginbtn_Ctl012
HtmlInputText logginButton = ActiveBrowser.Find.ById("relateLoginbtn_Ctl012").As<HtmlInputText>();
Contains -> id: 0012relateLoginbtn_Ctl012
id=~
HtmlInputText logginButton = ActiveBrowser.Find.ByExpression("id=~relateLoginbtn_").As<HtmlInputText>();
Starts With -> id: relateLoginbtn_Ctl012
id=^
HtmlInputText logginButton = ActiveBrowser.Find.ByExpression("id=^relateLoginbtn_Ctl").As<HtmlInputText>();
Ends With -> id: 012Loginbtn_Ctl
id=?
HtmlInputText logginButton = ActiveBrowser.Find.ByExpression("id=?Loginbtn_Ctl").As<HtmlInputText>();
Regular Expression
id=#
HtmlInputText logginButton = ActiveBrowser.Find.ByExpression("id=#regulerexpression").As<HtmlInputText>();
so this is how you capture elements with dynamic IDs for automation
Happy testing!!!!
Comments
Post a Comment