Exemplo n.º 1
0
        private static By CreateLocator(MemberInfo member)
        {
            LocateAttribute castedAttribute = GetAttribute <LocateAttribute>(member);

            if (castedAttribute.Using == null)
            {
                castedAttribute.Using = member.Name;
            }

            return(castedAttribute.Finder);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Compare this object to another object
        /// </summary>
        /// <param name="obj">other object</param>
        /// <returns>the difference</returns>
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj", "Object to compare cannot be null");
            }

            LocateAttribute other = obj as LocateAttribute;

            if (other == null)
            {
                throw new ArgumentException("Object to compare must be a LocateAttribute", "obj");
            }
            return(0);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Determines whether the specified <see cref="System.Object">Object</see> is equal
        /// to the current <see cref="System.Object">Object</see>.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object">Object</see> to compare with the
        /// current <see cref="System.Object">Object</see>.</param>
        /// <returns><see langword="true"/> if the specified <see cref="System.Object">Object</see>
        /// is equal to the current <see cref="System.Object">Object</see>; otherwise,
        /// <see langword="false"/>.</returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            LocateAttribute other = obj as LocateAttribute;

            if (other == null)
            {
                return(false);
            }

            if (other.Finder != this.Finder)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 4
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);
                    }
                }
            }
        }