2011-11-30 53 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

好的,那时我很不好。只是在那里大声思考;) –