Understanding Static and Dynamic Web Elements
Static Web Elements
A static web element is an element whose attribute values do not change. For example, the attribute 'font-family' for a website's logo text typically remains constant. Identifying static web elements accurately is crucial for building reliable and maintainable test automation scripts. Misidentifying these elements can lead to ineffective or failed test executions.
Scenario: Assume a Login page where the username field's “name” attribute is static, while its “id” attribute is dynamically generated each time the page is loaded. Here, using the “name” attribute is the best practice for identifying this static web element since its value remains constant.
Dynamic Web Elements
A dynamic web element is one whose attribute values may change. For example, the value of the 'src' property of a carousel image on a Homepage might vary. Identifying dynamic web elements correctly is critical for creating effective test automation scripts that can handle changes in the application.
Scenario: Let's consider, a news website where the main headline's “id” attribute changes based on the latest news story. In such cases, it would be better to identify the headline using a static attribute like “class,” or if there's a pattern in the dynamic attribute, we could use regular expressions. You can also customize the value to accommodate predictable dynamic changes.
Avoid attributes with unpredictable dynamism for identifying web elements, as this could lead to unreliable test scripts.
Setting Locator Priorities
The types of locators can be:
Link
Name
ID
XPath
CSS
This sequence is adjustable according to your specific needs.