2011-11-30 52 views
1

我有一個帶有下拉菜單的網站。當用戶改變下拉菜單時,會出現一個確認對話框,詢問他們是否希望改變它。如果他們點擊是,則繼續,否則保持不變。相當標準的東西。Watin觸發的onchange事件中的對話框

但是,當我開始編寫Watin單元測試時,這是一個痛苦。

我的HTML是一個簡單的選擇列表與_stateList

這一個ID是我的javascript:

$(function() { 
    $('#_stateList').change(function() { 
     if(confirm('Are you sure you wish to change your state?')) 
      //do something 
    }); 
}); 
在華廷

所以,我有一個擴展方法火更改事件:

public static void SelectWithChangeEvent(this SelectList selectList, string text) 
{ 
    selectList.Select(text); 
    string js = string.Format("$('#{0}').change();", selectList.Id); 
    InternetExplorer.Browser.Eval(js); //This is where it hangs 
} 

該擴展方法在此處稱爲:

ConfirmDialogHandler dialogHandler = new ConfirmDialogHandler(); 
using (new UseDialogOnce(InternetExplorer.Browser.DialogWatcher, dialogHandler)) 
{ 
    PageMapping.StateDropdown.SelectWithChangeEvent(stateName); //It never gets past here 
    dialogHandler.WaitUntilExists(5); 
    if(dialogHandler.Exists()) 
     dialogHandler.OKButton.Click(); 
    else 
     Assert.Fail("No Dialog Appeared"); 
} 

我真的希望這不是太多的代碼,但我根本無法弄清楚如何處理觸發變化事件而不是點擊事件的對話框。在Watin中,按鈕有ClickNoWait()。有沒有類似的選擇?還是Eval?或者,也許是一個說不要等待的設置?

任何幫助表示讚賞。

回答

5

結束語你的JavaScript中的setTimeout(函數(){});將允許Eval異步返回。

public static void SelectWithChangeEvent(this SelectList selectList, string text) 
{ 
    selectList.Select(text); 
    string js = string.Format("setTimeout(function() {{$('#{0}').change();}}, 5);", selectList.Id); 
    InternetExplorer.Browser.Eval(js); //This is where it hangs 
} 

https://developer.mozilla.org/en/DOM/window.setTimeout

-2

其中一個原因可能是您忘記結束您的if語句。

您的代碼表示:

i$(function() { 
$('#_stateList').change(function() { 
    if(confirm('Are you sure you wish to change your state?') 
     //do something 

});

雖然它應該是:

$(function() { 
    $('#_stateList').change(function() { 
     if(confirm('Are you sure you wish to change your state?')){ 
      //do something 
     } 
}); 
+0

一個if語句不需要括號線。另外,這不是問題在這裏。 – mccow002

+0

好的,那時我很不好。只是在那裏大聲思考;) –