2009-07-14 83 views
3

我们使用Watin进行验收测试,发现一旦我们的网页超过100K的HTML源代码,它就会变得非常慢。提高Watin的性能和速度

我有一种感觉,一些速度问题来自迭代HTML表格。我们的一些桌子有50-60行,每列有5-10列,这使得Watin在搜索页面上的项目时非常缓慢。

有没有人有(例如)元素搜索方法的最佳重载使用的具体建议?是否有特定的方法可以避免,因为它们真的很慢?

回答

1

您可以加快添加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; 
+1

我觉得很难相信。 WatiN是否对ID上的元素进行索引,并且不为元素设置数组? – Martin 2010-01-14 13:19:07

3

我已经做了什么来帮助加快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; 
      } 
     } 
    } 
} 

所有你需要做的就是给表的引用,它会找到第一个tr和遍历它的所有兄弟姐妹。

foreach (TableRow tr in ie.Table("myTable").TableRowEnumerator()) 
{ 
    //Do Someting with tr 
} 
+0

更快,但仍然非常慢。 – 2010-11-09 17:21:20