WatIn将新选项添加​​到选择列表吗? [英] WatIn Add a new option to a Selectlist?

查看:83
本文介绍了WatIn将新选项添加​​到选择列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个单元测试中向selectList添加一个新选项,但我不知道该怎么做.

I need to add a new option to a selectList in one of my unit tests, and I can't figure out how to do it.

下拉菜单当前有2个选项,我想添加第三个选项并使用它.

The Dropdown currently has 2 options, I want to add a third, and use it.

我尝试使用

I tried to use JavaScript injection using http://stevenharman.net/blog/archive/2007/07/10/add-option-elements-to-a-select-list-with-javascript.aspx as a base, but that failed. I get exceptions that crash the IE browser every time, and the text "RunScript failed" gets printed into my logs even though I don't use that text in my error output.

在Watin中这可能吗?还是开源使我失败了?

Is this possible in Watin? Or has Open Source Failed me?

推荐答案

使用您提供的链接中的代码,我做了一个小的改动就使它起作用.

Using the code in the link you provided, with one small change I've gotten it to work.

我的更改

  • 将ID更改为我的下拉列表的ID(当然!)
  • 将元素get中的$更改为"document.getElementById".取而代之的是$,我看不到任何明显的错误或类似的东西.只是不采取任何行动.

新选项"作为最后一项添加到下拉列表中,并且它是选定项.

The 'New Option' is added to the dropdown as the last item and it is the selected item.

string js = "";
js = js + "var theSelectList = document.getElementById('myDropDownID'); ";
js = js + " AddSelectOption(theSelectList, \"My Option\", \"123\", true);";
js = js + " function AddSelectOption(selectObj, text, value, isSelected) ";
js = js + "{";
js = js + " if (selectObj != null && selectObj.options != null)";
js = js + "{";
js = js + " selectObj.options[selectObj.options.length] = new Option(text, value, false, isSelected);";
js = js + "}}";

myIE.Document.Eval(js);

我的设置

  • WatiN 2.0
  • IE8
  • Win7

检查下拉菜单中是否有1个条目和2个条目;两种情况都添加了我的选择",没有问题.

Checked when the dropdown has 1 entry and 2 entries; both scenarios had "My Option" added without issue.

这篇关于WatIn将新选项添加​​到选择列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