C# 编码UI测试-基于表行上的其他内容单击控件
我正在Visual Studio 2010中为C#中的web应用程序进行编码UI测试,我希望根据第1列的内部文本单击表第6列中的按钮?这可能吗 例如,我有一个表,第一列有名称,第六列有按钮。全部自动生成 我假设如果我能从单元格中得到行号,里面写着“John Smith”,我就可以按下第6列中这一行的按钮。C# 编码UI测试-基于表行上的其他内容单击控件,c#,visual-studio-2010,web-applications,automated-tests,coded-ui-tests,C#,Visual Studio 2010,Web Applications,Automated Tests,Coded Ui Tests,我正在Visual Studio 2010中为C#中的web应用程序进行编码UI测试,我希望根据第1列的内部文本单击表第6列中的按钮?这可能吗 例如,我有一个表,第一列有名称,第六列有按钮。全部自动生成 我假设如果我能从单元格中得到行号,里面写着“John Smith”,我就可以按下第6列中这一行的按钮。 有什么想法吗?我试着用谷歌搜索,看看我能传递哪些参数,但我还是被难住了。你的问题我不太清楚,但请查看jQuery.trigger上的 此方法将允许您模拟clickevent。我希望这就是您所需
有什么想法吗?我试着用谷歌搜索,看看我能传递哪些参数,但我还是被难住了。你的问题我不太清楚,但请查看jQuery.trigger上的
此方法将允许您模拟clickevent。我希望这就是您所需要的。基于以下内容的内容可能会有所帮助 通过复制编码UI记录器为断言生成的代码或单击单元格来访问HTML表。您应该有如下内容:
HtmlCell theTable = new HtmlCell(this.UItheWindow.UItheTable.UIItemTable);
可以通过添加以下属性来访问表的单元格:
theTable.FilterProperties[HtmlCell.PropertyNames.RowIndex] = "0";
theTable.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = "6";
UITestControl cell = theTable.Find();
HtmlCell wantedCell = (HtmlCell)cell;
上述内容可以用作返回wantedCell
值的方法体。该名称现在应可用为:
wantedCell.InnerText;
访问要单击的按钮应遵循类似的方法
另一种方法使用GetChildren
方法遍历表。首先获取表格
,如上所述。那就吃点
UITestControlCollection children = theTable.GetChildren();
循环检查
子项
行属性。当找到所需的行时,调用该行的GetChildren
,让循环通过它们找到所需的列。要点:在循环行之前,可能需要循环列。您可以直接索引到行和列的UITestControlCollection
,而无需循环和检查值。根据表格的具体编写方式,表格和所需单元格之间可能会有额外的级别,因此您可能需要检查孩子、孙子、大孙子、大孙子……等等。我有一些扩展方法,用于处理表格中的内容(手动编码,而不是使用记录器)-
表的此扩展方法获取表中的第一行,该行在其单元格或控件中包含请求的文本
public static HtmlRow GetRow(this HtmlTable table, string cellContent)
{
if((UITestControl)table == (UITestControl)null)
throw new ArgumentNullException("table");
if(cellContent.Length > 80)
cellContent = cellContent.Substring(0, 80); //Our table cells only display the first 80 chars
//Locate the first control in the table that has the inner text that I'm looking for
HtmlControl searchControl = new HtmlControl(table);
searchControl.SearchProperties.Add(PropertyNames.InnerText, cellContent);
//Did we find a control with that inner text?
if(!searchControl.TryFind())
{
//If not, the control might be an HtmlEdit with the text
HtmlEdit firstTableEdit = new HtmlEdit(table);
//Get all of the HtmlEdits in the table
UITestControlCollection matchingEdits = firstTableEdit.FindMatchingControls();
//Iterate through them, see if any have the correct text
foreach (UITestControl edit in matchingEdits)
{
if(cellContent == ((HtmlEdit)edit).Text)
searchControl = (HtmlControl)edit;
}
}
//We have(hopefully) found the control in the table with the text we're searching for
//Now, we spiral up through its parents until we get to an HtmlRow
while (!(searchControl is HtmlRow))
{
searchControl = (HtmlControl)searchControl.GetParent();
}
//The control we're left with should be an HtmlRow, and should be an Ancestor of a control that has the correct text
return (HtmlRow)searchControl;
}
一旦能够获得正确的行,在该行中获得正确的控件(或该行中给定单元格中的正确控件)就相对容易了
在您的示例中,行的第6列中有一个按钮。按钮可能有一些属性与之关联,但即使没有它们,如果我们能够正确限制搜索,它仍然可以工作。假设我们的表名为UITableCustomers-声明一个新按钮,并将其限制为包含“John Smith”的行中的第6个(索引5)单元格
显然,如果给定文本不存在于表中的控件中,则此调用将失败。我建议使用对象检查器(十字光标)检查对象,以确保此方法获得结果。我很少让表按预期的方式工作:)它们经常使用嵌套编码,因此它们不完全是HTMLcell。这可能是我的运气不好,但我并不认为细胞就是细胞是理所当然的。我用十字线来检查结构。我还使用
GetChildren
对结构进行了递归下降,并打印了找到的每个控件的各种属性。哇,这非常令人印象深刻,但我不确定该放在哪里,也不确定它如何知道要查看哪个表?我必须以某种方式向它传递一个表吗?扩展方法可以放入任何静态类中,然后用using语句引用-通常,创建一个静态类只是为了保存扩展方法。在HtmlTable上实现了扩展方法之后,该表现在有了.GetRow()方法——正如您在我的示例中所看到的,我将UITableCustomers指定为我的表,并对其调用.GetRow()。请参阅:正在尝试实现此代码,并在这一行中显示searchControl.SearchProperties.Add(PropertyNames.InnerText,cellContent);如果(!searchControl.TryFind())TryFind不存在,则我获取的属性名称不存在,并且在此行中exist@PaulCunningham我刚刚注意到这个问题被标记为VS2010,TryFind()在那里不可用-一个.Exists检查而不是.TryFind()应该以大致相同的方式运行。PropertyNames类应该存在于Microsoft.VisualStudio.TestTools.UITesting.HtmlControl命名空间中-是否缺少using语句?我手边没有东西可以试一下。好的,别担心,是的,我想我可能遗漏了一个using语句,所以我试着找出哪一个。对不起,我觉得你完全误解了这个问题
Mouse.Click(new HtmlInputButton(UITableCustomers.GetRow("John Smith").Cells[5]));