我们正在使用 Watin 进行验收测试,我们发现一旦我们拥有超过 100K 的 HTML 源代码的网页,它就会变得非常缓慢。
我感觉一些速度问题来自对 HTML 表的迭代。我们的一些表格有 50 - 60 行,每行有 5 - 10 列,这使得 Watin 在页面上搜索项目时非常慢。
是否有人对(例如)要使用的元素搜索方法的最佳重载有具体建议?是否有特定的方法可以避免,因为它们真的很慢?
我们正在使用 Watin 进行验收测试,我们发现一旦我们拥有超过 100K 的 HTML 源代码的网页,它就会变得非常缓慢。
我感觉一些速度问题来自对 HTML 表的迭代。我们的一些表格有 50 - 60 行,每行有 5 - 10 列,这使得 Watin 在页面上搜索项目时非常慢。
是否有人对(例如)要使用的元素搜索方法的最佳重载有具体建议?是否有特定的方法可以避免,因为它们真的很慢?
为了帮助加快 Table 元素的处理,我编写了一个扩展方法,通过在表行上调用 NextSibling 而不是调用可能很慢的 .TableRows 属性来迭代表行。
public static class IElementContainerExtensions
{
/// <summary>
/// Safely enumerates over the TableRow elements contained inside an elements container.
/// </summary>
/// <param name="container">The IElementsContainer to enumerate</param>
/// <remarks>
/// This is neccesary because calling an ElementsContainer TableRows property can be an
/// expensive operation. I'm assuming because it's going out and creating all of the
/// table rows right when the property is accessed. Using the itterator pattern below
/// to prevent creating the whole table row hierarchy up front.
/// </remarks>
public static IEnumerable<TableRow> TableRowEnumerator( this IElementContainer container )
{
//Searches for the first table row child with out calling the TableRows property
// This appears to be a lot faster plus it eliminates any tables that may appear
// nested inside of the parent table
var tr = container.TableRow( Find.ByIndex( 0 ) );
while ( true )
{
if ( tr.Exists )
{
yield return tr;
}
//Moves to the next row with out searching any nested tables.
tr = tr.NextSibling as TableRow;
if ( tr == null || !tr.Exists )
{
break;
}
}
}
}
您需要做的就是获取对 Table 的引用,它会找到第一个 tr 并遍历它的所有兄弟姐妹。
foreach ( TableRow tr in ie.Table( "myTable" ).TableRowEnumerator() )
{
//Do Someting with tr
}
您可以通过将 ID 添加到 html 表格行或列元素来加快速度。因此,在您的列较少的情况下,将 id 添加到至少列可能更容易。(特别是因为行数可能在变化)。
所以而不是
string price = ie.Table(Find.ById("name")).TableRows[i].TableCells[i].Text;
随着 html 的这种变化
<table id="name">
<tr id='total'>
<td id='price'>
$1.00
</td>
</tr>
</table>
没有迭代
string total = ie.TableRow(Find.ByID("total")).TableCell(Find.ById("price")).Text;
或只有一次迭代
ie.Table(Find.ById("name")).TableRows[i].TableCell(Find.ById("price")).Text;
只是与 Watin 性能相关的一个小注释,我发现某些代码片段大大减慢了 watin 程序(我不知道为什么)。我在这里写过:
http://me-ol-blog.blogspot.co.il/2011/08/some-thoughts-about-watin-windows-7.html