编码ui测试项目,获取asp标签的值
coded ui test project, obtain value of asp label
在网络表单中创建了一个简单的计算器应用程序。
用户在文本字段 MainContent_numberTb
中输入一个数字,然后单击结果按钮。
在我的解决方案中添加了一个新的 'coded UI Test Project'。通过添加“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);
但它们都 return 正确,即使标签未显示,但它仍在页面上,只是设置为不可见。在那种情况下,我应该检查它的样式..类似于
//string labelText3 = PositiveNumValMsg.GetProperty("style").ToString();
然后检查样式是否包含visibility: visible
?
你想抢它InnerText属性.
设置 ControlType
不是强制性的,因此以下的一些变体应该有效:
UITestControl AllNumsTB = new UITestControl(Browser);
AllNumsTB.TechnologyName = "Web";
AllNumsTB.SearchProperties.Add(HtmlControl.PropertyNames.Id, "MainContent_AllNumLbl_Res");
var result = AllNumsTB.GetProperty(HtmlLabel.InnerText).Trim();
// var result = AllNumsTB.GetProperty("InnerText").Trim();
var AllNumsTB = new HtmlLabel(Browser);
AllNumsTB.TechnologyName = "Web";
AllNumsTB.SearchProperties.Add(HtmlControl.PropertyNames.Id, "MainContent_AllNumLbl_Res");
var result = AllNumsTB.InnerText;
string result2;
// you may need to include this section, or you may not
if (result.Length > 0)
{
AllNumsTB.WaitForControlReady();
result2 = AllNumsTB.InnerText;
}
编辑:关于测试 ASP.Net 验证器
我已经能够使用以下方法检查验证器消息是否显示:
1) 使用正则表达式验证器创建了一个测试 asp.net 页面,该验证器正好需要 2 位数字:
<asp:TextBox ID="numberTb" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="regexpName" ControlToValidate="numberTb" ValidationExpression="\d{2}" runat="server" ErrorMessage="Please enter 2 digits"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
2) 运行 编码 UI 测试生成器并开始记录 =>
点击输入框;输入 s;点击 tab(显示验证器错误消息)。
3) 暂停了录音机。
4) 点击"Generate Code"图标并给它一个方法名;单击 "Add and Generate" 按钮。
5) 现在我将十字准线图标拖放到验证器消息上。向下滚动选项列表是 ControlDefinition。右键单击它并选择 "Add Assertion...".
6) 将比较器更改为"Contains"; “可见”的比较值;并给它一条断言失败消息。
7) 单击 "Generate Code" 图标,给它一个方法名称等
现在我们有了代码,可以通过 运行 两种方法测试验证器 - 首先输入输入并触发(或不触发)验证器消息;第二个测试验证器的消息可见性。我复制并粘贴了生成的代码,并用它编写了另一个使用“hidden;”的对立测试。当给出正确的输入时。 运行 两个测试,他们都通过了。
你最终会得到类似(替换值)的结果:
public void DigitValidatorMsgShownWithIncorrectStringInput()
{
#region Variable Declarations
HtmlSpan uIAtleast2digitsPane = this.UIHomePageMyASPNETApplWindow.UIHomePageMyASPNETApplDocument.UIAtleast2digitsPane;
#endregion
// Verify that the 'ControlDefinition' property of 'At least 2 digits' pane contains ' visible;'
StringAssert.Contains(uIAtleast2digitsPane.ControlDefinition, " visible;", "The validator was not shown");
}
当然,一旦您知道要查找的内容,所有这些都可以手动编码。
在网络表单中创建了一个简单的计算器应用程序。
用户在文本字段 MainContent_numberTb
中输入一个数字,然后单击结果按钮。
在我的解决方案中添加了一个新的 'coded UI Test Project'。通过添加“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);
但它们都 return 正确,即使标签未显示,但它仍在页面上,只是设置为不可见。在那种情况下,我应该检查它的样式..类似于
//string labelText3 = PositiveNumValMsg.GetProperty("style").ToString();
然后检查样式是否包含visibility: visible
?
你想抢它InnerText属性.
设置 ControlType
不是强制性的,因此以下的一些变体应该有效:
UITestControl AllNumsTB = new UITestControl(Browser);
AllNumsTB.TechnologyName = "Web";
AllNumsTB.SearchProperties.Add(HtmlControl.PropertyNames.Id, "MainContent_AllNumLbl_Res");
var result = AllNumsTB.GetProperty(HtmlLabel.InnerText).Trim();
// var result = AllNumsTB.GetProperty("InnerText").Trim();
var AllNumsTB = new HtmlLabel(Browser);
AllNumsTB.TechnologyName = "Web";
AllNumsTB.SearchProperties.Add(HtmlControl.PropertyNames.Id, "MainContent_AllNumLbl_Res");
var result = AllNumsTB.InnerText;
string result2;
// you may need to include this section, or you may not
if (result.Length > 0)
{
AllNumsTB.WaitForControlReady();
result2 = AllNumsTB.InnerText;
}
编辑:关于测试 ASP.Net 验证器
我已经能够使用以下方法检查验证器消息是否显示:
1) 使用正则表达式验证器创建了一个测试 asp.net 页面,该验证器正好需要 2 位数字:
<asp:TextBox ID="numberTb" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="regexpName" ControlToValidate="numberTb" ValidationExpression="\d{2}" runat="server" ErrorMessage="Please enter 2 digits"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
2) 运行 编码 UI 测试生成器并开始记录 => 点击输入框;输入 s;点击 tab(显示验证器错误消息)。
3) 暂停了录音机。
4) 点击"Generate Code"图标并给它一个方法名;单击 "Add and Generate" 按钮。
5) 现在我将十字准线图标拖放到验证器消息上。向下滚动选项列表是 ControlDefinition。右键单击它并选择 "Add Assertion...".
6) 将比较器更改为"Contains"; “可见”的比较值;并给它一条断言失败消息。
7) 单击 "Generate Code" 图标,给它一个方法名称等
现在我们有了代码,可以通过 运行 两种方法测试验证器 - 首先输入输入并触发(或不触发)验证器消息;第二个测试验证器的消息可见性。我复制并粘贴了生成的代码,并用它编写了另一个使用“hidden;”的对立测试。当给出正确的输入时。 运行 两个测试,他们都通过了。
你最终会得到类似(替换值)的结果:
public void DigitValidatorMsgShownWithIncorrectStringInput()
{
#region Variable Declarations
HtmlSpan uIAtleast2digitsPane = this.UIHomePageMyASPNETApplWindow.UIHomePageMyASPNETApplDocument.UIAtleast2digitsPane;
#endregion
// Verify that the 'ControlDefinition' property of 'At least 2 digits' pane contains ' visible;'
StringAssert.Contains(uIAtleast2digitsPane.ControlDefinition, " visible;", "The validator was not shown");
}
当然,一旦您知道要查找的内容,所有这些都可以手动编码。