Validate element txt or attribute value - optimize
For Selenium RC...but may apply to Web Driver?
I was wondering, in cases where one is using or has to use XPath or CSS locators anyways, does it make more sense to do a 2 step validation of an element attribute's value or element's text, or a one step inline validation?
For example, with name/ID based locators or any locator: step 1, extract text or attribute value of element via getText or getAttribute, then step 2 compare actual against expected values.
Versus, with CSS or XPath locator, embedded the value you want to validate into locator and verify element exists. No need to separately compare actual vs expected. If it didn't match, it won't exist.
//someAdditionalXpath/div[@id='someId' and text()='text to verify']
//someAdditionalXpath/div[@id='someId' and text()[contains(.,'text to verify')]]
//someAdditionalXpath/div[@id='someId' and contains(@myAttribute,'value to verify')]
css=div#someId:contains('text to verify')
css=div[@myAttribute *='value to verify']
I guess it makes more sense to do 2 step process in terms of testing standards and code portability as you can then use any locator type and can better define static contants for locators, etc. Perhaps more readable for testers across locator types.
But in terms of programmer sense, the inline method is more optimal in reducing lines of code to write and read. Might even be a bit faster for non-XPath case. And for the XPath case, if you had to use XPath anyways, adding more to check in the XPath won't likely slow it down all that much more.
What do you think?
Re: Validate element txt or attribute value - optimize
I go with approach 2 (though not sure of coding standard) as it makes check more succinct.