Пример #1
0
        private object ElementFor(PropertySelectionContext ctx)
        {
            if (ctx.RawSelectedElement != null)
            {
                return(ctx.RawSelectedElement);
            }

            if (ctx.Attributes.Count > 1)
            {
                throw new Exception("Only one selection attribute is valid per property.");
            }
            if (ctx.IsPropertySetter)
            {
                throw new Exception("You can't set a property that has a selection attribute.");
            }
            return(ctx.RawSelectedElement = _elementSelectionHandler.SelectElement(ctx.FirstAttribute, ctx.TargetProperty));
        }
Пример #2
0
        public object PostProcessReturnValue(object current, PropertySelectionContext ctx, PassengerConfiguration configuration)
        {
            if (current == null)
            {
                return(null);
            }

            var snapshotOfCollection = new List <object>();

            if (current.IsCollection())
            {
                snapshotOfCollection.AddRange(((IEnumerable)current).Cast <object>());
            }

            if (!ctx.IsPageComponent)
            {
                if (ctx.RawSelectedElement.IsAWebElement() &&
                    ctx.TargetProperty.PropertyType.IsAPassengerElement())
                {
                    current = TypeMapping.WrapIntoPassengerElement(ctx.RawSelectedElement, ctx.TargetProperty.PropertyType);
                }

                if (ctx.RawSelectedElement.IsCollection() &&
                    ctx.TargetProperty.PropertyType.IsCollection())
                {
                    current = TypeMapping.BuildCollectionOfWrappers(ctx.RawSelectedElement, ctx.TargetProperty.PropertyType, _configuration);
                }
            }

            if (current.IsAPassengerElement())
            {
                ((IPassengerElement)current).Inner = ctx.RawSelectedElement as IWebElement;
            }

            if (current.IsCollection())
            {
                MapSnapshotToInnerProperty(current, snapshotOfCollection);
            }

            return(current);
        }
Пример #3
0
        public void Intercept(IInvocation invocation)
        {
            if (!invocation.Method.IsProperty())
            {
                invocation.Proceed();
                _postProcessor.PostProcessReturnValue(invocation);
                return;
            }

            var ctx = new PropertySelectionContext(invocation);

            var result = new InvocationSwitchboard
            {
                { () => SubstituteFor(ctx) != null, () => Assign(SubstituteFor(ctx)) },
                { () => !ctx.Attributes.Any() && !ctx.IsPageComponent, () => Proceed },
                { () => ctx.IsPageComponent, () => Assign(ProxyFor(ctx.TargetProperty)) },
                { () => ElementFor(ctx) != null, () => Assign(ElementFor(ctx)) }
            }.Route();

            EnsureAnyElementsAreSafelyLoaded(ctx);

            invocation.Assign(result);
            invocation.ReturnValue = _postProcessor.PostProcessReturnValue(invocation.ReturnValue, ctx, _configuration);
        }
Пример #4
0
 private void EnsureAnyElementsAreSafelyLoaded(PropertySelectionContext ctx)
 {
     try { ElementFor(ctx); } catch { /* ¯_(ツ)_/¯ */ }
 }
Пример #5
0
 private object SubstituteFor(PropertySelectionContext ctx)
 {
     return(_typeSubstitution.FindSubstituteFor(ctx.TargetProperty.PropertyType)?.GetInstance());
 }