views:

20

answers:

1

Based on this article, I've written a custom class which implements the Watin.Core.interfaces.IFindByDefaultFactory, but I don't think I'm correctly assigning it to the watin settings, because it is never used.

Basically, Where/when should I assign to the Settings.FindByDefaultFactory? I've tried in my test Setup, and the text fixture's constructor, but neither seem to cause my custom class to be used. The tests still run and work, but I have to use the full asp.net ID's.

I'm using Watin 2.0.15.928 in VS2008 from nUnit 2.5.2.9222. I am running visual studio as administrator, and tests run sucessfully as long as I don't rely on my custom find logic.

Here's what the start of my text fixture looks like, where I set the FindByDefaultFactory

namespace Fundsmith.Web.Main.BrowserTests
{
    [TestFixture]
    class WatinHomepageTests
    {
        private IE _ie;
        [SetUp]
        public void Setup()
        {
            Settings.FindByDefaultFactory = new FindByAspIdFactory();
            _ie = new IE("http://localhost/somepage.aspx");
        }
//etc etc...

And this is what my custom Find By Default factory looks like (simplified), unfortunately, it's never called.

using System.Text.RegularExpressions;
using WatiN.Core;
using WatiN.Core.Constraints;
using WatiN.Core.Interfaces;

namespace Fundsmith.Web.Main.BrowserTests
{
    public class FindByAspIdFactory : IFindByDefaultFactory
    {
        public Constraint ByDefault(string value)
        {
// This code is never called :(

// My custom find by id code to cope with asp.net webforms ids...
            return Find.ById(value);
        }

        public Constraint ByDefault(Regex value)
        {
            return Find.ById(value);
        }
    }
}

Edit: Extra information after the fact.

Based on me fuguring this out, (see answer below), It turns out that the way I was consuming Watin to find the elements was wrong. I was explicitly calling Find.ById, rather than letting the default action occur. So I'd reassigned the default but was then failing to use it!

[Test]
public void StepOneFromHomepageShouldRedirectToStepTwo()
{
    _ie.TextField(Find.ById("textBoxId")).TypeText("100");
        //Other test stuff...
}
A: 

Right, I've figured this one out, and it was me being an idiot and explicitly calling the Find.ById method, rather than letting the default action occur. It seems the test setup is a fine place to set the FindByDefaultFactory.

ie, I was doing this (wrong):

    [Test]
    public void StepOneFromHomepageShouldRedirectToStepTwo()
    {
        _ie.TextField(Find.ById("textBoxId")).TypeText("100");
            //Other test stuff...
    }

When I should have been simply doing this. (Without the explicit "Find.ById")

    [Test]
    public void StepOneFromHomepageShouldRedirectToStepTwo()
    {
        _ie.TextField("textBoxId").TypeText("100");
            //Other test stuff...
    }

Not only was this me being stupid, but I didn't include this in my original question, so it would have been impossible for anyone else to figure it out for certain. Double slaps for me.

Andrew M