1

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?

3 Answers 3

3

Look into WebDriverWait

Check to see if a function is there

JavascriptExecutor js = (JavascriptExecutor) driver; 
Boolean isLoaded = (Boolean)js.executeScript("return typeof window.yourFunctionName === 'function'", null);
Sign up to request clarification or add additional context in comments.

6 Comments

I know about WebDriverWait, but how do it check for the existence of the definition of a JS function?
USe the function to check to see if it is there.
If I use the function and it does not produce any results, how will I know whether it is the JS not loaded or the JS poorly written?
Write a check to see if the function is there. Edited my question above [untested]
I've been trying to get your JavascriptExecutor to work, but I've been getting a null pointer exception. I decided to leave out the null argument on executeScript because Eclipse complains.
|
0
JavascriptExecutor js = (JavascriptExecutor) driver; 
Boolean isLoaded = (Boolean)js.executeScript(
      "return typeof window.yourFunctionName === 'function'", null);

Comments

0
public boolean waitForJStoLoad(WebDriver driver) {
    WebDriverWait wait = new WebDriverWait(driver, 30);

    // wait for jQuery to load
    ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            try {
                return ((Long)((JavascriptExecutor) driver)
                        .executeScript("return jQuery.active") == 0);
            }
            catch (Exception e) {
                return true;
            }
        }
    };

    // wait for Javascript to load
    ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            return ((JavascriptExecutor) driver)
                    .executeScript("return document.readyState")
                    .toString().equals("complete");
        }
    };
    return wait.until(jQueryLoad) && wait.until(jsLoad);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.