【发布时间】:2017-12-27 09:44:30
【问题描述】:
在网络表单中创建了一个简单的计算器应用程序。
用户在文本字段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?
【问题讨论】:
标签: c# asp.net coded-ui-tests