Esempio n. 1
0
        private static void InitControlProperty <TOwner>(UIComponent <TOwner> parentComponent, PropertyInfo property)
            where TOwner : PageObject <TOwner>
        {
            UIComponentMetadata metadata = CreateStaticControlMetadata <TOwner>(property);

            UIComponent <TOwner> component = CreateComponent(parentComponent, metadata);

            parentComponent.Controls.Add(component);

            property.SetValue(parentComponent, component, null);
        }
Esempio n. 2
0
        private static void InitPageObject <TPageObject>(PageObject <TPageObject> pageObject)
            where TPageObject : PageObject <TPageObject>
        {
            pageObject.Owner = (TPageObject)pageObject;

            UIComponentMetadata metadata = CreatePageObjectMetadata <TPageObject>();

            InitPageObjectTriggers(pageObject, metadata);

            ApplyMetadata(pageObject, metadata);
        }
Esempio n. 3
0
        protected internal override void ApplyMetadata(UIComponentMetadata metadata)
        {
            base.ApplyMetadata(metadata);

            WindowTitleAttribute titleAttribute = metadata.Get <WindowTitleAttribute>(x => x.At(AttributeLevels.Component));

            if (titleAttribute != null)
            {
                WindowTitleValues = titleAttribute.GetActualValues(ComponentName);
                WindowTitleMatch  = titleAttribute.Match;
            }
        }
Esempio n. 4
0
        private static string RandomizeString(UIComponentMetadata metadata)
        {
            if (!TryRandomizeOneOfIncluded(metadata, out string value))
            {
                var attribute = metadata.Get <RandomizeStringSettingsAttribute>()
                                ?? new RandomizeStringSettingsAttribute();

                value = Randomizer.GetString(attribute.Format, attribute.NumberOfCharacters);
            }

            return(value);
        }
Esempio n. 5
0
        private static void ApplyMetadata <TOwner>(UIComponent <TOwner> component, UIComponentMetadata metadata)
            where TOwner : PageObject <TOwner>
        {
            foreach (IPropertySettings propertySettings in metadata.AllAttributes.OfType <IPropertySettings>().Where(x => x.Properties != null))
            {
                propertySettings.Properties.Metadata = metadata;
            }

            component.Metadata = metadata;
            component.ApplyMetadata(metadata);
            component.Triggers.ApplyMetadata(metadata);
        }
Esempio n. 6
0
        private static T RandomizeNumber <T>(UIComponentMetadata metadata)
        {
            if (!TryRandomizeOneOfIncluded(metadata, out T value))
            {
                var attribute = metadata.Get <RandomizeNumberSettingsAttribute>()
                                ?? new RandomizeNumberSettingsAttribute();

                decimal valueAsDecimal = Randomizer.GetDecimal(attribute.Min, attribute.Max, attribute.Precision);
                value = (T)Convert.ChangeType(valueAsDecimal, typeof(T));
            }

            return(value);
        }
Esempio n. 7
0
        private static void ApplyMetadataToTriggers(UIComponentMetadata metadata, IEnumerable <TriggerAttribute> triggers)
        {
            foreach (TriggerAttribute trigger in triggers)
            {
                trigger.ApplyMetadata(metadata);

                IPropertySettings triggerAsPropertySettings = trigger as IPropertySettings;
                if (triggerAsPropertySettings != null)
                {
                    triggerAsPropertySettings.Properties.Metadata = metadata;
                }
            }
        }
Esempio n. 8
0
 private static bool TryRandomizeOneOfIncluded <T>(UIComponentMetadata metadata, out T value)
 {
     T[] includeValues = GetRandomizeIncludeValues <T>(metadata);
     if (includeValues == null || includeValues.Length == 0)
     {
         value = default(T);
         return(false);
     }
     else
     {
         value = Randomizer.GetOneOf(includeValues);
         return(true);
     }
 }
Esempio n. 9
0
        private static string RandomizeString(UIComponentMetadata metadata)
        {
            string value;

            if (!TryRandomizeOneOfIncluded(metadata, out value))
            {
                var attribute = metadata.Get <RandomizeStringSettingsAttribute>(x => x.At(AttributeLevels.Declared)) ?? new RandomizeStringSettingsAttribute();

                string format = NormalizeStringFormat(attribute.Format);
                value = Randomizer.GetString(format, attribute.NumberOfCharacters);
            }

            return(value);
        }
Esempio n. 10
0
        public static T GetRandom <T>(UIComponentMetadata metadata)
        {
            Type          type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
            RandomizeFunc randomizeFunction;

            if (Randomizers.TryGetValue(type, out randomizeFunction))
            {
                return((T)randomizeFunction(metadata));
            }
            else
            {
                throw new InvalidOperationException("Cannot get random value for '{0}' type. There is no registered randomizer for this type.".FormatWith(typeof(T).FullName));
            }
        }
