iOS UIAutomation UIAElement.isVisible() 抛出过时的响应?

发布于 2024-11-29 05:21:12 字数 692 浏览 0 评论 0原文

我试图在循环中使用 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");
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

网名女生简单气质 2024-12-06 05:21:12

这是一个可以使用的简单 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);
        }
    }
}

Here is a simple wait_for_element method that could be used:

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);
        }
    }
}
月寒剑心 2024-12-06 05:21:12

我倾向于远离 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);
}

I tend to stay away from my wait_for_element and look for any activityIndicator objects on screen. I use this method to actual wait for the page to load.

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);
}
原谅我要高飞 2024-12-06 05:21:12

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

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;

}

Here is a simple and better one using recursion. "return true" is not needed but incase u want it.

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;

}
ぇ气 2024-12-06 05:21:12

解决方案

我知道这是一个老问题,但这是我针对必须针对可变定时事件执行重复任务的情况的解决方案。由于 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();
}

Solution

I know this is an old question but here is my solution for a situation where I have to perform a repetitive task against a variable timed event. Since UIAutomation runs on javascript I use a recursive function with an empty while loop that checks the critical control state required before proceeding to the next screen. This way one never has to hard code a delay.

// 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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文