private static IEnumerable <LocateOptions> CreateLocateOptionsFromAttribute(WaitForElementsOnActionAttribute waitForOnActionAttribute, Type pageObjectType, When when) { if (waitForOnActionAttribute != null && waitForOnActionAttribute.When == when) { LocateOptions[] locateOptions = { new LocateOptions { By = CreateLocatorForRelatedElement(waitForOnActionAttribute.WaitForPageElementWithName, pageObjectType), Visibility = waitForOnActionAttribute.Visibility } }; return(locateOptions); } return(new LocateOptions[0]); }
private static void InitElements(IWebDriver driver, BasePageObject pageObject) { if (pageObject == null) { throw new ArgumentNullException("pageObject"); } Type type = pageObject.GetType(); List <MemberInfo> memberInfos = new List <MemberInfo>(); const BindingFlags publicBindingOptions = BindingFlags.Instance | BindingFlags.Public; memberInfos.AddRange(type.GetFields(publicBindingOptions)); memberInfos.AddRange(type.GetProperties(publicBindingOptions)); while (type != null) { const BindingFlags nonPublicBindingOptions = BindingFlags.Instance | BindingFlags.NonPublic; memberInfos.AddRange(type.GetFields(nonPublicBindingOptions)); memberInfos.AddRange(type.GetProperties(nonPublicBindingOptions)); type = type.BaseType; } Type pageObjectType = pageObject.GetType(); foreach (MemberInfo member in memberInfos) { LocateAttribute locateAttribute = GetAttribute <LocateAttribute>(member); ClickBehaviourAttribute clickBehaviourAttribute = GetAttribute <ClickBehaviourAttribute>(member); ClickBehaviours clickBehaviour = clickBehaviourAttribute != null ? clickBehaviourAttribute.Using : ClickBehaviours.Default; FillBehaviourAttribute fillBehaviourAttribute = GetAttribute <FillBehaviourAttribute>(member); FillBehaviour fillBehaviour = fillBehaviourAttribute != null ? fillBehaviourAttribute.Using : FillBehaviour.Default; WaitAttribute waitAttribute = GetAttribute <WaitAttribute>(member); WaitForElementsOnActionAttribute waitForElementsOnActionAttribute = GetAttribute <WaitForElementsOnActionAttribute>(member); WaitModel waitModel = new WaitModel { WaitBeforeAction = waitAttribute != null ? waitAttribute.BeforePerformAction : Settings.Default.WaitBeforePerformAction, WaitAfterAction = waitAttribute != null ? waitAttribute.AfterPerformAction : Settings.Default.WaitAfterPerformAction, WaitForElementsBeforeAction = CreateLocateOptionsFromAttribute(waitForElementsOnActionAttribute, pageObjectType, When.Before), WaitForElementsAfterAction = CreateLocateOptionsFromAttribute(waitForElementsOnActionAttribute, pageObjectType, When.After), }; if (locateAttribute != null) { By by = CreateLocator(member); object createdElementObject; FieldInfo field = member as FieldInfo; PropertyInfo property = member as PropertyInfo; if (field != null) { createdElementObject = CreateElementObject(field.FieldType, driver, by, waitModel, pageObject, clickBehaviour, fillBehaviour); if (createdElementObject == null) { throw new ArgumentException("Type of field '" + field.Name + "' is not IWebElement or IList<IWebElement>"); } field.SetValue(pageObject, createdElementObject); } else if (property != null) { createdElementObject = CreateElementObject(property.PropertyType, driver, by, waitModel, pageObject, clickBehaviour, fillBehaviour); if (createdElementObject == null) { throw new ArgumentException("Type of property '" + property.Name + "' is not IWebElement or IList<IWebElement>"); } property.SetValue(pageObject, createdElementObject, null); } } PartialPageObjectAttribute partialPageObjectAttribute = GetAttribute <PartialPageObjectAttribute>(member); if (partialPageObjectAttribute != null) { PropertyInfo property = member as PropertyInfo; if (property != null) { MethodInfo castMethod = typeof(PageObjectFactory).GetMethod("CreatePageObject", BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(property.PropertyType); object createdElementObject = castMethod.Invoke(null, new object[] { driver }); if (createdElementObject == null) { throw new ArgumentException("Type of property '" + property.Name + "' is not IWebElement or IList<IWebElement>"); } property.SetValue(pageObject, createdElementObject, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, null, null); } } } }