开发者

Locators preference in Selenium RC

I need help开发者_Go百科 on choosing locators while writing selenium script...

what are the differences between using XPath, CSS, identifiers? Is XPath preferred over the others? If so, why?


The order you should try pick locators is in the following order

  1. ID
  2. Name
  3. CSS
  4. XPATH

ID and Name will help create good maintainable tests because the types of elements can be changed without breaking your test. For example, if you clicked on a <input type=button id='foo'> and that got changed to <div id='foo'>Bar</div> then your tests would still work as long as what happened next still was in the same order


CSS would be my choice for speed in execution and readability/maintainability. Further, CSS locators probably witness the least amount of change in an iterative environment (as opposed to changing link names, captions, etc.). The only downside to using CSS is the lack of in-built support within the IDE for formulating them.

Here is a great post articulating why CSS is the locator of choice :)


Xpath is generally slow, especially in IE. CSS selectors are not supported in all browsers, especially IE (seeing a trend here?). Generally use name or id selectors where you can as these are fast and back it up with xpath selectors where you can't. Writing good xpath selectors is a skill, if you notice that it is causing your tests start to slow down alot you can probably go back and optimise the selectors.


While the three other answers thusfar all provide important information on aspects of your question, there is still one more resource that you may find helpful. I recently put together a cookbook and comparison chart covering all the locator technologies supported by Selenium: this quick reference literally lets you see "...the differences between using XPath, CSS, identifiers" and more. See XPath, CSS, DOM and Selenium: The Rosetta Stone on Simple-Talk.com.


Hi just to add bit more, 1). ID or NAME is native to browser and all the others CSS, XPATH, Javascript are foreign to the browser( ie.. they have to rely on their respective engines). so native is always first choice. 2). you can not make use of all the features of CSS or XPATH ( ie.. browser support issues with different versions of CSS or XPATH). 3). XPATH is problematic with IE7. (so i had write every element location paths using CSS only) 4). IE7 failing to find very basic CSS path sometimes (css=table.nav td.name), so i had to change that to javascript coding (dom=document.findElementsByTagName('td')[0])


The order of choosing Locators should be :

  1. Id : If the web application follows W3C guidelines, you wil get only single element on the page. Hence, speed of searching element will be highest.
  2. Name
  3. Link text
  4. Partial Link text
  5. Tag name
  6. Class name
  7. CSS selector
  8. Xpath
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