3

I'm working with Selenium and need to determine if jquery animations have completed before moving on to the next action (click, input etc etc).

I need to do this without altering any existing animation scripts already on the site - so I need to be able to monitor something in JQuery I can iterate over and when any/all animations have stopped, I can carry on.

Something like this would be ideal...

function jqueryWait(){
    while(jQuery.animation.queue > 0){
        // do nothing - wait
    }
    // return to selenium
    return true;
}
Phil Cooper
  • 3,083
  • 39
  • 63
  • possible duplicate of [Determining if JQuery effects are still executing](http://stackoverflow.com/questions/6661903/determining-if-jquery-effects-are-still-executing) – Neal Gokli Mar 14 '14 at 22:47

4 Answers4

2

Without knowing the full details of animation, couldn't you make this more generic by checking if there is anything jquery active going on like this:

function jqueryWait(){
    while(jQuery.active != 0){
        // do nothing - wait
    }
    // return to selenium
    return true;
}    

That is how i wait for any jquery ajax request to complete before moving on in my selenium tests

Bob Evans
  • 616
  • 6
  • 18
  • shouldn't it be while(jQuery.active != 0) { //Do Nothing } – CBRRacer Nov 03 '11 at 19:35
  • Sort of along the right lines, although we already have that check in place. To be more specific, we have a modal pop up that embeds an iframe. There are a series of animations to load the window and it's these i'm interested in checking for before I need to focus into the new iframe. – Phil Cooper Nov 04 '11 at 08:54
  • ok but where is the animation occuring in the new window? If so you may want to swich to that window then wait for my abobe suggestion or you could perhaps do something like this to wait for that window to appear then switch to that iframe `driver.switch_to_default_content while(len(driver.window_handles))<2) { #do nothing }` – Bob Evans Nov 09 '11 at 02:26
  • jQuery.active seems to just be a count of active ajax requests. It is not affected by animations. See: http://stackoverflow.com/questions/3148225/jquery-active-function#3148506 – Neal Gokli Mar 14 '14 at 22:45
1

The answer is in the linked question: use http://api.jquery.com/animated-selector/

If you know the specific element to check: $("#el").is(":animated")

If not: $(":animated").length == 0

You can, of course, wrap arbitrary javascript code in a custom wait by using return (boolean) ((JavascriptExecutor) driver).executeScript(scriptHere); as your wait condition

Community
  • 1
  • 1
Yamikuronue
  • 746
  • 8
  • 37
0

It's not officially documented as far as I know (and thus could change without warning), but it seems that $.timers.length will be non-zero as long as animations are running.

sethobrien
  • 969
  • 7
  • 13
-1

I don't think there is a way to do that without modifying the jquery animation scripts. If on animation complete it would add a class to the element with the animation then you could check if the class exists but other than that the only possible messy option is the good old Thread.Sleep(xxxxxx);

CBRRacer
  • 4,649
  • 1
  • 23
  • 27