I'm using Selenium 2 to test the user interface of my website. I need to check that clicking a button yields some result. In 1 in every 10 or so test runs, Selenium fails to even click the button. There are no errors printed out. There is nothing wrong with the JS because I can manually click the button after Selenium fails to click it.
At the very simplest, my HTML looks like this:
<html>
<head></head>
<body>
<a id="button" onclick="clickIt(); return false;"></a>
<script type="text/javascript" src="javascript.js"></script>
</body>
</html>
JS is placed at the end of body so a slow loading JS file wouldn't block the entire page. The JS file defines the clickIt function.
function clickIt() {
$('button').addClassName('beenClicked');
}
Java:
WebElement button = driver.findElement(By.xpath("//a[@id='button']"));
button.click();
My hypothesis on why it fails 1 out of 10 times is that Selenium is not waiting for the JS file to load before clicking the button. If this is true, how do I wait for this JS file to load and execute?