NOTE: You might have been redirected here. Don’t worry! I’ve moved my active blog to 40. (with egg)

We’re trying to use Selenium to test our ajax-ruby-on-rails projects (a-rails? r-jax? rail-jax? We gotta get some marketing folks on this…) We’ve ended up extending Selenium via their extension framework to write our own custom asserts and such, and things are going pretty well, though we’ve had to spelunk into the Selenium code quite a bit. One of the biggest problem has been the Ajax – since we don’t know when our app is done doing it’s thing (that would be the asynchronous part) the test cases would charge ahead, testing results that weren’t there yet. Selenium has many “AndWait” functions that are handy for synchronous actions: clickAndWait, openAndWait, getMeAChantico™WithAShotAndWait, etc., but they often do not work well for Ajax, since the asynchronous nature of the action does not lend itself (for us anyway) to have an action “complete” in the traditional submit-reply paradigm.

Luckily, Selenium has a feature that is horribly documented: waitForValue, which you can use in the test case directly, and waitForCondition, callable in a JavaScript-based extension. Using a combination of these, you can make your Ajax call and wait for any condition that should happen, such as the DOM changing or values updating. Why the Selenium folks don’t have bright red “hey, use this for your Ajax testing!” flag on these two items is beyond me. We’ve only just had our first success using this and I’ll write more details later.


8 Comments (from old blog):

At 11/30/2005 2:04 PM, Jason Huggins said…

That’s it– I’m firing my marketing department. Seriously though, thanks for your post and we’ll try to do a better job at getting the word out about testing Ajax with Selenium.

Jason “the guy who wrote Selenium” Huggins


At 12/05/2005 11:48 PM, Joseph Moore said…

Hi Jason – I hope I didn’t come across as harsh! We’re having a lot of fun with Selenium and writing more tests every day, and staring to use it to test other projects. I’ll admit that my Ajax knowledge is still green (as in young, not as in not-red :) ) so I was surprised when I found that waitForValue and waitForCondition were exactly what we were looking for.

Also, I have a history of making a fool of myself: a few years ago I was at the Eclipse BOF at JavaOne when I started talking to a guy running a demo. He was showing off a Swing GUI builder that looked very much like VisualAge for Java’s GUI builder, and I mentioned this. “Oh yeah, what did you think of the VisualAge tool?” He asked, provoking a major rant from me about how I hated VAJ’s Swing tools.

“Oh,” he said. “I wrote it”.

Find rock. Crawl under. Hide.


At 12/08/2005 3:17 PM, Dan Fabulich said…

Where exactly did you find the waitForCondition user extension?


At 12/08/2005 7:01 PM, Joseph Moore said…

waitForCondition is implemented in Selenium’s JavaScript; it’s actually how they implemented waitForValue.

Take a look at Selenium’s API for waitForValue: selenium-api.js, line 508. For examples of how they used it, do a search for waitForCondition on in their source. For example, selenium-executionloop.js, line 81:

      this.lastCommandResult = result;
          if (result.processState == SELENIUM_PROCESS_WAIT) {
              this.waitForCondition = function() {
              return selenium.browserbot.isNewPageLoaded();
          };
      }

At 12/16/2005 11:38 AM, Dan Fabulich said…

Thanks. I’ve taken your suggestion and baked it into a packaged user extension, including a new feature allowing you to specify a timeout for how long you want to wait:

http://wiki.openqa.org/display/SEL/waitForCondition


At 12/18/2005 5:01 PM, Joseph Moore said…

Hi Dan –

Nice! We’ll have to download it and give it a try

– Joe


At 6/16/2006 9:38 AM, DrydenMaker said…

Indeed. The wait function worls well. Selenium is certainly at the top of my testing tools list.


At 10/25/2006 1:50 PM, Evan Farrar said…

wait_for_condition(‘Ajax.activeRequestCount == 0’,’5000’)

3 Responses to “Using Selenium's waitForValue, waitForCondition for Ajax Tests”

  1. John Says:

    Check out webaii’s element identification, that if you are using .net. http://www.artoftest.com/Resources/WebAii/Quickstarts/Quickstarts.aspx

  2. Navid Says:

    Where can I see examples of how to use waitForCondition in selenese. I want to know what the JavaScript would look like.

    Thanks

    Navid

  3. Joe Says:

    @Navid – check out http://selenium-core.openqa.org/reference.html. If you download the selenium libraries, you should be able to find the JS inside.

Leave a Reply