我将SpecFlow与编码的UI测试结合使用,并且需要在多个表单字段上设置值,在这些字段中我知道HTML标记的ID,但我不一定知道与之相对应的基础System.Type
编码的UI测试中的HTML标签。
基本上,我想以通用方式使用编码的UI测试将值分配给表单字段。
示例SpecFlow步骤:
When I fill in the "Hours of Operation Information" table with the following values:
| Shift | From | To |
| DAY ONLY | 12:30AM | 12:00AM |
| DAY ONLY | 12:30AM | 12:00AM |
| DAY ONLY | 12:30AM | 12:00AM |
| DAY ONLY | 12:30AM | 12:00AM |
| DAY ONLY | 12:30AM | 12:00AM |
| DAY ONLY | 12:30AM | 12:00AM |
| DAY ONLY | 12:30AM | 12:00AM |
“ Shift”,“ From”和“ To”列标题已映射到HTML标签ID,为简便起见,由于此映射有效,因此我将其省略。
步骤定义:
[When(@"I fill in the ""(.*)"" table with the following values:")]
public void WhenIFillInTheTableWithTheFollowingValues(string tableName, Table table)
{
HtmlTable grid = GetGrid(tableName);
HtmlControl control = null;
TableRow row = null;
string id;
for (int i = 0; i < table.Rows.Count; i++)
{
row = table.Rows[i];
foreach (KeyValuePair<string, string> column in row)
{
id = String.Format(FormMap.GetFieldName(tableName, column.Key), i);
control = new HtmlControl(grid);
control.SearchProperties[HtmlControl.PropertyNames.Id] = id;
Assert.IsTrue(control.Exists, "Form field '{0}' does not exist in row {1} of the '{2}' grid (id={3}).", column.Key, i, tableName, id);
// How do I set the form field value?
}
}
}
我创建了一个使用
HtmlControl
对象和值的帮助程序方法,然后查看HTML标记名称。然后,它将尝试将其强制转换为适当的类,以便我可以与其进行交互。public void SetFormFieldValue(HtmlControl control, object value)
{
string controlValue = value == null ? null : value.ToString();
string tagName = control.TagName.ToLower();
string fieldType = null;
switch (tagName)
{
case "select":
((HtmlComboBox)control).SelectedItem = controlValue;
break;
case "input":
fieldType = control.GetProperty(HtmlEdit.PropertyNames.Type).ToString().ToLower();
switch (fieldType)
{
case "text":
case "password":
((HtmlEdit)control).Text = controlValue;
break;
case "checkbox":
((HtmlCheckBox)control).Checked = controlValue.ToLower() == "checked";
break;
case "radio":
((HtmlRadioButton)control).Selected = controlValue.ToLower() == "checked";
break;
default:
throw new ArgumentException(String.Format("Cannot set value on {0}[type={1}]", tagName, fieldType));
}
break;
case "textarea":
((HtmlTextArea)control).Text = controlValue;
break;
default:
throw new ArgumentException(String.Format("Cannot set value on {0} tag.", tagName));
}
}
在这种特定情况下,我试图在
<select>
框中设置值。我收到以下异常:System.InvalidCastException:无法将类型为“ Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlControl”的对象转换为类型为“ Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlComboBox”。
题:
如何在不事先知道HTML标记类型的情况下,使用编码的UI测试在表单字段上批量设置值?
最佳答案
我总比问一个问题要了解得多,因为我总是在问完问题后总是找到答案。
有一个称为UITestControl.CopyFrom的方法,该方法可以接收任何UITestControl
对象并将其复制到源对象:
public void SetFormFieldValue(HtmlControl control, object value)
{
string controlValue = value == null ? null : value.ToString();
string tagName = control.TagName.ToLower();
string fieldType = null;
switch (tagName)
{
case "select":
// The concrete Coded UI Test class I want to interact with
HtmlComboBox select = new HtmlComboBox();
// Make the `select` object reference the same element as `control`
select.CopyFrom(control);
// Set the value on the dropdown list
select.SelectedItem = controlValue;
break;
case "textarea":
HtmlTextArea textarea = new HtmlTextArea();
textarea.CopyFrom(control);
textarea.Text = controlValue;
break;
...
}
}
关于c# - 如何使用C#中的编码UI测试批量设置表单字段值,而无需提前知道HTML标记类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25329367/