Beispiel #1
0
        /// <summary>
        /// Creates a list of <see cref="By"/> locators based on the attributes of this member.
        /// </summary>
        /// <param name="member">The <see cref="MemberInfo"/> containing information about
        /// the member of the Page Object class.</param>
        /// <returns>A list of <see cref="By"/> locators based on the attributes of this member.</returns>
        private static ReadOnlyCollection <By> CreateLocatorList(MemberInfo member)
        {
            List <By> bys = DefaultPageObjectMemberDecoratorProxy.CreateLocatorList(member).ToList();

            var allAttributes = member.GetCustomAttributes(true);
            var useSequence   = allAttributes.OfType <FindsBySequenceAttribute>().Any();
            var useAll        = allAttributes.OfType <FindsByAllAttribute>().Any();

            var attributes = allAttributes.OfType <ByAttribute>().ToList();

            if (attributes.Any())
            {
                bys.AddRange(attributes.Select(attribute => attribute.Locator));

                if (useSequence)
                {
                    bys = new List <By>()
                    {
                        new ByChained(bys.ToArray())
                    };
                }
                else if (useAll)
                {
                    bys = new List <By>()
                    {
                        new ByAll(bys.ToArray())
                    };
                }
            }

            return(bys.AsReadOnly());
        }
Beispiel #2
0
        private object DecorateObject(MemberInfo member, IElementLocator locator)
        {
            FieldInfo    field    = member as FieldInfo;
            PropertyInfo property = member as PropertyInfo;

            if (field == null && (property == null || !property.CanWrite))
            {
                return(null);
            }

            Type targetType = GetTargetType(member);

            IList <By> bys = CreateLocatorList(member);

            if (bys.Any())
            {
                bool   cache = DefaultPageObjectMemberDecoratorProxy.ShouldCacheLookup(member);
                object result;

                if (typeof(WebElement).IsAssignableFrom(targetType))
                {
                    var proxyElement = new WebElementProxy(typeof(IWebElement), locator, bys, cache);
                    var args         = new object[] { proxyElement, this._driver };
                    result = Activator.CreateInstance(targetType, args);
                }
                else if (typeof(IWebElement).IsAssignableFrom(targetType))
                {
                    result = new WebElementProxy(typeof(IWebElement), locator, bys, cache).GetTransparentProxy();
                }
                else if (typeof(IList <WebElement>).IsAssignableFrom(targetType))
                {
                    throw new NotImplementedException();
                    //var proxyElements = new ProxyListElement(locator, bys, cache);
                    //var args = new object[] { proxyElements, this._driver };
                    //result = Activator.CreateInstance(targetType, args);
                }
                else if (targetType == typeof(IList <IWebElement>))
                {
                    result = new WebElementListProxy(typeof(IList <IWebElement>), locator, bys, cache).GetTransparentProxy();
                }
                else
                {
                    throw new Exception($"Undefined type of element: '{targetType?.FullName}'");
                }

                return(result);
            }

            return(null);
        }