编码的 UI 测试 - 单击基于表行上其他内容的控件

本文关键字:其他 控件 于表行 UI 测试 单击 编码 | 更新日期: 2023-09-27 18:32:03

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

因此,例如,我有一个表格,第一列中有名称和其他信息,然后在第 6 列中有一个按钮。全部自动生成。

我假设如果我可以从单元格中获取行号,其中有"约翰史密斯",然后我可以按下第 6 列中此行的按钮。有什么想法吗?我尝试过谷歌搜索并查看我可以传入的参数,但我遇到了困难。

编码的 UI 测试 - 单击基于表行上其他内容的控件

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

通过从编码的 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中,而不需要循环和检查值。根据表格的确切编写方式,表格和所需单元格之间可能会有额外的级别,因此您可能需要检查子

项、孙项、曾孙项、曾孙项、曾曾孙项等。

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

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

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]));

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

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

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