Saturday, November 26, 2011

WatiN Page Model, FindBy attribute on a property throws a System.ArgumentException


While running WatiN tests in NUnit, I faced this exception as soon as I initialized a page object
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at WatiN.Core.Composite.InitializedProperty.SetValue(Object instance, Object value)
at WatiN.Core.Composite.InitializedMember.Initialize(Object instance, IElementContainer container)
at WatiN.Core.Composite.InitializeContents(IElementContainer container)
at WatiN.Core.Page.InitializeContents()
at WatiN.Core.Page.Initialize(Document document)
at WatiN.Core.Page.CreatePage[TPage](Document document)
at WatiN.Core.Document.Page[TPage]()
System.ArgumentException : Property set method not found.

This would occur as soon as I created an instance of a page class.
PurchaseSubPage pspage = browserinstance.Page<PurchaseSubPage>();
In the automation framework, that we had used, we had modeled each and every page in the UI as a class and extended our own set of controls that would perform actions that were specific to our application.

After delving into the PurchaseSubPage class, I came across this section, in which a FindBy attribute was not removed. 
[FindBy(IdRegex="_GridView_ProductsCoveredUnsupported$")]
            protected ITAMGrid unsupportedproductscoveredresultsgrid;
            [FindBy(IdRegex = "_GridView_ProductsCoveredUnsupported")]
            public override ITAMGrid ResultsGrid
            {
                get
                {
                    return unsupportedproductscoveredresultsgrid;
                }
            }
The FindBy attribute on the ResultsGrid property was the culprit and on removing it, harmony and order were restored.