【问题标题】:Why is the UI Automation instrument unable to determine if a button is enabled here?为什么 UI 自动化工具无法确定此处是否启用了按钮?
【发布时间】:2011-03-31 23:39:37
【问题描述】:

我正在使用 Xcode 4.0.1 和 Instruments 尝试实现一些 UIAutomation 测试。

截至目前,我正在尝试确定是否启用了 MainWindow (mainWindow = app.mainWindow();) 上的按钮。

这是在 iPad II 上运行的应用程序,现在我感觉不到爱。

有人可以帮忙吗?

这是我尝试使用的语法;这看起来对吗?

var title="Checking that Sign-In button is disabled";  
try {  
    if (mainWindow.buttons()["Sign In"].isEnabled())  
    UIALogger.logPass("Try: " + title + " has passed.");  
}  
catch (error) {  
    UIALogger.logError(error);  
    target.logElementTree();  
    UIALogger.logFail("Try: " + title + " has failed.");  
}

【问题讨论】:

  • 您应该确定您的问题到底是什么!按钮是否可以访问?按钮的路径是否正确(尝试对其进行 .tap() 或在主窗口上执行 .logElementTree())。如果我们不知道您的界面是如何组织的,则无法回答此问题...您的语法似乎正确,因此这一定是按钮可访问性的问题。此外,尝试解释 Instrument 在脚本执行时的错误消息!

标签: iphone xcode ipad instruments ios-ui-automation


【解决方案1】:

嘿。
我同意索苏利文的观点。一般来说,按钮访问可能存在问题。此外,如果 mainWindow.buttons() 不包含一个名为“登录”的代码,那么整个代码是否会在没有任何输出的情况下执行?我会确定,该按钮在对象树中,验证它的位置(以便我可以访问它)然后调用 .isEnabled() 以查看它是否已为用户启用。
也许试试这个代码开始:

var title="Checking that Sign-In button is disabled";  
try {
  if (mainWindow.buttons()["Sign In"] && mainWindow.buttons()["Sign In"].isEnabled())  {
      UIALogger.logPass("Try: " + title + " has passed.");  
  } else {
      throw new Error("no SignIn button or button not enabled");
    }
} catch (error) {  
  UIALogger.logError(error);  
  target.logElementTree();  
  UIALogger.logFail("Try: " + title + " has failed.");  
}

首先检查if 将检查是否在树结构中找到按钮(在主应用程序下)。如果它不能解决问题,请检查按钮在树结构中的位置,然后将正确的位置验证作为检查它是否启用的前提。

【讨论】:

    【解决方案2】:

    在您的代码中,如果按钮被禁用,则以下表达式将为 False

    mainWindow.buttons()["Sign In"].isEnabled()
    

    你的代码没有 else 语句,也不会抛出错误。

    您应该拥有 else 结构,甚至更好 - 首先检查您正在访问的对象是否存在于当前窗口中,如yoosiba 的回答所示。

    【讨论】:

      【解决方案3】:

      在这种情况下,您应该使用方法 .isVisible 来检查对象是否存在。

      if (mainWindow.buttons()["Sign In"].isVisible())  
          UIALogger.logPass("Try: " + title + " has passed."); 
      

      【讨论】:

        【解决方案4】:

        嗯。好的....

        我们使用一个名为“alexvollmer-tuneup_js”的 UIAutomation 开源框架从 SourceForge 中取出。它是免费提供的,并且鼓励修改(作者要求您将他的代码版本分支以与他人共享)。

        他有一个名为 assertions.js 的文件,其中有两个函数 assertTrue() 和 assertFalse()。

        我在他包含的另一个名为“uiautomation-ext.js”的文件中使用这些文件,该文件被导入到我的测试文件中。在uiautomation-ext.js中,我添加了两个函数:

        /**
        * Asserts that the given button is disabled.
        */
        buttonIsDisabled: function(buttonName) {
                assertFalse(UIATarget.localTarget().frontMostApp().mainWindow().buttons()[buttonName].isEnabled(), "The button: " + buttonName + " is disabled.");
        },
        
        /**
        * Asserts that the given button is ENABLED.
        */
        buttonIsEnabled: function(buttonName) {
                assertTrue(UIATarget.localTarget().frontMostApp().mainWindow().buttons()[buttonName].isEnabled(), "The button: " + buttonName + " is enabled.");
        }
        

        这两个函数被放置在 uiautomation-ext.js 中名为“extend(UIAWindow.prototype”) 的 JDOS 对象中。

        我不完全确定我是否已按照我的意愿清楚地传达了我/我们所做的事情,但我已经尽我所能。祝大家好运!

        史蒂夫

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-28
          • 2018-10-16
          • 1970-01-01
          • 1970-01-01
          • 2011-11-23
          • 1970-01-01
          • 2017-01-31
          • 1970-01-01
          相关资源
          最近更新 更多