2013-04-29 73 views
1

我已經設置了一些UIAutomation腳本,最終將成爲jenkins測試。 UIAutomation腳本不以jenkins友好的格式輸出,所以我使用tuneup_js,特別是這個failure exception在iOS的UIAutomation中處理自定義JavaScript異常UIATarget.onAlert

當測試失敗時,我可以拋出一個自定義FailureException。正常捕獲異常並且故障輸出正確記錄。輸出是Error: FailureException: "hello world"

當我嘗試在onAlert處理程序UIATarget.onAlert = function onAlert(alert) {...}中嘗試拋出相同的FailureException時,會出現我的問題。在我onAlert功能,我拋出一個FailureException如果標題一定的正則表達式匹配,但FailureException從未捕獲和測試與下面的輸出崩潰:

Script threw an uncaught JavaScript error: hello world on line 18 of assertions.js 

任何想法如何,我可以拋出這個FailureExceptiononAlert處理程序並正確處理它?看起來好像onAlert是在與其他測試不同的範圍內處理的,但我不確定如何解決這個問題。

回答

3

這裏的核心問題是,js異常並不真正用於異步代碼,比如事件循環(這就是爲什麼你很少在「現代javascript」代碼中看到拋出異常的原因,而人們使用錯誤回調來代替)。

如果您看一下tuneup_js中的test函數,它將捕獲由fail引發的異常,然後調用UIALogger.logFailUIATarget.onAlert將響應某些頂級警報事件並在該環境中運行,但不在測試函數中,因此觸發異常意味着它不會被test中的try/catch塊捕獲。

一兩件事,做的工作是使用封閉到的數據返回給調用者,是這樣的:

test("My UI Test", function(app, target) { 
    var failureName = null; 
    setupOnAlert(function(name) { 
     failureName = name; 
    }); 
    // do something that might trigger a UIAlert 
    if (failureName) { 
     fail(failureName); 
    } 
}); 

function setupOnAlert(fail_callback) { 
UITarget.onAlert = function(alert) { 
    // do something that may fail, if it fails: 
    fail_callback("Some kind of error"); 
} 
} 

希望幫助!

+0

輝煌,謝謝 – johngraham 2013-05-01 19:57:38