2013-07-15 16 views
4

感谢第一次花时间阅读并希望提供一些煽动我的问题。目前,我只是简单地尝试自动登录和注销我的公司使用Instruments的应用程序。我遇到了一些小问题(正如你可以看到的密码输入一样,由于奇怪的输入问题,我使用char而不是字符来滚动字符)。UIATarget.onAlert =函数onAlert(警告)问题 - 脚本似乎没有正确地进入块

问题是,当我弹出屏幕提示时,我想点击注销按钮。但是,似乎我从来没有进入应该处理警报的区块。我可以推论出这是因为我在onAlert块中散布的logMessages,当运行测试脚本时,它并不出现在我的日志中。我知道默认处理程序只是解除了任何警报,除非警报是明确处理的。我也相信我或者至少试图明确地处理这个警报,以便我可以点击右边的按钮。

我在做什么错?我遵循Apple Instruments指南,我相信我使用的语法与他们提供的完全相同。请在下面找到我的代码,我将其全部包含在内,但感兴趣的内容就在最后。

var target = UIATarget.localTarget(); 
var password = "something" 

UIALogger.logStart("Test1"); 

target.frontMostApp().mainWindow().scrollViews()[0].webViews()[0].textFields()[0].tap(); 

target.frontMostApp().keyboard().typeString("something"); 

UIATarget.localTarget().delay(2); 

target.frontMostApp().mainWindow().scrollViews()[0].webViews()[0].secureTextFields()[0].tap(); 

UIATarget.localTarget().delay(2); 

for (i = 0; i < password.length; i++){ 

    var strChar = password.charAt(i); 
    target.frontMostApp().keyboard().typeString(strChar); 

} 

target.frontMostApp().mainWindow().scrollViews()[0].webViews()[0].buttons()["Log in"].tap(); 

target.frontMostApp().mainWindow().tableViews()[0].cells()["Wipe data after 10 attempts"].staticTexts()["Wipe data after 10 attempts"].scrollToVisible(); 

target.frontMostApp().mainWindow().tableViews()[0].groups()["logout default"].buttons()["logout default"].tap(); 
// Alert detected. Expressions for handling alerts should be moved into the    UIATarget.onAlert function definition. 

UIALogger.logMessage("Just before alert"); 

UIATarget.onAlert = function onAlert(alert){ 

    UIALogger.logMessage("In Alert!"); 

    UIATarget.delay(1); 
    var title = alert.name(); 
    UIALogger.logMessage("Alert with title '" + title + "' encountered!"); 


    UIALogger.alert.logElementTree(); 
    alert.buttons()["Logout"].tap(); 


} 
+0

另外请注意,我试图坚持走“如果它不能正常工作,尝试.delay(1)手之前,可能是仪器的速度太快了UI”的做法。那里有一些多余的延迟。 – Rhysdox

+0

我能够通过遵循http://stackoverflow.com/questions/3651316/handling-alert-with-uiautomation上的建议来自动执行警报,这是否适合您? – Andy

+0

也有这个问题。到目前为止,我没有添加警报处理程序。在应用程序变为活动状态之前不会显示警报 - 所有测试都会一直持续到警报测试通过。任何答案,这一个将不胜感激! –

回答

3

我有这个问题,并最终发现,自动化代码正在取得进展到UI先下一行(在这种情况下,警报视图)被显示在模拟器。 为了缓解这一点,我在onAlert块之前添加了一个延迟,以允许显示警报视图。

UIATarget.localTarget().delay(3) 
UIATarget.onAlert = function onAlert(alert){ 
    var title = alert.name(); 
    UIALogger.logWarning("Alert with title ’" + title + "’ encountered!"); 
    target.frontMostApp().alert().cancelButton().tap(); 
    return false; // use default handler 
} 
+0

为什么不使用函数内部的alert? – Andy

2

你有一些错别字。

首先,您需要在之前定义警报处理函数,然后执行测试代码。将它移动到测试脚本的顶部。

其次,你的函数定义和赋值稍微不正确。取而代之的是:

UIATarget.onAlert = function onAlert(alert){ 

你应该有这样的:

UIATarget.onAlert = function (alert){ 

最后,此警报的处理程序只适用于iOS的警报,像权限的弹出窗口。我在这里猜测,但alert.buttons()["Logout"].tap();看起来像你自己的应用程序正在创建的东西。你不需要一个提醒处理程序,只需在元素树中像访问其他任何UI元素一样访问它,然后点击它。