Improving Watin Performance and Speed

We use Watin for acceptance tests, and we find that it becomes very slow when we have web pages that contain more than 100 thousand HTML sources.

I get the feeling that some speed issues are due to iterating over HTML tables. Some of our tables contain 50-60 rows, each of which contains 5-10 columns, which makes Watin rather slow when searching for items on the page.

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

+3
source share
3 answers

You can speed up the bit by adding identifiers to table elements or html table columns. Thus, in your case, when you have fewer columns, it is probably easier to add identifiers, at least in the columns. (Especially because line numbers are likely to change).

So instead

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

with this change 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 just one iteration

ie.Table(Find.ById("name")).TableRows[i].TableCell(Find.ById("price")).Text;
+1
source

What I did to speed up the processing of table elements is recorded by the extension method for Iterate over the rows of the table, calling NextSibling on the 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 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 have to do is get a link to the table and it will find the first tr and repeat all its brothers and sisters.

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

Just a short note regarding Watin performance, I found that some code fragments slow down watin programs significantly (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

0
source

Source: https://habr.com/ru/post/1712565/


All Articles