/// <summary> /// Initializes the elements in the Page Object. /// </summary> /// <param name="driver">The driver used to find elements on the page.</param> /// <param name="page">The Page Object to be populated with elements.</param> public static void InitElements(ISearchContext driver, object page) { const BindingFlags BindingOptions = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy; if (page == null) { throw new ArgumentNullException("page", "page cannot be null"); } var type = page.GetType(); var fields = type.GetFields(BindingOptions); var properties = type.GetProperties(BindingOptions); var members = new List <MemberInfo>(fields); members.AddRange(properties); foreach (var member in members) { var attributes = member.GetCustomAttributes(typeof(FindsByAttribute), true); foreach (var attribute in attributes) { var castedAttribute = (FindsByAttribute)attribute; var generator = new ProxyGenerator(); var cacheAttributeType = typeof(CacheLookupAttribute); var cache = member.GetCustomAttributes(cacheAttributeType, true).Length != 0 || member.DeclaringType.GetCustomAttributes(cacheAttributeType, true).Length != 0; var interceptor = new ProxiedWebElementInterceptor(driver, castedAttribute.Bys, cache); var options = new ProxyGenerationOptions { BaseTypeForInterfaceProxy = typeof(WebElementProxyComparer) }; var field = member as FieldInfo; var property = member as PropertyInfo; if (field != null) { var proxyElement = generator.CreateInterfaceProxyWithoutTarget( typeof(IWrapsElement), new[] { field.FieldType }, options, interceptor); field.SetValue(page, proxyElement); } else if (property != null) { var proxyElement = generator.CreateInterfaceProxyWithoutTarget( typeof(IWrapsElement), new[] { property.PropertyType }, options, interceptor); property.SetValue(page, proxyElement, null); } } } }
/// <summary> /// Initializes the elements in the Page Object. /// </summary> /// <param name="driver">The driver used to find elements on the page.</param> /// <param name="page">The Page Object to be populated with elements.</param> public static void InitElements(ISearchContext driver, object page) { const BindingFlags BindingOptions = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy; if (page == null) { throw new ArgumentNullException("page", "page cannot be null"); } var type = page.GetType(); var fields = type.GetFields(BindingOptions); var properties = type.GetProperties(BindingOptions); var members = new List<MemberInfo>(fields); members.AddRange(properties); foreach (var member in members) { var attributes = member.GetCustomAttributes(typeof(FindsByAttribute), true); foreach (var attribute in attributes) { var castedAttribute = (FindsByAttribute)attribute; var generator = new ProxyGenerator(); var cacheAttributeType = typeof(CacheLookupAttribute); var cache = member.GetCustomAttributes(cacheAttributeType, true).Length != 0 || member.DeclaringType.GetCustomAttributes(cacheAttributeType, true).Length != 0; var interceptor = new ProxiedWebElementInterceptor(driver, castedAttribute.Bys, cache); var options = new ProxyGenerationOptions { BaseTypeForInterfaceProxy = typeof(WebElementProxyComparer) }; var field = member as FieldInfo; var property = member as PropertyInfo; if (field != null) { var proxyElement = generator.CreateInterfaceProxyWithoutTarget( typeof(IWrapsElement), new[] { field.FieldType }, options, interceptor); field.SetValue(page, proxyElement); } else if (property != null) { var proxyElement = generator.CreateInterfaceProxyWithoutTarget( typeof(IWrapsElement), new[] { property.PropertyType }, options, interceptor); property.SetValue(page, proxyElement, null); } } } }
/// <summary> /// Initializes the elements in the Page Object. /// </summary> /// <param name="driver">The driver used to find elements on the page.</param> /// <param name="page">The Page Object to be populated with elements.</param> public static void InitElements(ISearchContext driver, object page) { if (page == null) { throw new ArgumentNullException("page", "page cannot be null"); } var type = page.GetType(); var members = new List <MemberInfo>(); const BindingFlags PublicBindingOptions = BindingFlags.Instance | BindingFlags.Public; members.AddRange(type.GetFields(PublicBindingOptions)); members.AddRange(type.GetProperties(PublicBindingOptions)); while (type != null) { const BindingFlags NonPublicBindingOptions = BindingFlags.Instance | BindingFlags.NonPublic; members.AddRange(type.GetFields(NonPublicBindingOptions)); members.AddRange(type.GetProperties(NonPublicBindingOptions)); type = type.BaseType; } foreach (var member in members) { List <By> bys = new List <By>(); bool cache = false; var attributes = Attribute.GetCustomAttributes(member, typeof(FindsByAttribute), true); if (attributes.Length > 0) { Array.Sort(attributes); foreach (var attribute in attributes) { var castedAttribute = (FindsByAttribute)attribute; if (castedAttribute.Using == null) { castedAttribute.Using = member.Name; } bys.Add(castedAttribute.Finder); } var cacheAttributeType = typeof(CacheLookupAttribute); cache = member.GetCustomAttributes(cacheAttributeType, true).Length != 0 || member.DeclaringType.GetCustomAttributes(cacheAttributeType, true).Length != 0; var interceptor = new ProxiedWebElementInterceptor(driver, bys, cache); var options = new ProxyGenerationOptions { BaseTypeForInterfaceProxy = typeof(WebElementProxyComparer) }; var field = member as FieldInfo; var property = member as PropertyInfo; if (field != null) { var proxyElement = generator.CreateInterfaceProxyWithoutTarget( typeof(IWrapsElement), new[] { field.FieldType, typeof(ILocatable) }, options, interceptor); field.SetValue(page, proxyElement); } else if (property != null) { var proxyElement = generator.CreateInterfaceProxyWithoutTarget( typeof(IWrapsElement), new[] { property.PropertyType, typeof(ILocatable) }, options, interceptor); property.SetValue(page, proxyElement, null); } } } }
/// <summary> /// Initializes the elements in the Page Object. /// </summary> /// <param name="driver">The driver used to find elements on the page.</param> /// <param name="page">The Page Object to be populated with elements.</param> public static void InitElements(ISearchContext driver, object page) { if (page == null) { throw new ArgumentNullException("page", "page cannot be null"); } var type = page.GetType(); var members = new List<MemberInfo>(); const BindingFlags PublicBindingOptions = BindingFlags.Instance | BindingFlags.Public; members.AddRange(type.GetFields(PublicBindingOptions)); members.AddRange(type.GetProperties(PublicBindingOptions)); while (type != null) { const BindingFlags NonPublicBindingOptions = BindingFlags.Instance | BindingFlags.NonPublic; members.AddRange(type.GetFields(NonPublicBindingOptions)); members.AddRange(type.GetProperties(NonPublicBindingOptions)); type = type.BaseType; } foreach (var member in members) { List<By> bys = new List<By>(); bool cache = false; var attributes = Attribute.GetCustomAttributes(member, typeof(FindsByAttribute), true); if (attributes.Length > 0) { Array.Sort(attributes); foreach (var attribute in attributes) { var castedAttribute = (FindsByAttribute)attribute; if (castedAttribute.Using == null) { castedAttribute.Using = member.Name; } bys.Add(castedAttribute.Finder); } var cacheAttributeType = typeof(CacheLookupAttribute); cache = member.GetCustomAttributes(cacheAttributeType, true).Length != 0 || member.DeclaringType.GetCustomAttributes(cacheAttributeType, true).Length != 0; var interceptor = new ProxiedWebElementInterceptor(driver, bys, cache); var options = new ProxyGenerationOptions { BaseTypeForInterfaceProxy = typeof(WebElementProxyComparer) }; var field = member as FieldInfo; var property = member as PropertyInfo; if (field != null) { var proxyElement = generator.CreateInterfaceProxyWithoutTarget( typeof(IWrapsElement), new[] { field.FieldType, typeof(ILocatable) }, options, interceptor); field.SetValue(page, proxyElement); } else if (property != null) { var proxyElement = generator.CreateInterfaceProxyWithoutTarget( typeof(IWrapsElement), new[] { property.PropertyType, typeof(ILocatable) }, options, interceptor); property.SetValue(page, proxyElement, null); } } } }