How to Deal with Dynamic Elements
Some website use html generation frameworks. These frameworks will often create identifiers that change every time the page is loaded. These are known as dynamic element locators and can be quite tricky to deal with in UI test automation. If the identifiers for a particular element are different each time the page is loaded, how do you find the element that you want on the page? Well, this is where the TestProject Smart Recorder really shines!
If you have tried to create test automation for sites like this in the past, you have experienced first hand just how difficult and painful this can be. There are a number of strategies that you can use to deal with this. For completeness sake we have included ways to do those strategies, but the reality is if you are using the Smart Recorder, you deal with dynamic elements in the same way you deal with static ones. You just choose them in the recorder and then run your test.
But how is that even possible? If you click on the element the first time and the recorder chooses the autogenerated ID, then the next time you run the test it won't be able to find the ID. So how can we say that you don't need to worry about it? Well, it is thanks to the self-healing AI that TestProject uses. When the test is re-run and it can't find the ID, it will automatically find the element with another selector.
Try it out for yourself. If you have a website they uses these dynamic elements and you have been struggling with it in your test automation, try recording a test in TestProject and see what happens. TestProject's self-healing technology will take care of it automatically!
Without the self-healing AI that TestProject provides, you would have to use manual selectors to to find these kinds of elements. If you are using the old recorder, you may need to use XPath or Parameters to find the elements that you want. As you will find out if you try these strategies, they have their own set of challenges. You will almost certainly find that you have better luck if you let TestProject take care of everything automatically for you!
If the actual element you want has a dynamic ID or other attributes, you can always use XPath to find that element in the Document Object Model (DOM) hierarchy. This method can work, but there are also significant downsides to it as well. There are more details on the benefits and risks of XPath in the documentation section on Best Practices for Element Locators.
Sometimes you may be able to parameterize part of the search query you are looking for so that you can match what you looking for with what you have entered. For example, if an element locator is built based on some user input it would of course change depending on that input, but if you parameterize that input you can still find the element.