Selenium 2 Tips
by Luke Daffron Friday, November 11th, 2011
In previous posts, I described our use of Selenium for functional and regression testing – and I included some tips on how to use it effectively. We used the Firefox plugin Selenium IDE to run our tests.
Since that time, we’ve moved on to use Selenium 2 (now, Selenium Server) – which uses a completely different architecture built on top of a merged project called WebDriver. Now, instead of the custom ‘selenese’ scripts – our tests are driven with C#. This allows for much more effective branching, looping, etc. scenarios that are sometimes necessary for robust testing.
Some of the tips for selenese tests still apply, but in addition here are some specific Selenium 2 Server pointers:
- If you are having trouble getting an element clicked, sometimes it helps to have the test explicitly move the mouse to the element beforehand. Use:
new OpenQA.Selenium.Interaction.Actions(thewebdriver).MoveToElement(theelement).Perform();
- Selenium 2 will not interact with elements that are hidden or off screen. Because of this – each click/etc action implicitly performs a scroll-to-element action. Usually, this makes things easier, but occasionally it breaks. If you have a scrollable element with tight spaces, it might scroll it just out of range before the click, and it will silently fail. There currently isn’t a great way around this in the test – you can attempt to change your site to deal with it instead (by giving more room, or locking scrolling, etc).
- It can be a hassle to deal with nested frames.
- Things will fail if you don’t keep the Selenium context updated. XPath selections in FireFox will throw exceptions, and events won’t fire. Make sure you do the following to always use the proper context:
Driver.SwitchTo().DefaultContent().SwitchTo().Frame("fremename");
- Things will fail if you don’t keep the Selenium context updated. XPath selections in FireFox will throw exceptions, and events won’t fire. Make sure you do the following to always use the proper context:
- When things are failing differently on different machines, there are a couple of things to try:
- Set a consistent resolution at the beginning of the test:
((IJavaScriptExecutor)Driver).ExecuteScript("window.moveTo(0, 1); window.resizeTo(1200,1000);"); - Retry clicks until success. This tends to be necessary right after a iframe context change. Hacky, but this psuedocode handles some performance/timing issues:
do{click;sleep;}while(testforchange){}
- Set a consistent resolution at the beginning of the test:
Tags: Selenium 2, selenium server, selenium2, webdriver
« Datagrid Checkbox Column iText SimSun Degree Symbol Spacing »

