Elements
Using Environment Test Data for Dynamic Locators
Using Environment Test Data to parameterize locators
Section titled “Using Environment Test Data to parameterize locators”Suppose we need to locate the below element with the username text i.e ‘admin’:
![]()
An XPath locator using the text for the element would be //div[ text()=‘admin’ ]
Let’s assume that this username would be different for different Environments as shown below:
| Environment Name | Environment Parameters |
|---|---|
| Dev | {“Username”: “dev-admin”} |
| Staging | {“Username”: “staging-admin”} |
| QA | {“Username”: “qa-admin”} |
Here, we can make the XPath dynamic by taking the username from the Environment Parameter as demonstrated below: //button[text()=’*|username|’]
With the above dynamic locator, the locator value would be dynamically updated at runtime and the environment variable would be translated to its actual value as shown below:
| Selected Environment Name | Dynamic Element locator value at Test Runtime |
|---|---|
| Dev | //button[text()=’dev-admin’] |
| Staging | //button[text()=staging-admin’] |
| QA | //button[text()=’qa-admin’] |