Is your Web-App Selenium-Test Compatible?
By Pallavi Sharma
If you are trying to automate your web application using selenium, a few things which you should know before you start the quest. For most of the selenium commands a “target” is required which is laymen terms is the object in your web-application on which you wish to perform a desired action.
Selenium follows a location strategy to locate the element in your application which consists of four modes:
1. Locating by Identifier.
2. Locating by XPath/CSS
3. Locating by DOM
4. Location by CSS
You will find detail description of the above locating techniques on the selenium website which are explained well there [http://seleniumhq.org/]. But what not is explained is when to use what and the negatives of each locating strategy.
1. Locating by Identifier: To locate an element using an identifier means that the element must have either an “ID” attribute or “NAME” attribute which should be unique on that page. You may use “type” or “index” property in combination with these identifiers to help selenium uniquely identify the element.
Selenium is right in expecting that if will find an element uniquely by locating using an identifier. The W3-standards also states that the “ID” attribute has to be provided for all elements and it should be unique. But since the HTML has no inherent check on such slip-ups, most of the developers end up doing so in their web applications while developing forgetting that it has to undergo testing also.
So if you know before hand that “Selenium” is the automation tool which suits your web application scenario the best; ensure your developers haven’t made such mistakes.
2. Locating by XPath: To locate an element using an Xpath, is not a very straightforward solution but it helps immensely when you have to use multiple attributes to help locating an element. Xpath is a powerful way of using any attribute by which you would want the tool to find the element and perform action on it. It is useful mostly in the cases when the developers have used same name, ids across the website and didn’t cared for the W3-standards.
But if you can expect your developers to overlook W3 standards for unique IDs you may very well expect them to give you “unclosed” tags in your application! And if your web application has even a single unclosed tag, Selenium won’t be able to detect the element using the “XPath”.
So if the above is the locating method, you expect to find an element ensure you don’t have unclosed tags in the application.
3. Locating by DOM: The last resort to find an element and weakest one to. It is not advisable to use especially if your website undergoes too many changes. And also if the intention of testing your web site is functional/regression.
4. Locating by CSS: If for initial thoughts you are thinking that what if my web-app doesn’t have CSS will this work, then the answer is “Yes”. This locator type works whether you have CSS or don’t have CSS. It is faster than X-Path, and as stated on the Selenium website, experience users like to use the CSS way of locating an element. This also doesn’t works if the DOM is broken, and also is browser depended, as different browsers have different way of handling CSS.
To summarize the above, it is vital to do a W3 standard check on your website so that you can throw all issues back at your developers to fix, before you jump in to testing your website using Selenium. A good starting point is the, W3-validator [http://validator.w3.org/] available freely online. It clearly list down the issues like duplicate ids, unclosed tags and other slip-ups. Selenium is a powerful tool to use to test your web application across operating systems and browsers but if the DOM is broken than none of the locators will work, coz selenium using the browsers java script engine so the results will also depend on which browser you are using for the test.
It is not mentioned clearly on the website www.seleniumhq.org when to use what locator strategy, and it is kind of overwhelming information why they have provided all such locator categories to the users? Couldn’t there have been a simpler and straight forward way of handling elements using just a single locator type which works under any circumstance. I don’t know the answer to this yet, but maybe will have….
Till the next blog happy testing your sites with Selenium.
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.





July 13th, 2009 at 10:29 pm
One must read this blog, before testing any web application with selenium.
Nice and interesting post..
Keep up the Great work …
July 15th, 2009 at 1:38 am
Hey,
Great! work.
Regards
amit
July 26th, 2009 at 6:39 am
Hi Pallavi Sharma,
Could you please update on usage of css . As i have idea on usage of XPATH in selenium , But usage of css in selenium is not clear to me till now.Waitig for your reply.
Thanks,
Krishna
July 30th, 2009 at 10:08 pm
Hey Krishna
To use css as a locator is a bit complicated, you may need to go and read “http://www.w3.org/TR/css3-selectors/” [mentined on seleniumhq.org]
Lets take an example:
Now the locator for the abc text box becomes:
css=input[name=”abc”]
and for button def:
css=input[name=”def”]
Css locator is faster than XPath locator.
They also have something called JQuery locator which Tellirium uses. Have you ever tried it?
Krishna, thanks for your answers on the SeleniumRC issue.
August 16th, 2009 at 1:05 am
Good one. Keep up the good work.
August 31st, 2009 at 3:02 am
Can anyone say how to get CSS locator for an element in a webpage…
I need to convert all my xpath locators to CSS…
Waiting for your reply…
June 29th, 2010 at 9:41 pm
Buy:Lipitor.Aricept.Lipothin.Zetia.Advair.Benicar.Ventolin.Amoxicillin.Acomplia.Female Cialis.Female Pink Viagra.Buspar.Nymphomax.Seroquel.Zocor.Prozac.Lasix.Cozaar.Wellbutrin SR.SleepWell….
July 15th, 2010 at 11:10 am
Buy:Zyban.Actos.Prevacid.Synthroid.Human Growth Hormone.Accutane.Prednisolone.100% Pure Okinawan Coral Calcium.Valtrex.Arimidex.Lumigan.Mega Hoodia.Nexium.Retin-A.Zovirax.Petcam (Metacam) Oral Suspension….
July 20th, 2010 at 4:35 pm
Buy:Arimidex.Mega Hoodia.Zovirax.Accutane.Lumigan.Retin-A.Petcam (Metacam) Oral Suspension.100% Pure Okinawan Coral Calcium.Actos.Prednisolone.Synthroid.Nexium.Prevacid.Valtrex.Zyban.Human Growth Hormone….
August 29th, 2010 at 5:51 pm
Gucci http://fdragonghgo.BESTPARTSPLUS.INFO/tag/armani+Gucci+makeup/ : makeup…
Gucci…
August 29th, 2010 at 6:57 pm
bed http://odogf-fw.AACEHARDWARE.INFO/tag/Orthopedic+bed+dog/ : bed…
dog…
September 5th, 2010 at 1:33 am
Buy:Tramadol.Super Active ED Pack.Viagra Professional.VPXL.Levitra.Viagra Super Force.Propecia.Viagra.Soma.Cialis.Cialis Professional.Zithromax.Cialis Super Active+.Viagra Super Active+.Cialis Soft Tabs.Maxaman.Viagra Soft Tabs….
September 5th, 2010 at 9:52 pm
Buy:Synthroid.Retin-A.Petcam (Metacam) Oral Suspension.Actos.Mega Hoodia.Lumigan.Human Growth Hormone.100% Pure Okinawan Coral Calcium.Prevacid.Arimidex.Accutane.Zovirax.Zyban.Nexium.Valtrex.Prednisolone….
September 6th, 2010 at 4:45 pm
Buy:SleepWell.Lipothin.Zetia.Buspar.Aricept.Amoxicillin.Nymphomax.Benicar.Acomplia.Seroquel.Lasix.Cozaar.Female Cialis.Female Pink Viagra.Lipitor.Prozac.Ventolin.Advair.Wellbutrin SR.Zocor….