admin管理员组

文章数量:1401629

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));
        }
    }
});

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));
        }
    }
});
Share Improve this question edited Feb 2, 2010 at 2:57 Matt Sheppard asked Feb 1, 2010 at 23:23 Matt SheppardMatt Sheppard 118k46 gold badges113 silver badges134 bronze badges 3
  • @Matt what changes when the page reloads – AutomatedTester Commented Feb 2, 2010 at 9:03
  • Some links which were disabled bee re-enabled - Nothing relevant to the test. Ideally we would update the page to the new state with ajax or something, but the effort required to do that was extensive pared to this page refreshing mechanism. – Matt Sheppard Commented Feb 3, 2010 at 0:33
  • Looks like I might be able to detect running under selenium with something like nerd.metrocat/2009/10/detecting-selenium or thoughtstoblog.blogspot./2009/09/…. I'll give that a go tomorrow... – Matt Sheppard Commented Feb 3, 2010 at 10:37
Add a ment  | 

4 Answers 4

Reset to default 1

One solution might be to always use a waitForCondition using isElementPresent before attempting to interact with the application under test. You could put the following method in a superclass to keep your tests more readable. Alternatively you could create helper methods for mon Selenium mands that perform this wait.

/** Waits for an element to be present */
public static void waitForElementToBePresent(String locator) {
    session().waitForCondition("var value = selenium.isElementPresent('" + locator.replace("'", "\\'") + "'); value == true", "60000");
}

You may also want to wait for the element to be visible, as waiting for it to just be present isn't always enough (imagine a textbox that is always present but hidden until a certain condition). You can bine this with the above method:

/** Waits for an element to be visible */
public static void waitForElementToBeVisible(String locator) {
    waitForElementToBePresent(locator);
    session().waitForCondition("var value = selenium.isVisible('" + locator.replace("'", "\\'") + "'); value == true", TIMEOUT);
}

Incidentally, the WebDriver (Selenium 2) team are working on having implicit waits, specifically to address AJAX issues where elements are not present immediately.

My solution was to disable the refresh in javascript by wrapping it in something like the following...

var isSeleniumActive = parent.seleniumAlert;
if (isSeleniumActive) {
    alert("Selenium");
} else {
    alert("Not selenium");
}

I'm not sure if the seleniumAlert function here is likely to sick around forever, so be aware if you're taking this that you may be relying on internal selenium implementation details of selenium.

There i was facing the same problem and i use a single line of code and it helps.

i was getting the error about the page is getting auto refresh plus this warning:

-1490472087141 Marionette WARN Using deprecated data structure for setting timeouts

all i use is

Thread.sleep(2000) 

and it worked for me.

I think that you can pause, or use a click and wait. There are a few good articles on the google. Good luck.

Edit for your ment:

How about the waitFor mand?

本文标签: javaHandling selfrefreshing pages from seleniumStack Overflow