【问题标题】:Verifying the existence of a button object using Javascript, in Xcode UIAutomation and Instruments在 Xcode UIAutomation 和 Instruments 中使用 Javascript 验证按钮对象的存在
【发布时间】:2011-03-01 23:02:35
【问题描述】:

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

我希望以太坊中的某个天才能够启发我如何验证在我们的 iOS 应用程序的主窗口上显示名为“哔声”的按钮是否存在?

还有没有人找到任何用 JavaScript 编写测试脚本(相对于动态网页)的好的参考资料?

感谢您的所有帮助!

问候,

史蒂夫·奥沙利文

【问题讨论】:

    标签: javascript xcode automated-tests ios-ui-automation


    【解决方案1】:

    嘿。
    实际上,来自苹果的文档(thisthis)是我唯一能找到的。
    至于你的问题试试

    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 guide to predicates

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 2012-03-31
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      相关资源
      最近更新 更多