/// <summary> /// Initializes the elements in the Page Object. /// </summary> /// <param name="page">The Page Object to be populated with elements.</param> /// <param name="locator">The <see cref="IElementLocator"/> implementation that /// determines how elements are located.</param> /// <param name="decorator">The <see cref="IPageObjectMemberDecorator"/> implementation that /// determines how Page Object members representing elements are discovered and populated.</param> /// <exception cref="ArgumentException"> /// thrown if a field or property decorated with the <see cref="FindsByAttribute"/> is not of type /// <see cref="IWebElement"/> or IList{IWebElement}. /// </exception> public static void InitElements(object page, IElementLocator locator, IPageObjectMemberDecorator decorator) { if (page == null) { throw new ArgumentNullException(nameof(page), "page cannot be null"); } if (locator == null) { throw new ArgumentNullException(nameof(locator), "locator cannot be null"); } if (decorator == null) { throw new ArgumentNullException(nameof(locator), "decorator cannot be null"); } if (locator.SearchContext == null) { throw new ArgumentException("The SearchContext of the locator object cannot be null", nameof(locator)); } const BindingFlags PublicBindingOptions = BindingFlags.Instance | BindingFlags.Public; const BindingFlags NonPublicBindingOptions = BindingFlags.Instance | BindingFlags.NonPublic; // Get a list of all of the fields and properties (public and non-public [private, protected, etc.]) // in the passed-in page object. Note that we walk the inheritance tree to get superclass members. Type?type = page.GetType(); var members = new List <MemberInfo>(); members.AddRange(type.GetFields(PublicBindingOptions)); members.AddRange(type.GetProperties(PublicBindingOptions)); while (type != null) { members.AddRange(type.GetFields(NonPublicBindingOptions)); members.AddRange(type.GetProperties(NonPublicBindingOptions)); type = type.BaseType; } foreach (var member in members) { // Examine each member, and if the decorator returns a non-null object, // set the value of that member to the decorated object. object?decoratedValue = decorator.Decorate(member, locator); if (decoratedValue == null) { continue; } if (member is FieldInfo field) { field.SetValue(page, decoratedValue); } else if (member is PropertyInfo property && property.CanWrite) { property.SetValue(page, decoratedValue, null); } } }
public static void InitElements(object page, IElementLocator locator, IPageObjectMemberDecorator decorator) { if (page == null) { throw new ArgumentNullException("page", "page cannot be null"); } if (locator == null) { throw new ArgumentNullException("locator", "locator cannot be null"); } if (decorator == null) { throw new ArgumentNullException("locator", "decorator cannot be null"); } if (locator.SearchContext == null) { throw new ArgumentException("The SearchContext of the locator object cannot be null", "locator"); } Type type = page.GetType(); List <MemberInfo> list = new List <MemberInfo>(); list.AddRange(type.GetFields(BindingFlags.Instance | BindingFlags.Public)); list.AddRange(type.GetProperties(BindingFlags.Instance | BindingFlags.Public)); while (type != null) { list.AddRange(type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)); list.AddRange(type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic)); type = type.BaseType; } foreach (MemberInfo current in list) { object obj = decorator.Decorate(current, locator); if (obj != null) { FieldInfo fieldInfo = current as FieldInfo; PropertyInfo propertyInfo = current as PropertyInfo; if (fieldInfo != null) { fieldInfo.SetValue(page, obj); } else if (propertyInfo != null) { propertyInfo.SetValue(page, obj, null); } } } }
internal void InitElements(object page, IElementLocator locator) { foreach (var member in MembersToDecorate(page)) { var bys = member.GetCustomAttributes() .Select(x => (x as ByAttribute)?.ByFinder()) .Where(x => x != null) .Distinct() .ToArray(); if (bys.Any()) { //Decorates the member if (CanWriteToMember(member, out var typeToDecorate)) { var decoratedValue = _pageObjectMemberDecorator.Decorate(typeToDecorate, bys, locator); if (decoratedValue != null) { var field = member as FieldInfo; var property = member as PropertyInfo; if (field != null) { field.SetValue(page, decoratedValue); } else if (property != null) { property.SetValue(page, decoratedValue, null); } } } else { throw new DecorationException($"Unable to decorate {member.DeclaringType?.Name}.{member.Name}, it cannot be written to"); } } } }