Search code examples
javaeclipseseleniumselenium-rc

How to use waitForCondition()


Good day,

I'm new to selenium and I'm trying to write a simple test. I want to test my AJAX functionality so I thought that the waitForCondition() method was the right fit for me. Problem is, I get an error every time, saying that 'testLoop' is not defined. My test is simple:

@Test
public void firstTest() 
{
    selenium.open(BASE_URL);
    assertTrue(selenium.isElementPresent("css=a#performAjax"));
    selenium.click("link=Add some content!");
    String script = "selenium.browserbot.getCurrentWindow().document.getElementByClassName('ajaxMessage').length > 0";
    selenium.waitForCondition(script, "10000");
    Assert.assertEquals(selenium.getText("css=div.ajaxMessage"), "You got me with AJAX!");
}

But every time I run it, I get the error:

com.thoughtworks.selenium.SeleniumException: ERROR: Command execution failure. Please search the forum at http://clearspace.openqa.org for error details from the log window. The error message is: testLoop is not defined

I've searched forums, etc, for an answer to this but I've found nothing helpful thus far. Any tips are appreciated.

Other tips regarding better ways to write selenium tests are also welcome, but my main goal is to get around this error :)

In case anybody is wondering, yes, I've verified that the error is thrown during my call to waitForCondition().


Solution

  • Some more searching did the trick! For anyone else who is wondering...

    A user commenting on this post mentions that the current implementation of the waitForCondition() extension is out of sync with the current version of selenium. The user-extensions.js file must be updated to replace all instances of 'testLoop' with 'TestLoop'.

    If you've imported selenium-rc as an external jar into Eclipse like I did, this is a matter of decompressing the jar, editing the js file, and re-compressing it into a jar.

    Update:

    After further research, it seems that waitForElementPresent() is the preferred way to do this, as you can simply specify a locator rather than evaluating a JavaScript expression. There is also waitForTextPresent() and other equivalents.

    These AJAX methods are not present in the .jar files that you get with selenium-rc. They are available in Selenium-IDE, and if you export these tests to Java, you will see that it is simply creating a loop with a default timeout of 60 seconds and checking for isElementPresent().

    If you prefer to work strictly from Java-driven mode, you can probably create some helpers to do this work for you, since unzipping the jar, modifying user extensions, and re-zipping the jar gets rather tedious and annoying.