I’m very new to Katalon Web Testing and I’m trying to get the dynamically get the xpath in my application.
I’ve a drop-down list (list of locations).
My application can access different instances by changing the url. Functionality for each instance remains the same. But, the list of locations for each instance may be different. I’m trying to search for a location say, A-ABC on my first instance and record the testing it works fine when i play it. But when I run the same test case on another instance it fails to find that location, because i the second instance the Location lies in a different list index.
Xpath for 1st instance-> //*[@id=“main”]/div/div/div/div/div/ul/li/a/span
Xpath for 2nd instance-> //*[@id=“main”]/div/div/div/div/div/ul/li/a/span
So, the li index has changed from 7 to 8 from first instance to second.
Can someone help me find a solution to run my application without having to manually change the xpath every time i switch between the instances.
You should try to identify more attributes in your XPath so that it is not influenced when the instance is restarted.
I don’t know the contents of the page you are automating (that would have helped a lot) but adding more attributes to the XPath would help in your issue.
As an example in one webpage I identified the li by using id so XPath is something like //*[@id=“main”]//li[@id=‘id_of_li’]/a/span.
You can use different attributes to help identify the objects better. You can use XPath helper on Google Chrome to test your XPath (it helps me a lot).
These are examples of what are known as “highly position-dependent xpaths”, which are a cardinal sin for XPath enthusiasts. They are what the recorder will usually return to you, because they aren’t very good at determining relative xpaths.
So you need smarter relative xpath for sure (your topic reads “Dynamic xpath…”, but in general, xpaths are static, so you really need a “smarter” xpath to locate a dynamic element). Using the index of an element is generally not a good thing, as you’ve obviously found. I second @cristian.ivanescu, we need to see your HTML to give you a good example xpath to use.
My li tag does not have an id attribute against it. Also, the list is dynamically refreshed by the js which shifts the index.
However is there a way where I can get the xpath of a test object. Say, I search a location by setText and get the xpath of the location where it occurs and then set that xpath to a variable and then modify the current xpath in the Object repository with the value of the xpath stored in the variable.
I’m not sure if this is valid. I need your opinions please. Thanks in advance.