2012-10-02 115 views
1

我正嘗試在JavaScript中編寫腳本來測試MonkeyTalk的iPhone應用程序的UI。我如何使用邏輯和條件?現在我有一個塊:MonkeyTalk布爾邏輯

if (this.app.button("name").verify()) 
     do this if button exists 

的問題是驗證不返回一個bool,它只是拋出一個錯誤的測試如果按鈕不存在。有沒有辦法來捕捉錯誤並相應地運行腳本?

回答

2

這似乎工作:

function verifiedViewOrNull (view) 
{ 
var exists = false; 
try 
{ 
    view.verify(); 
    exists = true; 
} 
catch (e) 
{ 

} 

return exists ? view : null; 
} 

,你可以直接稱呼其爲這樣的:

if (verifiedViewOrNull(this.app.view("name")) != null 
{ 
     // It exists 
} else 
     //doesn't exist, not gonna throw exception