4

I'm trying to use isVisible() within a loop to create a waitForElement type of a function for my iOS UIAutomation. When I try to use the following code, it fails while waiting for an element when a new screen pops up. The element is clearly there because if I do a delay(2) before tapping the element it works perfectly fine. How is everyone else accomplishing this, because I am at a loss...

Here's the waitForElement code that I am using:

function waitForElement(element, timeout, step) {
        if (step == null) {
                step = 0.5;
        }

        if (timeout == null) {
            timeout = 10;
        }

        var stop = timeout/step;

        for (var i = 0; i < stop; i++) {
                if (element.isVisible()) {
                        return;
                }
                target.delay(step);
        }
        element.logElement();
        throw("Not visible");
}
1
  • What if you check for existence before checking for visibility: if exists {if visible: return} Commented Oct 23, 2012 at 19:52

2 Answers 2

2

If your page reloads during waitForElement function execution then you have to refresh element by finding it once again. Try to use selector as parameter an not the element and use findElement function:

function waitForElement(selector, timeout, step) {
        if (step == null) {
                step = 0.5;
        }

        if (timeout == null) {
            timeout = 10;
        }

        var stop = timeout/step;

        for (var i = 0; i < stop; i++) {
                if (driver.findElement(selector).isVisible()) {
                        return;
                }
                target.delay(step);
        }
        element.logElement();
        throw("Not visible");
}
0

This waiting should be handled for you. You can specify isVisible using a predicate or withValueForKey Try this ...

target.pushTimeout(timeout); element.withValueForKey(1, "isVisible"); target.popTimeout();

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.