Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

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.

Wednesday, July 6, 2011

TinyMCE and Selenium


TinyMCE is a platform independent web based Javascript HTML WYSIWYG editor control that can be embedded in web pages. Selenium is an open source automation framework that can drive tests on most web browsers.

The web page that I wanted to test using Selenium consisted of multiple tinymce editors used for various rich textareas. Internet Explorer 8 was the target browser. Obviously simply using the type command with input text did not work. So googled on how other people achieved this, found following two solutions:

1. Select frame and then type text

selectFrame id_of_tinymce_iframe
focus tinymce
type tinymce Text_text
selectFrame relative=parent
2. Identify the text area using DOM locator
type dom=document.getElementById('id_of_tinymce_iframe').contentWindow.document.body Test_text

Both of them seem to work on Firefox where I develop my test using Selenium IDE. But on IE 8 I would not see the "Test_text" in the TinyMCE textarea even though the steps would run fine. The way I ended up achieving this was by using the following:
focus dom=document.getElementById('id_of_tinymce_iframe').contentWindow.document.body
getEval selenium.browserbot.getCurrentWindow().document.getElementById('id_of_tinymce_iframe').contentWindow.document.body.innerHTML = 'Test_text';
Yes and this did work on IE 8.


Click here to read "Selenium tips for better automation".