2

我试图在循环中使用 isVisible() 为我的 iOS UIAutomation 创建一个 waitForElement 类型的函数。当我尝试使用以下代码时,在弹出新屏幕时等待元素时失败。该元素显然存在,因为如果我在点击该元素之前执行延迟(2),它可以正常工作。其他人是如何做到这一点的,因为我不知所措......

这是我正在使用的 waitForElement 代码:

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

4 回答 4

1

这是一个可以使用的简单 wait_for_element 方法:

this.wait_for_element = function(element, preDelay) {
    if (!preDelay) {
        target.delay(0);
    }
    else {
        target.delay(preDelay);
    }

    var found = false;
    var counter = 0;      
    while ((!found) && (counter < 60)) {    
        if (!element.isValid()) {
            target.delay(0.5);
            counter++;
        }
        else {
            found = true;
            target.delay(1);
        }
    }
}
于 2011-08-16T18:20:29.337 回答
1

我倾向于远离我的 wait_for_element 并在屏幕上寻找任何 activityIndi​​cator 对象。我使用这种方法来实际等待页面加载。

this.wait_for_page_load = function(preDelay) {        
    if (!preDelay) {
        target.delay(0);
    }
    else {
        target.delay(preDelay);
    }

    var done = false;
    var counter = 0;      
    while ((!done) && (counter < 60)) {
        var progressIndicator = UIATarget.localTarget().frontMostApp().windows()[0].activityIndicators()[0];
        if (progressIndicator != "[object UIAElementNil]") {
            target.delay(0.25);
            counter++;  
        }
        else {
            done = true;           
        }
    }
    target.delay(0.25);
}
于 2011-08-16T18:28:33.377 回答
0

这是一个使用递归的简单且更好的方法。不需要“返回真”,但如果你想要它。

waitForElementToDismiss:function(elementToWait,waitTime){ //Using recursion to wait for an element. pass in 0 for waitTime 

    if(elementToWait && elementToWait.isValid() && elementToWait.isVisible() && (waitTime < 30)){
        this.log("Waiting for element to invisible");
        target.delay(1);
        this.waitForElementToDismiss(elementToWait, waitTime++);
    }

    if(waitTime >=30){
        fail("Possible login failed or too long to login. Took more than     "+waitTime +" seconds")
    }

    return true;

}
于 2013-10-15T20:00:42.940 回答
0

解决方案

我知道这是一个老问题,但对于我必须针对可变定时事件执行重复任务的情况,这是我的解决方案。由于 UIAutomation 在 javascript 上运行,我使用了一个带有空 while 循环的递归函数,该函数在进入下一个屏幕之前检查所需的关键控制状态。这样一来,人们就不必对延迟进行硬编码。

// Local target is the running simulator
var target = UIATarget.localTarget();
// Get the frontmost app running in the target
var app = target.frontMostApp();
// Grab the main window of the application
var window = app.mainWindow();
//Get the array of images on the screen
var allImages = window.images();

var helpButton = window.buttons()[0];
var nextButton = window.buttons()[2];

doSomething();

function doSomething ()
{   
    //only need to tap button for half the items in array
    for (var i=0; i<(allImages.length/2); i++){
        helpButton.tap();
    }

    //loop while my control is NOT enabled
    while (!nextButton.isEnabled())
    {
        //wait  
    }   

    //proceed to next screen
    nextButton.tap();

    //go again
    doSomething();
}
于 2013-12-08T15:12:19.273 回答