コード例 #1
0
        private static void ApplyPageSettings(BasePageObject pageObject, IDictionary <string, object> pageSettings)
        {
            IDictionary <string, object> combinedDefaultAndSpecificPageSettings = pageObject.DefaultPageSettings ?? new Dictionary <string, object>();

            foreach (KeyValuePair <string, object> pageSetting in pageSettings)
            {
                combinedDefaultAndSpecificPageSettings[pageSetting.Key] = pageSetting.Value;
            }

            pageObject.CurrentPageSettings = combinedDefaultAndSpecificPageSettings;

            pageObject.ApplySettings();;
        }
コード例 #2
0
 internal static BasicPageElement CreateElementObject(Type fieldType, IWebDriver webDriver, By @by, WaitModel waitModel, BasePageObject parentPageObject, ClickBehaviours clickBehaviours = ClickBehaviours.Default, FillBehaviour fillBehaviour = FillBehaviour.Default)
 {
     if (fieldType == typeof(Button))
     {
         return(new Button(@by, webDriver, waitModel, parentPageObject, clickBehaviours));
     }
     if (fieldType == typeof(Link))
     {
         return(new Link(@by, webDriver, waitModel, parentPageObject, clickBehaviours));
     }
     if (fieldType == typeof(Image))
     {
         return(new Image(@by, webDriver, waitModel, parentPageObject, clickBehaviours));
     }
     if (fieldType == typeof(BasicPageElement))
     {
         return(new BasicPageElement(@by, webDriver, waitModel, parentPageObject));
     }
     if (fieldType == typeof(SelectBox))
     {
         return(new SelectBox(@by, webDriver, waitModel, parentPageObject));
     }
     if (fieldType == typeof(CheckBox))
     {
         return(new CheckBox(@by, webDriver, waitModel, parentPageObject, clickBehaviours));
     }
     if (fieldType == typeof(Input))
     {
         return(new Input(@by, webDriver, waitModel, parentPageObject, clickBehaviours, fillBehaviour));
     }
     if (fieldType == typeof(HiddenElement))
     {
         return(new HiddenElement(@by, webDriver, waitModel, parentPageObject, clickBehaviours));
     }
     return(null);
 }
コード例 #3
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);
                    }
                }
            }
        }
コード例 #4
0
        internal static BasicPageElement CreateElementObject(Type fieldType, IWebDriver webDriver, By @by, WaitModel waitModel, BasePageObject parentPageObject, ClickBehaviours clickBehaviours = ClickBehaviours.Default, FillBehaviour fillBehaviour = FillBehaviour.Default)
        {
            if ((typeof(Button).IsAssignableFrom(fieldType)) ||
                (typeof(Link).IsAssignableFrom(fieldType)) ||
                (typeof(Image).IsAssignableFrom(fieldType)) ||
                (typeof(CheckBox).IsAssignableFrom(fieldType)) ||
                (typeof(HiddenElement).IsAssignableFrom(fieldType)))
            {
                return((BasicPageElement)Activator.CreateInstance(fieldType,
                                                                  BindingFlags.CreateInstance |
                                                                  BindingFlags.Public |
                                                                  BindingFlags.Instance |
                                                                  BindingFlags.OptionalParamBinding, null, new object[] { @by, webDriver, waitModel, parentPageObject, clickBehaviours }, CultureInfo.CurrentCulture));
            }

            if ((typeof(Input).IsAssignableFrom(fieldType)))
            {
                return((BasicPageElement)Activator.CreateInstance(fieldType,
                                                                  BindingFlags.CreateInstance |
                                                                  BindingFlags.Public |
                                                                  BindingFlags.Instance |
                                                                  BindingFlags.OptionalParamBinding, null, new object[] { @by, webDriver, waitModel, parentPageObject, clickBehaviours, fillBehaviour }, CultureInfo.CurrentCulture));
            }


            if ((typeof(BasicPageElement).IsAssignableFrom(fieldType)) ||
                (typeof(SelectBox).IsAssignableFrom(fieldType)))
            {
                return((BasicPageElement)Activator.CreateInstance(fieldType,
                                                                  BindingFlags.CreateInstance |
                                                                  BindingFlags.Public |
                                                                  BindingFlags.Instance |
                                                                  BindingFlags.OptionalParamBinding, null, new object[] { @by, webDriver, waitModel, parentPageObject }, CultureInfo.CurrentCulture));
            }


            return(null);
        }