Okay, so I will start this post saying there is not a single golden rule to handle dynamic id while automating Test. It depends, I repeat it depends. In this post I will focus on various ways you can handle dynamic id in Test Studio.
In this post we will learn to deal with dynamic id in three different ways,
- Handling with changing setting at Test Project level
- Handling with changing setting at Element level
- Handling with Coded Step
Scenario # 1 : Test Project Level
Let us say you are sure that id is going to be dynamic throughout the application. And you cannot rely on id to identify element. In this scenario Test Studio allows you to configure Identification Logic at the Test Project level. You can change that in Project Settings.
By default Test Studio finds and element on following identification logic. It search on the id AND tagname. This is set for the Test Project level.
Now we know that id for application in test is going to be dynamic and we want to change the FindLogic to some other tag. In other words we want to change default Find Logic for whole Test Project. To do this navigate to Project Settings.
You can browse to FindLogic setting of Test project by clicking on
Project -> Show Setting
In Project Setting dialog box click on identification logic. Here you can find identification logic and change it as per requirement to your project.
As you can clearly see that by default Test Studio find an element on its Id. If you consider that Name of the elements are not expected to change frequently then you can consider to change project setting to find an element on its Name attribute.
To change FindLogic select any attribute then click on up/down arrow to change its position. For example Name is at second from top. To make it first attribute on which Test Studio will find element select Name and then click on up arrow button to bring it at the top in the list.
In this way you can configure FindLogic. You may notice that if required you can add new Tag to list as well. After reconfiguring Identification Logic you can find that now FindLogic for whole Test project (recording which will happen after changing logic) has been set to name and tagname.
Scenario # 2 : Element Level
Second scenario could be you need to change identification logic only for a particular element. You do not want to change identification logic for whole Test project rather for a particular element. Test Studio allows you to do that as well. To do this select element from Element Repository and right click. From context menu select Edit Element.
When click on Edit Element, next you will get various options to Find Element.
Let us Find Element in Live Version. To find element in Live Version make sure that application is already open in browser. Since already application is opened here so select open Current Page and select application from drop down.
After navigating to application you will find Elements current FindLogic.
To delete current logic move mouse and you get option like below to delete current filter.
After deleting both filter let us use XPath to find element.
This is what all you need to change FindLogic for a particular element. You can see in Element Repository that FindLogic has been changed to XPath.
Scenario # 3 : Code Step
So far in above two scenario we saw that if Id is dynamic we can change it to some other attribute of element like Name, XPath etc. Now there could be scenario that you cannot rely on any attributes.
XPath gives you positional based FindLogic. Consider scenario of a Grid. In Grid you need to automate test to find an Employee ‘e2’. Say e2 is second employee in database and is listed in the second rows of Grid. You can put verification step as below,
Now above test will fail if order of elements in Grid is changed. So in this kind of scenario you cannot rely on either id or XPath of element.
There could be many ways you can fix and automate test to find whether employee e2 exists in db or not. Test Studio allows you to write your custom logic using Coded Step. So we can write a coded step as below.
In above coded step, we are finding reference of Grid then searching Employee ‘e2’ in whole grid and then asserting if count is equal to 1 to verify existence of Employee e2.
So in this scenario we are not finding element on any of the attribute and writing our own logic in script to find element.
We learnt in this post that there are various ways to deal with dynamic id. Again there is not a single bullet proof method to deal with dynamic id. It depends on the scenario that how dynamic id cane be handled while automating test.