使用Webforms创建了一个简单的计算器应用程序。用户在文本字段MainContent_numberTb
中输入数字,然后点击结果按钮。
将一个新的“编码 UI 测试项目”添加到我的解决方案中。通过添加“5”测试了UI,一切正常。现在希望将实际结果与预期结果进行比较。
BrowserWindow Browser = BrowserWindow.Launch("http://url");
UITestControl UiInputField = new UITestControl(Browser);
UiInputField.TechnologyName = "Web";
UiInputField.SearchProperties.Add("ControlType", "Edit");
UiInputField.SearchProperties.Add("Id", "MainContent_numberTb");
//Populate input field
Keyboard.SendKeys(UiInputField, "5");
//Results Button
UITestControl ResultsBtn = new UITestControl(Browser);
ResultsBtn.TechnologyName = "Web";
ResultsBtn.SearchProperties.Add("ControlType", "Button");
ResultsBtn.SearchProperties.Add("Id", "MainContent_calBtn");
Mouse.Click(ResultsBtn);
所有的代码都能正常工作,但访问标签时出现了问题。
<asp:Label ID="AllNumLbl_Res" runat="server"></asp:Label>
控件类型旁边应该插入什么?它不是编辑,因为编辑是文本字段。那么,用来存储实际结果以便我可以比较AllNumsTB
的是什么?
string expectedAllNums = "1, 2, 3, 4, 5";
UITestControl AllNumsTB = new UITestControl(Browser);
AllNumsTB.TechnologyName = "Web";
AllNumsTB.SearchProperties.Add("ControlType", "?????");
AllNumsTB.SearchProperties.Add("Id", "MainContent_AllNumLbl_Res");
if(expectedAllNums != AllNumsTB.??????)
{
Assert.Fail("Wrong Answer");
}
更新
好的,使用调试器控制台,我能够通过((Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlSpan)new System.Collections.ArrayList.ArrayListDebugView(((System.Collections.CollectionBase)(AllNumsTB.FindMatchingControls()).List).InnerList).Items[0]).DisplayText
获取标签的值。
但是当我在代码中使用它,并且ArrayListDebugView
由于受到保护而无法访问??
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 更新 感谢K Scandrett的答案...如果可以的话,我想知道您是否也可以帮助我进行验证...如果用户输入字母或非正数,则会触发错误消息。
<asp:RegularExpressionValidator ID="regexpName"
//VALIDATION MESSAGE
UITestControl PositiveNumValMsg = new UITestControl(Browser);
PositiveNumValMsg.TechnologyName = "Web";
PositiveNumValMsg.SearchProperties.Add("Id", "MainContent_regexpName");
一切都运行良好,但是我想测试标签是否出现...到目前为止我已经尝试过了
//bool visible = false;
//System.Drawing.Point p;
//// If the control is offscreen, bring it into the viewport
//PositiveNumValMsg.EnsureClickable();
// // Now check the coordinates of the clickable point
// visible = PositiveNumValMsg.TryGetClickablePoint(out p)
// && (p.X > 0 || p.Y > 0);
var isVisible = PositiveNumValMsg.WaitForControlPropertyNotEqual(UITestControl.PropertyNames.State, ControlStates.Invisible);
但是它们都返回true,即使标签没有显示,但它仍然在页面上设置为不可见。在这种情况下,我应该检查它的样式..类似于
//string labelText3 = PositiveNumValMsg.GetProperty("style").ToString();
然后检查样式是否包含 visibility: visible
?
var PositiveNumValMsg = new HtmlSpan(Browser);
。通常变量以小写字母开头,因此“positiveNumValMsg”将是一个更常规的名称。 - K Scandrett