提高 Watin 的性能和速度

发布于 2024-07-28 22:00:12 字数 223 浏览 4 评论 0原文

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

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

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

We're using Watin for acceptance tests and we find it gets mighty slow once we have Web pages that are more than 100K of HTML source.

I've a feeling that some of the speed issues come from iterating over HTML tables. Some of our tables have 50 - 60 rows, each with 5 - 10 columns, and this makes Watin quite slow when searching for items on the page.

Does anyone have specific recommendations on (for instance) the best overloads of the element search methods to use? Are there specific methods to avoid because they're really slow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩饰不了的爱 2024-08-04 22:00:12

为了帮助加速表元素的处理,我所做的就是编写一个扩展方法,通过在表行上调用 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
}

What I have done to help speed up processing of Table elements is written a extension method to Iterate over table rows by calling NextSibling on a table row instead of calling the .TableRows property which can be slow.

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;
            }
        }
    }
}

All you need to do is get a reference to a Table and it will find the first tr and iterate over all of it's siblings.

foreach ( TableRow tr in ie.Table( "myTable" ).TableRowEnumerator() )
{
    //Do Someting with tr
}
慵挽 2024-08-04 22:00:12

您可以通过向 html 表格行或列元素添加 ID 来加快速度。 因此,在您的列较少的情况下,将 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;

You can speed up a bit with adding IDs to html table rows or columns elements. So in your case where you have less columns it is probably easier to add ids to at least columns. (Specially because numbers of rows is probably changing).

So instead of

string price = ie.Table(Find.ById("name")).TableRows[i].TableCells[i].Text;

with this changes in html

<table id="name">
<tr id='total'>             
            <td id='price'>
                $1.00
            </td>
        </tr>
</table>

without iteration

string total = ie.TableRow(Find.ByID("total")).TableCell(Find.ById("price")).Text;

or
only one iteration

ie.Table(Find.ById("name")).TableRows[i].TableCell(Find.ById("price")).Text;
柠栀 2024-08-04 22:00:12

只是与 Watin 性能相关的一个小注释,我发现某些代码片段会大大减慢 watin 程序的速度(我不知道为什么)。 我在这里写过:

http://me-ol-blog.blogspot.co.il/2011/08/some-thoughts-about-watin-windows-7.html

Just a small note related to Watin performance, i found that certain code fragments slow down watin programs considerably (I don't know why). I wrote about it here:

http://me-ol-blog.blogspot.co.il/2011/08/some-thoughts-about-watin-windows-7.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文