8

I have been running into intermittent errors with some java selenium-rc tests which I think are related to a page which has an ajax poll and automatically refreshes when some condition is reached on the server. In this scenario, I have no way of asking selenium to wait for the page to load, and so I run into a bunch of random "Couldn't access document.body" errors.

So, is there some way I can cause selenium to gracefully handle this situation? If not, is there some way I could detect whether the user is selenium from the page's javascript, and disable the automatic refresh?

If it helps at all, the javascript code in the page looks something like...

var ajax = new Ajax(url, { 
    update: state,
    method: 'get',
    onComplete: function(message) {
        if (some_condition) {
            window.location.replace(unescape(window.location));
        }
    }
});
0

3 Answers 3

8

In this scenario you could wait for the Ajax request to finish.

That way you don't concern yourself if the page is loaded. You continue after the ajax request is done.

You could do that this way:

        selenium.waitForCondition(
            "selenium.browserbot.getCurrentWindow().jQuery.active == 0",
            timeout);

Where timeout is an optional integer parameter.

5
  • Was writing an answer as you posted this. I think that I'm going to see about implementing your solution in my own automation as this seems much more efficient. Commented May 6, 2011 at 11:12
  • Would that just wait for the ajax request and the onComplete handler to finish, or would it wait until the new page (which is loaded by window.location.replace) is full loaded? Commented May 6, 2011 at 11:12
  • @Lyndon: Thanks man. :) @Matt: This actually waits for EVERY jQuery request to finish. It waits until everything jQuery does on your page is done, even for a microsecond. If jQuery get's in the state: active=0 your program will continue. This of course is only valid for jQuery, but you can edit this to represent whatever tool you are using. Btw if you are not using jQuery, shame on you you should. :D
    – Hannibal
    Commented May 6, 2011 at 11:26
  • Thanks - I'll try it out. I'm still not sure it will work though, because I would have assumed that when the window.location.replace started loading a new page, the old jquery context would need to be destroyed. Assuming that happens, jquery wouldn't actually become active again unless the new page which is being loaded used it for something, and even then there would be points during the page loading where it was inactive. That said, I'm not sure so I'll test it out and see if you're right that jQuery.active survives through a page load. Commented May 8, 2011 at 11:18
  • Yeah i hope it helps. It's just an idea after all. I really hope that will help you get from there at least.
    – Hannibal
    Commented May 9, 2011 at 7:08
5

If Hannibal's answer doesn't work, what has worked for me with pages that take a very long time to load, is adding calling a method either directly after the action which causes page to be refreshed, or, at the beginning of the next action, which checks to see if a particular element which will always be present on the page, is actually present.

2

The simple solution would be to have selenium execute a script to override/disable the javascript that self refreshes.
selenium rc is selenium 1, but regardless, javascript can be executed with both drivers.

That aside, @Hannibal has a pretty good solution, and I would most likely do it that way myself

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.