Element identification is all about setting up a combination of attributes for an Element, so that it can be uniquely identified on the screen, when the test is running.
An element may be uniquely identified using multiple combinations of attributes. Not all attributes may be equally suitable from a long term maintenance and change-management point of view.
Here are some pointers to help you with setting up a stable element identification. These are just general suggestions, and your specific case may always be an exception. So, please use discretion.
Attributes with numeric values
In general, any attribute whose value includes random numeric digits may not be a suitable one to use.
Take an example below. When you use the attribute id as part of the Selector, it seems to get a perfect unique ID. But this value (2663:0) may not always stay the same. Application developer may change it in future or it may be dynamically generated based on various application conditions.
Note, that the id may still work in the first few passes of test execution, but you can't count on something like this longer term. A better option could be placeholder or class as an identification attribute.
See Regular Expression article for additional information.
It is important to avoid redundancy when you choose attributes. Find the minimal set of attributes that work for unique ID.
In the example below, you could get uniqueness by just using one of the attributes - class or placeholder. You don't need both.
More attributes you are relying on for unique ID, more risk you carry of change-management. If one of these two attributes changes in the future, this element may have challenges being recognized.
Extraneous space characters
If the attribute value includes leading or trailing spaces or some times multiple consecutive spaces, it may not be preferred as part of Selector. These space characters may change in the future and it is hard to troubleshoot as the text value looks pretty similar. So, avoid such attributes if possible.
href attribute and other URL based values
Similarly, when you use any URL based attribute values, make sure that application-environment specific text is not part of the URL. In the example below, your scripts may fail when running in Production or Dev environments as the URL specifically points to test environment. Either avoid such attributes or use regular expression to remove the initial part of the text.
Not all classes may be necessary when selecting class attribute
class attribute in html can indicate one or more classes. In accelQ, value for class attribute is a multi-select type. When you select class attribute for the element identification, you can edit the value to just choose a subset of classes that you may deem stable.
As a good practice, whenever you select class attribute in the Selector, be sure to review and remove unnecessary classes. If you are achieving uniqueness with a subset of classes that have good functional meaning, there is no need to choose others.
When working with Angular applications, for example, you can avoid classes such as ng-hide, ng-show etc. These are Angular implementation framework related classes with technical connotation. Do not rely on such classes.
In the example below, you can safely remove ng-prestine, ng-valid and medium classes from the value of class. source-account seems more functional and potentially safe to retain in the class value.
Be wary of 'text' attribute
In many elements, you may find that text attribute provides good uniqueness. But before deciding to use text attribute, carefully evaluate if this text will remain the same over a period of time and under different use cases.
Consider the example below:
This username link element will recognize perfectly as long as you are always running the test as Thomas. But if you run the same test with another customer account, this element will not be identified, as the name of the customer won't match the name that this element is looking for.
Having said that, there may be perfectly valid cases for using text attribute as in the example below. Forgot Password Link is a link with static text and the text is functional. Safe to use.