为什么UI自动化仪器无法确定此处是否启用了按钮?

时间:2011-03-31 23:39:37

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

我正在使用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.");  
}

4 个答案:

答案 0 :(得分:2)

嘿。
我同意Sosullivan。一般来说,按钮访问可能存在问题。另外,如果mainWindow.buttons()不包含名称为“Sign In”的名称,那么在没有任何输出的情况下执行的代码是不是完整的?我确定,该按钮位于对象树中,验证其位置(因此我可以访问它),然后调用.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将检查是否在树状结构中找到按钮(在主应用程序下)。如果它没有解决问题,请检查按钮所在的树结构中的位置,然后将正确的位置验证作为检查是否已启用的前提条件。

答案 1 :(得分:0)

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

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

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

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:-1)

槽糕。确定....

我们使用名为“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对象中。

我并不完全确定我已经按照我的意愿传达了我/我们所做的事情,但我在这里做得最好。祝大家好运!

SteveO