在 Xcode UIAutomation 和 Instruments 中使用 Javascript 验证按钮对象是否存在

发布于 2024-10-19 23:40:23 字数 330 浏览 6 评论 0原文

为了在 iOS 4.2 上对我们的项目进行质量测试,我们正在通过 Xcode 3.x 中的 Instruments 使用 UIAutomation。我们正在用 Javascript 编写脚本。我是 Javascript 的新手,并且发现 UIAutomation 文档是(我该怎么说?)“稀疏”。

我希望以太坊中的一些天才能够启发我,当我们的iOS应用程序的主窗口上显示一个名为“beep sound”的按钮时,如何验证它是否存在?

还有人找到了用 JavaScript 编写测试脚本(而不是动态网页)的好参考吗?

感谢您的任何和所有帮助!

问候,

史蒂夫·奥沙利文

For the quality testing of our project on iOS 4.2, we are working with UIAutomation though Instruments in Xcode 3.x. We are writing our scripts in Javascript. I am new to Javascript, and have found the UIAutomation documentation to be (how shall I put this?), "sparse".

I am hoping that some genius in the Ether may be able to enlighten me as to how to verify the existence of a button named 'beep sound' when it is displayed on the main window of our iOS application?

Also has anyone out there found any good references for writing test scripts (as opposed to dynamic web pages) in JavaScript?

Thanks for any and all assistance!

Regards,

Steve O'Sullivan

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

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

发布评论

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

评论(1

半透明的墙 2024-10-26 23:40:23

嘿。
实际上文档(this这个)来自苹果是我唯一的东西可以找到。
至于你的问题尝试

if(UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].name() === "beep sound")) {
    UIALogger.logPass("Buton Present");
} else {
    UIALogger.logFail("Buton Not Present");
};

当然,假设(elements()[0])你的按钮位于主窗口下的对象树中的第一个。如果不是,您可能需要调用其他元素 ((elements()3),或者您可能需要更深入地调用层次结构(elements()[0].elements()3)。
请记住,如果链中的对象之一不存在,上面的代码将会失败。您可能需要检查链中的每个对象。此外,您可能需要检查给定的按钮是否不仅存在,而且在屏幕上是否可见。在这种情况下,上面的代码可能需要如下所示:

if(UAITarget.localTarget().frontMostApplication().mainWindow() && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0] && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].withPredicate("name matches 'beep sound'")) {
    if(UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].isVisible()) {
        UIALogger.logPass("Buton Present");
    } else {
        UIALogger.logFail("Buton Present, but Not Visible");
    }
} else {
    UIALogger.logFail("Buton Not Present");
};

但现在代码的可读性、可维护性和过度属性受到了影响。所以我将其重构为:

function isButtonWithPredicate (predicate) {
    if(UAITarget.localTarget().frontMostApplication().mainWindow() && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0] && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].withPredicate(predicate)) {
    return true;
} else { 
    throw new Error("button not found, predicate: " + predicate);
}

function getButtonWithPredicate (predicate) {
    try {
        if(isButtonWithPredicate(predicate)) {
            return UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].withPredicate(predicate);
        }
    } catch (error) {
        throw new Error("getButtonWithPredicateError: " + error.message);
    };
}


var strpredicate = "name matches 'beep sound'";
var objButton = null;
try{
    objButton = getButtonWithPredicate(strPredicate);
    if(objButton.isVisible) {
        UIALogger.logPass("Buton Present");
    };
} catch(error) {
    UIALogger.logFail(error.message);
}

当然你仍然可以改进它......但你应该明白这个想法。

顺便说一句 Apple 谓词指南

PS代码是用记事本编写的,未经过检查,因此可能包含一些解析错误。

Hey.
Actually documentation (this and this) from apple is the only thing I could find.
As for your question try

if(UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].name() === "beep sound")) {
    UIALogger.logPass("Buton Present");
} else {
    UIALogger.logFail("Buton Not Present");
};

Of course, that assumes (elements()[0]) that your button is first in object tree under main window. If it is not, you may need to call other element ((elements()3), or you may need to call deeper into hierarchy (elements()[0].elements()3).
Keep in mind that code above will fail if one of the objects in chain won't be present. You may need to check every object in chain. Additionally you may need to check if given button is not only present but if it visible on the screen. In this case code above may need to look like this:

if(UAITarget.localTarget().frontMostApplication().mainWindow() && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0] && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].withPredicate("name matches 'beep sound'")) {
    if(UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].isVisible()) {
        UIALogger.logPass("Buton Present");
    } else {
        UIALogger.logFail("Buton Present, but Not Visible");
    }
} else {
    UIALogger.logFail("Buton Not Present");
};

But now readability, maintainability, and over -ity attributes of code suffer. So I would refactor it to:

function isButtonWithPredicate (predicate) {
    if(UAITarget.localTarget().frontMostApplication().mainWindow() && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0] && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].withPredicate(predicate)) {
    return true;
} else { 
    throw new Error("button not found, predicate: " + predicate);
}

function getButtonWithPredicate (predicate) {
    try {
        if(isButtonWithPredicate(predicate)) {
            return UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].withPredicate(predicate);
        }
    } catch (error) {
        throw new Error("getButtonWithPredicateError: " + error.message);
    };
}


var strpredicate = "name matches 'beep sound'";
var objButton = null;
try{
    objButton = getButtonWithPredicate(strPredicate);
    if(objButton.isVisible) {
        UIALogger.logPass("Buton Present");
    };
} catch(error) {
    UIALogger.logFail(error.message);
}

Of course you can still improve it... but you should get the idea.

btw apple guide to predicates

P.S. Code was written in notepad, and wasn't checked so it may contain some parse errors.

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