Friday, September 23, 2011

Selenium Tips for better automation

Selenium provides a powerful and yet simple way to write and execute automated browser tests but poorly designed tests can add a lot of maintainability headache. Here are some tips that I have learnt using Selenium.

1. Use CSS Selectors rather than XPATH
They are faster and much more readable. Don't just take my word for it, read more at this blog.

2. Use UI Map for storing UI object locators
The idea here is to use a central place to store all the UI object locators of application under test. This is advantageous in two ways. This makes the logic in the tests cleaner thereby easier to read and less prone to modifications when application UI changes. The maintainability improves as there is only one place to make changes when application UI does change which happens all the time.

3. Use waitForCondition instead of timeout
In the Web 2.0 world we find a lot of pages using dynamic elements that load asynchronously without loading the parent page. The classic method waitForPageToLoad fails miserably and very often people resort to using certain timeout assuming the element will be available after that. This technique is very inefficient and error prone. Instead use the waitForCondition. The method takes two arguments - a javascript snippet that evaluates to a boolean value and a timeout in milliseconds. The javascript snippet is run repeatedly until it evaluates to "true" or until the timeout is reached after which the command returns an error.

4. Write user extensions to extend selenium api for your application
Sometimes its more efficient to have Selenium execute your custom Javascript functions natively. Extending selenium to add your own actions, assertions and locator strategies is easy, more information can be found in the official selenium documentation here.

No comments:

Post a Comment