3

我在 Visual Studio 2010 中为 C# 中的 Web 应用程序进行编码 UI 测试,我希望根据第 1 列的内部文本单击表格第 6 列中的按钮?这可能吗?

因此,例如,我在第一列中有一个名称和其他信息的表,然后在第 6 列中有一个按钮。所有这些都是自动生成的。

我假设如果我可以从带有“John Smith”的单元格中获取行号,那么我可以在第 6 列中按下该行的按钮。有什么想法吗?我试过谷歌搜索并查看我可以传递的参数,但我很难过。

4

3 回答 3

4

基于以下内容的东西可能会有所帮助。

通过从 Coded 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方法遍历表。从上面得到theTable。然后有类似的东西

UITestControlCollection children = theTable.GetChildren();

循环children检查行属性。当找到所需的行时,调用该GetChildren行的 并通过它们进行循环以找到所需的列。几点:在循环遍历行之前,您可能需要循环遍历列。您可以直接索引到UITestControlCollection行和列,而不需要循环和检查值。根据表格的具体书写方式,表格和想要的单元格之间可能存在额外的级别,因此您可能需要检查孩子、孙子、曾孙、伟大...等等。

于 2013-09-20T11:46:11.663 回答
2

我有几种扩展方法用于处理表格中的内容(手工编码,而不是使用记录器)-

表格的此扩展方法获取表格中的第一行,该行在其一个单元格或控件中包含请求的文本

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)单元格

Mouse.Click(new HtmlInputButton(UITableCustomers.GetRow("John Smith").Cells[5]));

显然,如果表中的控件中不存在给定文本,则此调用将失败。

于 2013-09-24T13:58:04.707 回答
-1

您的问题对我来说不是很清楚,但请查看jQuery.trigger 上的文档

此方法将让您模拟点击事件。我希望这是你需要的。

于 2013-09-20T09:37:16.410 回答