Esempio n. 11
0
        private static string GetComponentNameFromMetadata(UIComponentMetadata metadata)
        {
            if (metadata.Name is null)
            {
                FindAttribute findAttribute = metadata.ResolveFindAttribute();

                return(findAttribute.BuildComponentName());
            }
            else
            {
                return(metadata.ComponentDefinitionAttribute.
                       NormalizeNameIgnoringEnding(metadata.Name).
                       ToString(TermCase.Title));
            }
        }
Esempio n. 12
0
        private static string GetControlNameFromFindAttribute(UIComponentMetadata metadata)
        {
            FindAttribute findAttribute = metadata.ResolveFindAttribute();

            if (findAttribute is FindByLabelAttribute findByLabelAttribute && findByLabelAttribute.Match == TermMatch.Equals)
            {
                string[] terms = findByLabelAttribute.Values;

                if (terms?.Any() ?? false)
                {
                    return(string.Join("/", terms));
                }
            }

            return(null);
        }
Esempio n. 13
0
        private static void InitComponentLocator(UIComponent component, UIComponentMetadata metadata)
        {
            // TODO: Remove this condition when IItemsControl will be removed.
#pragma warning disable CS0618
            if (component is IItemsControl itemsControl)
#pragma warning restore CS0618
            {
                IFindItemAttribute       findItemAttribute       = GetPropertyFindItemAttribute(metadata);
                IItemElementFindStrategy itemElementFindStrategy = findItemAttribute.CreateStrategy(component, metadata);
                itemsControl.Apply(itemElementFindStrategy);
            }

            component.ScopeLocator = new StrategyScopeLocator(
                new StrategyScopeLocatorExecutionDataCollector(component),
                StrategyScopeLocatorExecutor.Default);
        }
Esempio n. 14
0
        private static void InitComponentLocator(UIComponent component, UIComponentMetadata metadata, FindAttribute findAttribute)
        {
            ComponentScopeLocateOptions   locateOptions = CreateScopeLocateOptions(metadata, findAttribute);
            IComponentScopeLocateStrategy strategy      = findAttribute.CreateStrategy(metadata);

            component.ScopeSource = findAttribute.ScopeSource;

            if (component is IItemsControl itemsControl)
            {
                IFindItemAttribute       findItemAttribute       = GetPropertyFindItemAttribute(metadata);
                IItemElementFindStrategy itemElementFindStrategy = findItemAttribute.CreateStrategy(component, metadata);
                itemsControl.Apply(itemElementFindStrategy);
            }

            component.ScopeLocator = new StrategyScopeLocator(component, strategy, locateOptions);
        }
Esempio n. 15
0
        public static T GetRandom <T>(UIComponentMetadata metadata)
        {
            Type type = typeof(T);

            type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);

            RandomizeFunc randomizeFunction;

            if (Randomizers.TryGetValue(type, out randomizeFunction))
            {
                return((T)randomizeFunction(metadata));
            }
            else
            {
                throw new InvalidOperationException($"Cannot get random value for '{type.FullName}' type. There is no registered randomizer for this type. Use {nameof(ValueRandomizer)}.{nameof(RegisterRandomizer)} method to register custom randomizer.");
            }
        }
Esempio n. 16
0
        private static T[] GetEnumOptionValues <T>(Type enumType, UIComponentMetadata metadata)
        {
            T[] values = GetRandomizeIncludeValues <T>(metadata);
            if (values == null || values.Length == 0)
            {
                values = enumType.GetIndividualEnumFlags().Cast <T>().ToArray();
            }

            var excludeAttribute = metadata.Get <RandomizeExcludeAttribute>(x => x.At(AttributeLevels.Declared));

            if (excludeAttribute?.Values?.Any() ?? false)
            {
                var valuesToExclude = excludeAttribute.Values.Cast <T>();
                values = values.Except(valuesToExclude).ToArray();
            }

            return(values);
        }
