Wednesday, July 21, 2010

Selenium, XPath, and Firebug

Armed with Selenium, Xpath, and Firebug you're ready to automate almost any web test case. Looking at the Selenium Reference guide most of the actions take a locator. The easiest locator to use is the id attribute of the element you are performing the action on. If you or you're team are developing with automated web tests in mind, then there should be an id on all the significant elements. If there is no id on the element, don't fret you can use my next favorite locator: XPath.

Here's a tutorial to XPath if you are not familiar with the language.

...

Okay so now you've got your selenium script and the xpath of that link inside the table you want to click (for example). You run your selenium script and it errors saying the element you're trying to click does not exist or could not be found. You go back to your script and try to figure out what's wrong with your XPath expression

selenium.click("//table/td/a[contains(.,'Important link')]");

In my head I'm thinking "Click on the cell that contains the link that says Important link". What the heck is wrongs with this code?!? After trial and error you figure out it's missing the tr in the path

selenium.click("//table/tr/td/a[contains(.,'Important link')]");

When I first started with XPath and Selenium I would spend hours developing the XPath expression, run the selenium test, watch it fail on the XPath command, edit it, and repeat till it worked. This was an inefficient use of time and brain power.

That's when Firebug came in to save the day. Firebug has an awesome console where you can do javascript debugging and object inspection. Using the console you can test your XPath expression and determine if the element actually exists before putting it into your selenium script. To use it navigate to the page you want to test and then open up firebug. Click on the console tab, then click on the little red triangle pointing upward to expand the console. Inside the console you can write javascript to evaluate your XPath expression. Fortunately firebug comes with a built in function that evaluates XPath expressions, $x.

Here's the snippet of code I use

var node = $x( [your xpath expression here] );
console.dir(node);

Click run and if your XPath is valid it should return the dom element allowing you to inspect it. If your XPath is invalid it won't return anything. Here's a screen shot





Now you can test your XPath independently of your selenium script and be sure that your locator is correct. This will save you lots of time and increase your confidence in what your selenium actions are doing.

32 comments:

  1. The future of automation testing tool is on the positive tool. As the demand of quality web application keeps on increasing, the tool like Selenium IDE is getting very popular all over the world. Thus, taking Selenium Training in Chennai will help you to enter software testing industry.Best Selenium training institute in Chennai

    ReplyDelete
  2. The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers. Software testing training|Software training|Software testing training in chennai

    ReplyDelete
  3. Excellent article!!! LoadRunner is popular automation testing tool used for validating a software application/system under load. It delivers most precise information about the performance, functionality and behavior of the software product. Loadrunner Training in Chennai | Loadrunner training Chennai|Qtp training Chennai

    ReplyDelete
  4. Excellent sharing. Testing is a wonderful field for IT peoples. Want to learn Selenium Online Training reach GangBoard. Hadoop Online Training

    ReplyDelete
  5. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it. erp in chennai.

    ReplyDelete
  6. Great content thanks for sharing this informative blog which provided me technical information keep posting.Android Training Institute in Chennai | Android Training Institute in Velachery

    ReplyDelete

  7. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.


    Selenium Training in Jayanagar

    ReplyDelete
  8. Fantastic site. Lots of helpful information here. I¡¦m sending it to several pals ans additionally sharing in delicious. And naturally, thanks on your effort!
    Good Vecation classes in Chennai | Best Summer Course in Guindy

    ReplyDelete
  9. Great information on this blog, many useful and new ideas i'm learning here.It's very helpful....
    Good Technical Boot Camp in Chennai | Best Summer Course in Porur

    ReplyDelete
  10. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.

    software testing course in chennai

    ReplyDelete
  11. Impressive blog with lovely information. really very useful article for us thanks for sharing such a wonderful blog...
    Summer Courses in Perungudi | Summer Courses in OMR | Summer Courses in Velachery

    ReplyDelete
  12. Superb explanation & it's too clear to understand the concept as well,thank you for your sharing...
    No.1 Summer Technical Boot Camp in Chennai | Summer Courses for Hardware and Networking in Velachery

    ReplyDelete
  13. Impressive blog with lovely information. really very useful article for us thanks for sharing such a wonderful blog... Excellent Summer Courses for Business Administration in Chennai | Best Vacation Classes in Chennai

    ReplyDelete
  14. Good and nice information, thanks for sharing your views and ideas..keep rocks and updating.
    Blue prism Exams in Chennai | Blue prism Exam Centers in Chennai | Blue prism Certifications in Chennai

    ReplyDelete
  15. Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on updating...
    Python Certification Exam Training in Chennai | Best Python Training in Guindy

    ReplyDelete
  16. Well Said, you have furnished the right information that will be useful to anyone at all time.
    ISTQB Certifications Exam institute in Chennai | QA Testing in Sholinganallur

    ReplyDelete
  17. Great Article...Thanks for sharing the best information

    Android Training

    ReplyDelete
  18. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    ECE Project Center in Chennai | BE Project Training in Velachery

    ReplyDelete
  19. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    Digital Marketing Training

    ReplyDelete