Atata – 处理 table 行很慢

Atata – Processing table rows is slow

还有一个关于Atata框架的问题。我发现 tables 的加载速度很慢。有一个 table 大约。 1000 行,每当我对检索到的行执行任何操作时,引擎似乎都会再次加载 table。

运行缓慢的示例代码:

var row = _page.Inventory.Rows[x => x.Text == "some inventory"];
row.Should.Exist(); // that statement takes the same time as the above one and is slow...

我用于 table 的代码:

[FindByCss(".invGrid")]
public Table<GroupsRow, Page> Inventory { get; set; }

我一直在寻找方便的方法来热切加载数据,但一直没有找到。

我的问题是:

  1. 有什么方法可以快速加载行吗?
  2. 有什么方法可以使 table 加载速度更快?

目前,Table.Rows[predicate] 对大型 table 工作缓慢,因为它会检查每一行。它适用于小型 tables。可能我会更新它以便将来更快地工作,但这并不容易,所以暂时推迟。您可以使用另一种快速执行单个 XPath 命令的方法:

  1. 使用索引器:public TItem this[params string[] cellValues]

    _page.Inventory.Rows["some inventory"]
    

    它将找到包含传递值的单元格的第一行。 但它可能不准确,因为它使用 'contains' 谓词,如果你 在 table.

  2. 中有相似的字符串值
  3. 使用方法:public TItem GetByXPathCondition(string itemName, string xPathCondition)

    string itemName = "some inventory";
    
    _page.Inventory.Rows.GetByXPathCondition(
        itemName,
        $".//span[contains(concat(' ', normalize-space(@class), ' '), ' item-name ')][normalize-space(.) = '{itemName}']")
    

    该示例查找包含 <span class="item-name"> 和所需文本的 <tr>

    这种方法是准确的,但语法更复杂。

  4. 你可以把上面的例子解压到 扩展方法:

    public static class TableRowListExtensions
    {
        public static SomePage.GroupsRow GetByName(this TableRowList<SomePage.GroupsRow, SomePage> rowList, string name)
        {
            return rowList.GetByXPathCondition(name, $".//span[contains(concat(' ', normalize-space(@class), ' '), ' item-name ')][normalize-space(.) = '{name}']");
        }
    }
    

    然后在测试中使用:

    _page.Inventory.Rows.GetByName("some inventory")
    

    应该又快又准。

我还建议您使用 [FindByClass("invGrid")] 属性而不是 [FindByCss(".invGrid")] 当您有 CSS 选择器时,单个 class。 FindByCssAttribute 在 Atata 中运行速度稍慢,因为与大多数其他查找属性相比,它执行 1 个额外的 WebDriver 命令。