Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: intermittent "element not found" issues

Tags:

selenium

Every now and then my selenium tests randomly fail with an "element X not found" error message. I do a simple select by ID, eg.

click('sideBar_queryButton')

When I use the dom inspector, the element is there, so I wonder why selenium doesn't find it sometimes. When I run the same test again, it works or it breaks again, there seems to be no reliable way of reproducing it. Even tests which are there for ages seem to randomly break and then magically work again. Inserting a few sleep statements sometimes helps but not reliably. So I wonder if I'm using it incorrectly. Has anyone else had these problems with selenium and if so, how did you fix them?

like image 355
Jan Thomä Avatar asked May 11 '11 08:05

Jan Thomä


1 Answers

Edit: I found it to be much more reliable to put some test markers in my pages and wait for them to appear. If you use asynchronous operations which might create race conditions in your tests, inserting a test marker into your html after you finish the operation worked pretty well for me. E.g.

$('<div>').addClass("testMarker").append("OpXYZFinished").appendTo($('#content'));

That way, you can do a simple "waitForTextPresent" to see if things worked out and its much more reliable than guessing the browser's loading state. The testmarker class needs to be formatted in a way that it is not visible to the user (e.g. font color == background color).

Thanks for all your comments. After some deeper digging on the net and in our tests I finally found combining these statements instead of a simple waitForPageToLoad to be the cure for our issues:

waitForPageToLoad('')
// wait until all ajax activity has ceased. That check's jQuery's $.active
waitForCondition('selenium.browserbot.getUserWindow().$.active == 0', 5000)
// wait a second for all JS to properly initialize
pause(1000)

There is still a pause in there which is somewhat ugly, but it does the trick.

like image 86
Jan Thomä Avatar answered Jan 04 '23 12:01

Jan Thomä