2009-07-14 81 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