2013-07-15 14 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元素一樣訪問它,然後點擊它。