November 20, 2007

Don't skimp on defining ids in your DOM

Recently after updating from Selenium RC from 0.8.3 to 0.9.2 I noticed some serious performance degradation, actually in an order of magnitude slower. Tests affected the most were those using XPath to match attributes. These tests were doing too broad searches by using '//*' such as in '//*[@id='errorField1']/td[2]'.

As a fix searches were narrowed by providing a more specific search, such as '//tr[@id='errorField1']/td[2]'.

Locators starting with '//*' search any element node from the current node that match the selection no matter where they are, been therefore very expensive.

So the general recommendation is: be specific. Try to provide the node you want directly, ideally, a specific id should be provided, i.e. 'id('errorField')//' (although there are known issues in IE6).

Despite the fact that I'd rather see a better performance when advancing versions, the moral is: don't skimp on defining ids in your DOM, the more the DOM is properly marked the easier and faster it is to be tested. Keep in mind that acceptance tests are time consuming and performance improvements are greatly appreciated, especially if you run them as part of your continuous integration build.

No comments:

Post a Comment