Watin 触发的 onchange 事件中的对话框

发布于 2024-12-18 12:06:04 字数 1274 浏览 1 评论 0原文

我有一个带有下拉菜单的网站。当用户更改下拉列表时,会出现一个确认对话框,询问他们是否希望更改。如果他们单击“是”,则继续,否则保持不变。相当标准的东西。

然而,当我开始编写 Watin 单元测试时,却很痛苦。

我的 html 是一个简单的选择列表,id 为 _stateList

这是我的 javascript:

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

所以在 Watin 中,我有一个扩展方法来触发更改事件:

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()。 Select 有类似的东西吗?还是埃瓦尔?或者也许有一个设置说根本不需要等待?

任何帮助表示赞赏。

I have a site with a dropdown. When the user changes the dropdown, a confirm dialog appears asking them if they wish to change it. If they click yes, it continues, otherwise it stays the same. Pretty standard stuff.

However, when I got to writing the Watin unit test, it was a pain.

My html is a simple select list with an id of _stateList

This is my javascript:

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

So in Watin, I have an extension method to fire the change event:

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
}

That extension method is called here:

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");
}

I really hope this isn't too much code, but I simply cannot figure out how to handle a dialog that is trigger in change event rather than a click event. In Watin, buttons have ClickNoWait(). Is there anything similar for Select? Or Eval? Or maybe a setting that says don't wait at all?

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦初启 2024-12-25 12:06:04

将 JavaScript 封装在 setTimeout(function(){}) 中;将允许 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

Wrapping your javascript in a setTimeout(function(){}); will allow Eval to return asynchronously.

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

心欲静而疯不止 2024-12-25 12:06:04

原因之一可能是您忘记结束 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 
        }
});

one of the reasons might be the fact you forgot to end your if statement.

Your code says:

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

});

While it should be:

$(function() {
    $('#_stateList').change(function() {
        if(confirm('Are you sure you wish to change your state?')){
            //do something 
        }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文