jeffrey_crowley
13 years agoContributor
Tips & tricks for using XPaths in script automation
When I first started using TestComplete, I used "easy mode" (keyword tests) to record some tests. That all worked fine until I wanted to expand those tests recorded in one browser to do cross-browser testing... things stopped working and when I tried to fix them, things got complicated.
I searched the forums for alternatives and found recommendations for XPath. I had some experience with XPaths and so I tried using them. I ditched the keyword tests and started writing my own script tests. With a little bit of work, I had most everything working cross-browser. There were a few cases where the XPath I supplied worked in two browsers but not the third. At that point, I had to hand edit the XPath to make it more generic (which took some googling and reading). Now my test is working in IE, FF, and Chrome.
I wanted to distil what I have learned into a fairly short post for using XPaths to write automation in the hopes that others might try it and also that others more experienced than I would offer additional tips & tricks that I could learn from.
Getting started
If you want to learn how to use XPaths in script automation, read the article Finding Web Objects Using XPath Expressions.
Quickly generating XPaths
You can use Chrome or FF (with FireBug) to quickly generate XPaths.
I'll give you a quick example using Chrome but similar steps can be done in FF with the same result.
You now have the XPath query that you will need for your TestComplete script.
Performance
I've found the performance to be OK. I haven't compared XPaths to keyword testing but my gut is that they are about the same. I have done some initial testing of just using javascript and DOM methods to find elements and that is MUCH faster... orders of magnitude faster. For example, I ran a test and found 40 elements on a product page using XPaths vs DOM methods. The XPaths test took ~40s and the DOM methods took less than 1s. (By DOM methods I mean document.getElementByID, .getElementsByName, and the like.)
Cross-browser
I've generally found it to be very effective for cross-browser testing. It will depend on how much your HTML changes from browser to browser and what elements you are looking for but so far I've been able to generalize XPaths where needed so that I can find the element that I'm looking for in all browsers with just one statement (instead of one statement for each browser, etc.).
I'm interested to hear if anyone else has corrections or additional tips/tricks from using XPaths.
I searched the forums for alternatives and found recommendations for XPath. I had some experience with XPaths and so I tried using them. I ditched the keyword tests and started writing my own script tests. With a little bit of work, I had most everything working cross-browser. There were a few cases where the XPath I supplied worked in two browsers but not the third. At that point, I had to hand edit the XPath to make it more generic (which took some googling and reading). Now my test is working in IE, FF, and Chrome.
I wanted to distil what I have learned into a fairly short post for using XPaths to write automation in the hopes that others might try it and also that others more experienced than I would offer additional tips & tricks that I could learn from.
Getting started
If you want to learn how to use XPaths in script automation, read the article Finding Web Objects Using XPath Expressions.
Quickly generating XPaths
You can use Chrome or FF (with FireBug) to quickly generate XPaths.
I'll give you a quick example using Chrome but similar steps can be done in FF with the same result.
Load the test page in Chrome
Press F12 to open the Developer toolbar
Click on the Elements tab of the dev toolbar
Click on the magnifying glass (bottom left) and then click the element of the page you want to inspect. For example, if you want to validate the product description text, click on the product description on the page.
The HTML for the element you inspected is displayed with all parents and children of that element. Use this display to find the exact element that contains the text you want to verify.
Right-click on the desired element and click Copy XPath.
You now have the XPath query that you will need for your TestComplete script.
Performance
I've found the performance to be OK. I haven't compared XPaths to keyword testing but my gut is that they are about the same. I have done some initial testing of just using javascript and DOM methods to find elements and that is MUCH faster... orders of magnitude faster. For example, I ran a test and found 40 elements on a product page using XPaths vs DOM methods. The XPaths test took ~40s and the DOM methods took less than 1s. (By DOM methods I mean document.getElementByID, .getElementsByName, and the like.)
Cross-browser
I've generally found it to be very effective for cross-browser testing. It will depend on how much your HTML changes from browser to browser and what elements you are looking for but so far I've been able to generalize XPaths where needed so that I can find the element that I'm looking for in all browsers with just one statement (instead of one statement for each browser, etc.).
I'm interested to hear if anyone else has corrections or additional tips/tricks from using XPaths.