Esempio n. 17
0
        public static TComponent CreateControlForProperty <TComponent, TOwner>(UIComponent <TOwner> parentComponent, string propertyName, params Attribute[] attributes)
            where TComponent : UIComponent <TOwner>
            where TOwner : PageObject <TOwner>
        {
            PropertyInfo        property = parentComponent.GetType().GetPropertyWithThrowOnError(propertyName, typeof(TComponent), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            UIComponentMetadata metadata = CreateStaticControlMetadata(parentComponent, property);

            if (attributes != null)
            {
                metadata.Push(attributes);
            }

            var component = (TComponent)CreateComponent(parentComponent, metadata);

            parentComponent.Controls.Add(component);

            return(component);
        }
Esempio n. 18
0
        private static void InitComponentLocator(UIComponent component, UIComponentMetadata metadata, FindAttribute findAttribute)
        {
            ComponentScopeLocateOptions   locateOptions = CreateScopeLocateOptions(metadata, findAttribute);
            IComponentScopeLocateStrategy strategy      = findAttribute.CreateStrategy(metadata);

            component.ScopeSource = findAttribute.ScopeSource;

            // TODO: Remove this condition when IItemsControl will be removed.
#pragma warning disable CS0618
            if (component is IItemsControl itemsControl)
#pragma warning restore CS0618
            {
                IFindItemAttribute       findItemAttribute       = GetPropertyFindItemAttribute(metadata);
                IItemElementFindStrategy itemElementFindStrategy = findItemAttribute.CreateStrategy(component, metadata);
                itemsControl.Apply(itemElementFindStrategy);
            }

            component.ScopeLocator = new StrategyScopeLocator(component, strategy, locateOptions);
        }
Esempio n. 19
0
        private static void InitDelegateProperty <TOwner>(UIComponent <TOwner> parentComponent, PropertyInfo property)
            where TOwner : PageObject <TOwner>
        {
            Type controlType = ResolveDelegateControlType(property.PropertyType);

            if (controlType != null)
            {
                UIComponentMetadata metadata = CreateStaticControlMetadata(parentComponent, property, controlType);

                UIComponent <TOwner> component = CreateComponent(parentComponent, metadata);
                parentComponent.Controls.Add(component);

                Delegate clickDelegate = CreateDelegatePropertyDelegate(property, component);

                property.SetValue(parentComponent, clickDelegate, null);

                DelegateControls[clickDelegate] = component;
            }
        }
Esempio n. 20
0
        /// <summary>
        /// Calculates the target rank.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <returns>The rank.</returns>
        public virtual int?CalculateTargetRank(UIComponentMetadata metadata)
        {
            if (!IsNameApplicable(metadata.Name))
            {
                return(null);
            }

            int?depthOfTypeInheritance = GetDepthOfInheritance(TargetTypes, metadata.ComponentType);

            if (depthOfTypeInheritance == null)
            {
                return(null);
            }

            int?depthOfParentTypeInheritance = GetDepthOfInheritance(TargetParentTypes, metadata.ParentComponentType);

            if (depthOfParentTypeInheritance == null)
            {
                return(null);
            }

            int rank       = 0;
            int rankFactor = 100000;

            if (TargetNames != null && TargetNames.Any())
            {
                rank += rankFactor;
            }
            rankFactor /= 2;

            if (depthOfTypeInheritance >= 0)
            {
                rank += Math.Max(rankFactor - (depthOfTypeInheritance.Value * 100), 0);
            }
            rankFactor /= 2;

            if (depthOfParentTypeInheritance >= 0)
            {
                rank += Math.Max(rankFactor - (depthOfParentTypeInheritance.Value * 100), 0);
            }

            return(rank);
        }
Esempio n. 21
0
        private static string GetControlNameFromFindAttribute(UIComponentMetadata metadata, FindAttribute findAttribute)
        {
            if (findAttribute is FindByLabelAttribute findByLabelAttribute && findByLabelAttribute.Match == TermMatch.Equals)
            {
                if (findByLabelAttribute.Values?.Any() ?? false)
                {
                    return(string.Join("/", findByLabelAttribute.Values));
                }
                else
                {
                    TermAttribute termAttribute = metadata.Get <TermAttribute>(x => x.At(AttributeLevels.Declared));
                    if (termAttribute?.Values?.Any() ?? false)
                    {
                        return(string.Join("/", termAttribute.Values));
                    }
                }
            }

            return(null);
        }
Esempio n. 22
0
        private static T RandomizeFlagsEnum <T>(Type enumType, UIComponentMetadata metadata)
        {
            var optionValues   = GetEnumOptionValues <T>(enumType, metadata);
            var countAttribute = metadata.Get <RandomizeCountAttribute>(x => x.At(AttributeLevels.Declared));

            int minCount = countAttribute?.Min ?? 1;
            int maxCount = countAttribute?.Max ?? 1;

            T[] valuesAsArray = Randomizer.GetManyOf(minCount, maxCount, optionValues);

            if (valuesAsArray.Length > 1)
            {
                Enum first = (Enum)(object)valuesAsArray[0];
                return((T)(object)valuesAsArray.Skip(1).Cast <Enum>().Aggregate(first, (a, b) => a.AddFlag(b)));
            }
            else
            {
                return(valuesAsArray[0]);
            }
        }
Esempio n. 23
0
        private static FindAttribute GetPropertyFindAttribute(UIComponentMetadata metadata)
        {
            FindAttribute findAttribute = metadata.Get <FindAttribute>(x => x.At(AttributeLevels.Declared));

            if (findAttribute != null)
            {
                return(findAttribute);
            }
            else
            {
                Type controlType         = metadata.ComponentType;
                Type parentComponentType = metadata.ParentComponentType;

                ControlFindingAttribute controlFindingAttribute =
                    GetNearestControlFindingAttribute(controlType, parentComponentType, metadata.ParentComponentAttributes) ??
                    GetNearestControlFindingAttribute(controlType, parentComponentType, metadata.AssemblyAttributes) ??
                    GetNearestDefaultControlFindingAttribute(parentComponentType, metadata.ComponentAttributes);

                return(controlFindingAttribute != null
                    ? controlFindingAttribute.CreateFindAttribute()
                    : GetDefaultFindAttribute(metadata));
            }
        }
Esempio n. 24
0
        private static UIComponentMetadata CreateComponentMetadata <TOwner>(
            UIComponent <TOwner> parentComponent,
            string name,
            Type componentType,
            Attribute[] declaredAttributes)
            where TOwner : PageObject <TOwner>
        {
            Type parentComponentType = parentComponent?.GetType();

            UIComponentMetadata metadata = new UIComponentMetadata(name, componentType, parentComponentType);

            metadata.DeclaredAttributesList.AddRange(declaredAttributes);

            if (parentComponent != null)
            {
                metadata.ParentDeclaredAttributesList = parentComponent.Metadata.DeclaredAttributesList;
            }

            metadata.ParentComponentAttributesList.AddRange(GetClassAttributes(parentComponentType));
            metadata.AssemblyAttributesList.AddRange(GetAssemblyAttributes(typeof(TOwner).Assembly));
            metadata.ComponentAttributesList.AddRange(GetClassAttributes(componentType));

            return(metadata);
        }
Esempio n. 25
0
        public static TComponent CreateControl <TComponent, TOwner>(UIComponent <TOwner> parentComponent, string name, params Attribute[] attributes)
            where TComponent : UIComponent <TOwner>
            where TOwner : PageObject <TOwner>
        {
            parentComponent.CheckNotNull(nameof(parentComponent));

            attributes = attributes?.Where(x => x != null).ToArray() ?? new Attribute[0];

            if (!attributes.OfType <NameAttribute>().Any())
            {
                attributes = attributes.Concat(new[]
                {
                    new NameAttribute(name)
                }).ToArray();
            }

            UIComponentMetadata metadata = CreateComponentMetadata(
                parentComponent,
                name,
                typeof(TComponent),
                attributes);

            return((TComponent)CreateComponent(parentComponent, metadata));
        }
Esempio n. 26
0
        public static T GetRandom <T>(UIComponentMetadata metadata)
        {
            Type type = typeof(T);

            type = Nullable.GetUnderlyingType(type) ?? type;

            if (Randomizers.TryGetValue(type, out RandomizeFunc randomizeFunction))
            {
                return((T)randomizeFunction(metadata));
            }
            else if (type.IsEnum)
            {
                return(type.IsDefined(typeof(FlagsAttribute), false)
                    ? RandomizeFlagsEnum <T>(type, metadata)
                    : RandomizeNonFlagEnum <T>(type, metadata));
            }
            else
            {
                throw new InvalidOperationException(
                          $"Cannot get random value for '{type.FullName}' type. " +
                          $"There is no registered randomizer for this type. " +
                          $"Use {nameof(ValueRandomizer)}.{nameof(RegisterRandomizer)} method to register custom randomizer.");
            }
        }
Esempio n. 27
0
 protected internal virtual void ApplyMetadata(UIComponentMetadata metadata)
 {
 }
Esempio n. 28
0
        private static void InitPageObjectTriggers <TOwner>(PageObject <TOwner> pageObject, UIComponentMetadata metadata)
            where TOwner : PageObject <TOwner>
        {
            var componentTriggers = metadata.ComponentAttributes.
                                    OfType <TriggerAttribute>().
                                    Where(x => x.AppliesTo == TriggerScope.Self);

            pageObject.Triggers.ComponentTriggersList.AddRange(componentTriggers);

            var assemblyTriggers = metadata.AssemblyAttributes.
                                   OfType <TriggerAttribute>();

            pageObject.Triggers.AssemblyTriggersList.AddRange(assemblyTriggers);

            pageObject.Triggers.Reorder();
        }
Esempio n. 29
0
 public static PageObjectDefinitionAttribute GetPageObjectDefinition(UIComponentMetadata metadata)
 {
     return((metadata.ComponentDefinitionAttribute as PageObjectDefinitionAttribute) ?? new PageObjectDefinitionAttribute());
 }
Esempio n. 30
0
 public static ControlDefinitionAttribute GetControlDefinition(UIComponentMetadata metadata)
 {
     return((metadata.ComponentDefinitionAttribute as ControlDefinitionAttribute) ?? new ControlDefinitionAttribute());
 }