SpecFlow and WatiN and HTML 5

I recently started testing with SpecFlow and WatiN. A problem that I stumbled on was that in my binding class, I had a method to type input into textfields:

        [When(@"jag angett följande data:")]
        public void NarJagAngettFoljandeData(Table table)
        {
            foreach (var tableRow in table.Rows)
            {
                string id = tableRow["Fält"];
                var field = WebBrowser.Current.TextField(id);
                field.TypeText(tableRow["Värde"]);
            }
        }

But for some fields, it failed with the following exception: “WatiN.Core.Exceptions.ElementNotFoundException: Could not find INPUT (hidden) or INPUT (password) or INPUT (text) or INPUT (textarea) or TEXTAREA element tag matching criteria…”

It turned out some of my fields didn’t have type=”text” but type=”number” and type=”datetime”. So thanks to StackOverflow I found a solution: Create an extended textfield like this:

    [ElementTag("input", InputType = "text", Index = 0)]
    [ElementTag("input", InputType = "password", Index = 1)]
    [ElementTag("input", InputType = "textarea", Index = 2)]
    [ElementTag("input", InputType = "hidden", Index = 3)]
    [ElementTag("textarea", Index = 4)]
    [ElementTag("input", InputType = "email", Index = 5)]
    [ElementTag("input", InputType = "url", Index = 6)]
    [ElementTag("input", InputType = "number", Index = 7)]
    [ElementTag("input", InputType = "range", Index = 8)]
    [ElementTag("input", InputType = "search", Index = 9)]
    [ElementTag("input", InputType = "color", Index = 10)]
    [ElementTag("input", InputType = "datetime", Index = 11)]
    public class TextFieldExtended : TextField
    {
        public TextFieldExtended(DomContainer domContainer, INativeElement element)
            : base(domContainer, element)
        {
        }

        public TextFieldExtended(DomContainer domContainer, ElementFinder finder)
            : base(domContainer, finder)
        {
        }

        public static void Register()
        {
            var typeToRegister = typeof(TextFieldExtended);
            ElementFactory.RegisterElementType(typeToRegister);
        }
    }

Then, change the binding method:

                //var field = WebBrowser.Current.TextField(id);
                var field = WebBrowser.Current.ElementOfType<TextFieldExtended>(id);

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s