Speed Up Typing Text with WatiN

Posted by Petr Kozelek
Aug 05 2010

I use WatiN for UA testing and I was facing the problem that WatiN is slow while typing text into text field with TextField.TypeText(string) method.

TypeText(string) is slow because during run it fires sequences of key events: KeyDown(char), KeyPress(char), KeyUp(char). The sequence is invoked as many times as the length of the input string is. As the result, the input text is typed to the text field one by one char. It is slow and when you have hundreds of tests then you wait tens of minutes for their execution! I created simple extension method to speed up typing of text markedly – for a string 10 chars long the performance of typing is around 30 times higher.

public static class WatiNExtensions
{
    public static void TypeTextQuickly(this TextField textField, string text)
    {
        textField.SetAttributeValue("value", text);
    }
}

In my solution I do not use simulated key events. I use direct setting of value attribute in text field. The limitation of this solution is that it does not raise focus and blur events. Additionally, no key events are fired.

Usage is simple:

var browser = new IE("http://google.com");
browser.TextField("q").TypeTextQuickly("WatiN"); // fast - raises neither key events nor focus and blur on text field
// browser.TextField("q").TypeText("WatiN"); // slow - raises key events

3 Responses

  1. Mohammad Ashour says:

    Great Tip! Thank you, just sped up my Watin tests significantly.

  2. Ed says:

    Thanks, this is a great tip. I would also point out that another limitation of this approach is that it is possible to set the value of a disabled input field, which would be inconsistent with what the user is able to do. However, that is an unlikely scenario and the benefits in this case vastly outweigh the potential drawback.

  3. Good point about the disabled field. I added an assertion in my extension method to check that the edit field does not have the disabled attribute applied.

Trackback URL for this entry