Thursday, June 4, 2009

Help! My Selenium tests are flaky!

In my development of Selenium tests, I have come to dread the oh-so-common "Permission denied" errors. A quick scan of google search results indicates that there is likely some cross-domain issue, and a suggested workaround is using an experimental browser, like *iehta. I generally use *iehta anyway, and still seem to have problems with the "Permission denied" error.

In my most recent experience, testing against what seems to be an inconsistently slow and sometimes flaky web app, I have come to expect that "Permission denied" usually indicates that an element that I am trying to access is not yet accessible. For my app, which is heavily AJAXed, "waitforpagetoload" doesn't cut it. Since the AJAX scripts are still running, Selenium would wait until the end of time (or the timeout, whichever comes first) for the page to load and it would not.

So, I found a few people who solved this problem by creating a function that would wait for a specific element to load. I decided to go with it, and now my "WaitForElement" function is standard in my local Selenium Template for Visual Studio.

This function looks like this (this is C# code):

private void WaitForElement(string elementName)
{
for (int second = 0; ; second++)
{
if (second >= 120) Assert.Fail("timeout");
try

{
if (selenium.IsElementPresent(elementName)) break;
}
catch (Exception e) { }
Thread.Sleep(1000);
}
}


I call this function the first time I am going to take action after the web app has has to re-load or re-render elements. I tend to pass it the element which I am wanting to interact with first.

So in my test, the steps tend to look like this:
selenium.Click("btn1");
WaitForElement("txtBox1");
selenium.Type("txtBox1", "username");

I have seen some other suggestions from people on stabilizing selenium tests, but would love to hear from anyone else who has worked out a way to make their selenium tests less fail-prone.

2 comments:

M said...

Dawn.
We have a group on Linkedin that can hopefully help you out called "Selenium User Group".
We have over 600 members..

..and all with Selenium issues .. lol. hope this helps.

TestWithUs said...

SWIFT Interview questions on

http://testwithus.blogspot.in/p/swift.htm

For selenium solution visit
http://testwithus.blogspot.in/p/blog-page.html


For QTP interview questions

http://testwithus.blogspot.in/p/qtp-questions.html


www.searchyourpolicy.com